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

MediaGroupType = list[InputMediaAudio | InputMediaDocument | InputMediaPhoto | InputMediaVideo]
MediaType = list[PhotoSize] | Audio | Document | Video

@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)
    
    def get_media(self) -> t.Iterator[MediaType]:
        """Return media group iterator"""
        ...
    
    def as_input_media(self) -> MediaGroupType:
        """Return media group for sending via aiogram.Bot"""
        ...
        
    @property
    def content_type(self) -> ContentType:
        """Return media group content type"""
        ...

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...
    
    # f.e. answer with media
    await message.answer_media_group(media=media_data.as_input_media())

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.3.tar.gz (45.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.3-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for aiogram_mediagroup_handle-0.1.3.tar.gz
Algorithm Hash digest
SHA256 0edc83010bc783c20bc411cc1733b6be53c43da347b204428939822fc6dd507a
MD5 f1946a2002a58cb50145ce2c02eab437
BLAKE2b-256 a4029c4621de0aea8366fa40363ca9a2b582456c4e773e42c5118296c73e4966

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiogram_mediagroup_handle-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e4b9a010c5b9a7378397d9a7366c74b791a968e986e8a0f76714798919dccc1c
MD5 cba7739ad5df76f6b9a511c0d67473ac
BLAKE2b-256 68493e791f50ebb704f1fa147abd57a78ea3e9b5c95e700c7b5f35fb5384164a

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