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 FSMStorage from the aiogram dispatcher to collect and store media files in FSM data using media_group_id. It comes with some utilities:

  • MediaGroupFilter – A filter to detect media groups
  • MediaGroupMiddleware – Middleware for handling media groups
  • MediaGroup – A lightweight dataclass accessible in aiogram handlers

MediaGroup Dataclass

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

@dataclass
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:
        """Returns the content type of the media group."""
        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.0.tar.gz (39.7 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.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for aiogram_mediagroup_handle-0.1.0.tar.gz
Algorithm Hash digest
SHA256 336e13a333bd313fbd90e4267b08914e022f89fc31581452bbac4257df0e2d47
MD5 1d7d6427600e031c2c1b5306004d68ca
BLAKE2b-256 5b14b74e82b867962be753fdb03818af11d796f3e81a060defcc9fca4c3d1170

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiogram_mediagroup_handle-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 03a51c782344705dae7f80247d4f8a830c14c81aeeec34b87ff29c553b5e9d47
MD5 228713562e3791b888d31cfebe281c86
BLAKE2b-256 b85b11804a09d13750090dc27f643521a706aa0bc96d47999accfa50f2dc3b4b

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