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.1.tar.gz (40.3 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.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for aiogram_mediagroup_handle-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2b919a301bd63454129bdf6c9247a46461358cf3c234bbd5ae61f99bdb1ff605
MD5 8ad750942d718f41764d935615b03923
BLAKE2b-256 c31f730ab488905a127d4ca0e127377eb3f22a218d14fc9c25e562801468647e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiogram_mediagroup_handle-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 04533d083a6e1ca746f4406d8da480ca8cd0a8e5a4416fcb6e042ef866b7f13e
MD5 272e124371b4bfa12dea8fe13b7daf68
BLAKE2b-256 6bb100a08d60093eddea320e9101ba17c1c849cf3b216b95fb9cb6e20d5d6a92

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