Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
import asyncio from typing import * from aiogram import BaseMiddleware from aiogram.exceptions import TelegramBadRequest from aiogram.types import Message, CallbackQuery class MediaMiddleware(BaseMiddleware): def __init__(self, latency: Union[int, float] = 0.6): self.medias = {} self.latency = latency super(MediaMiddleware, self).__init__() async def __call__( self, handler: Callable[[Union[Message, CallbackQuery], Dict[str, Any]], Awaitable[Any]], event: Union[Message, CallbackQuery], data: Dict[str, Any] ) -> Any: if isinstance(event, Message) and event.media_group_id: try: self.medias[event.media_group_id].append(event) last_message_flag = False try: await event.bot.edit_message_text( text=" ", chat_id=event.from_user.id, message_id=event.message_id + 1 ) except TelegramBadRequest as exc: if (exc.message == "Bad Request: message must be non-empty" or exc.message == "Bad Request: message to edit not found"): last_message_flag = True if last_message_flag: data["media_events"] = self.medias.pop(event.media_group_id) return await handler(event, data) return except KeyError: self.medias[event.media_group_id] = [event] return return await handler(event, data)