From 9131ce4ac22c25d6f7146c7f14f10556eb4659a3 Mon Sep 17 00:00:00 2001 From: typhoon Date: Fri, 22 Apr 2022 16:45:16 +1000 Subject: [PATCH] update --- examples/echo_class_based_bot_asyncio.py | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 examples/echo_class_based_bot_asyncio.py diff --git a/examples/echo_class_based_bot_asyncio.py b/examples/echo_class_based_bot_asyncio.py new file mode 100644 index 00000000..e478a36d --- /dev/null +++ b/examples/echo_class_based_bot_asyncio.py @@ -0,0 +1,31 @@ +from aiogram import Bot, Dispatcher, types +import logging +import asyncio + + +API_TOKEN = "" +logging.basicConfig(level=logging.INFO) + +class AsyncioBot: + + # Initialize bot and dispatcher + def __init__(self) -> None: + self.bot = Bot(token=API_TOKEN) + self.dp = Dispatcher(self.bot) + + self.dp.register_message_handler(self.on_message) + + async def on_message(self, message: types.Message): + print(message.get_command()) + await message.answer(f"Asnwer: {message.text}") + + async def run(self): + asyncio.create_task(self.dp.start_polling()) + +if __name__ == '__main__': + bot = AsyncioBot() + loop = asyncio.get_event_loop() + loop.create_task(bot.run()) + + loop.run_forever() +