Skip to content

Instantly share code, notes, and snippets.

@homus32
Created September 9, 2020 12:52
Show Gist options
  • Save homus32/aaf8e948c23dc024381d4ba042ae0695 to your computer and use it in GitHub Desktop.
Save homus32/aaf8e948c23dc024381d4ba042ae0695 to your computer and use it in GitHub Desktop.
костыль для aiogram. нужен для получения вложений сообщения
from aiogram import Bot, Dispatcher, executor, types
from aiogram.types.message import ContentTypes
import asyncio
class FormulateAttachment:
users = {}
def __init__(self, user_id):
cls = self.__class__
self._user_id = user_id
if cls.users.get(user_id) is None:
cls.users[user_id] = {}
self._new = True
self._args = None
self._function = None
else:
self._new = False
self._args = cls.users[user_id].get("args")
self._function = cls.users[user_id].get("function")
self.stop_timer()
@property
def args(self):
return self._args
@args.setter
def args(self, args_: list):
cls = self.__class__
self._args = args_
cls.users[self._user_id]["args"] = args_
@property
def function(self):
return self._function
@function.setter
def function(self, func: callable):
cls = self.__class__
self._function = func
cls.users[self._user_id]["function"] = func
@property
def is_new(self):
return self._new
async def _timer(self):
await asyncio.sleep(0.1)
await self._function(*self._args)
del self.__class__.users[self._user_id]
def start_timer(self):
cls = self.__class__
cls.users[self._user_id]["task"] = tg_bot.loop.create_task(self._timer())
def stop_timer(self):
cls = self.__class__
cls.users[self._user_id]["task"].cancel()
async def send_media(message: types.Message, files: list):
print("ALL OKAY!")
print(files)
media_content_types = ContentTypes.PHOTO | ContentTypes.VIDEO | ContentTypes.DOCUMENT | ContentTypes.ANIMATION
@tg_dp.message_handler(content_types=media_content_types)
async def send_media_to_vk(message: types.Message):
FA = FormulateAttachment(message.from_user.id)
if FA.is_new is True:
FA.args = [message, []]
FA.function = send_media
FA.args[1].append(message.message_id)
FA.start_timer()
@homus32
Copy link
Author

homus32 commented Sep 9, 2020

Если кто то это будет юзать то не забудьте поменять строку 57

cls.users[self._user_id]["task"] = tg_bot.loop.create_task(self._timer())

на

cls.users[self._user_id]["task"] = Bot.loop.create_task(self._timer())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment