Skip to main content

aiogram media group observer

Project description

aiogram-mediagroup-handle

aiogram plugin for handling media groups

This library supports aiogram v3 and above.

Overview

aiogram-mediagroup-handle leverages the aiogram.Dispatcher.storage to collect and store media files in FSM data by media_group_id.

FSMStorage ensures data consistency and, combined with data serialization, allows any of the known storage strategies to be used (not just in-memory).

aiogram-mediagroup-handle comes with some utilities:

  • MediaGroupFilter – A filter to detect media groups.
  • MediaGroup – A lightweight dataclass object (the actual media group intake) accessible in FSMStorage data dictionary by media_group_id.

MediaGroup Dataclass reference

from dataclasses import dataclass, field
import typing as t
from aiogram.types import PhotoSize, Audio, Document, Video
from aiogram.enums import ContentType

@dataclass(slots=True, kw_only=True, frozen=True)
class MediaGroup:
    """Lightweight media group representation."""
    caption: t.Optional[str] = None
    photos: list[list[PhotoSize]] = field(default_factory=list)
    audio: list[Audio] = field(default_factory=list)
    documents: list[Document] = field(default_factory=list)
    video: list[Video] = field(default_factory=list)

    @property
    def content_type(self) -> str:
        """Return media group content type"""
        if self.photos:
            return ContentType.PHOTO
        if self.documents:
            return ContentType.DOCUMENT
        if self.audio:
            return ContentType.AUDIO
        if self.video:
            return ContentType.VIDEO
        return ContentType.UNKNOWN

Usage

Registering the Observer

import aiogram
from aiogram_mediagroup_handle import MediaGroupObserver

dp = aiogram.Dispatcher(...)
observer = MediaGroupObserver()
observer.register(dp)

Handling Media Groups

from aiogram import Router
from aiogram.types import Message
from aiogram.fsm.context import FSMContext
from aiogram_mediagroup_handle import MediaGroupFilter, MediaGroup

router = Router()

@router.message(MediaGroupFilter())
async def media_group_handler(message: Message, state: FSMContext):
    data = await state.get_data()
    media_data: MediaGroup = data[message.media_group_id]
    # Process the media group data here

Installation

pip install aiogram-mediagroup-handle

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiogram_mediagroup_handle-0.1.2.tar.gz (40.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiogram_mediagroup_handle-0.1.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_mediagroup_handle-0.1.2.tar.gz.

File metadata

File hashes

Hashes for aiogram_mediagroup_handle-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e3609ad71dbcd2439bde00772b70056cda73857305a6a5a2a7c07e023fb6758e
MD5 c9b77e9a2d696eec63de0a223eee145f
BLAKE2b-256 d64609b4d6f2a897e9a439c17161b5b1f658b25ceb26a828fc2c683f2ca21b34

See more details on using hashes here.

File details

Details for the file aiogram_mediagroup_handle-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for aiogram_mediagroup_handle-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 44feb2959afa5b0a51b96166ffd5a2cff9183cb74fadee757d71ff0e275dd53c
MD5 d21d06860d75f09a8cf79982dafe5155
BLAKE2b-256 5ee9a2ca5d8a3723effed9befbe55cf9104a953ec0853277244d191011188976

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page