|
| 1 | +# Taskiq + Aiogram |
| 2 | + |
| 3 | +[Taskiq-Aiogram](https://github.com/taskiq-python/taskiq-aiogram) is a nice integration with one of the best telegram bot libraries - [aiogram](https://docs.aiogram.dev/en/latest/). |
| 4 | + |
| 5 | +This integration allows you to easily send delayed messages or run intensive functions without blocking the message handing. |
| 6 | + |
| 7 | +This integration adds three main dependencies which you can use in your taskiq functions: |
| 8 | + |
| 9 | +- `aiogram.Bot` - the bot instance that you can use to send messages or perform other actions. If multiple bots listen to the same dispatcher, this dependency will be resolved to the latest bot passed in the `taskiq_aiogram.init` function. |
| 10 | +- `aiogram.Dispatcher` - current dispatcher instance. |
| 11 | +- `List[aiogram.Bot]` - list of all bots that were passed to the `taskiq_aiogram.init` function. |
| 12 | + |
| 13 | +To enable the integration, please install the `taskiq-aiogram` library: |
| 14 | + |
| 15 | +```bash:no-line-numbers |
| 16 | +pip install "taskiq-aiogram" |
| 17 | +``` |
| 18 | + |
| 19 | +After the installation is complete, add an initialization function call to your broker's main file so it becomes something like this: |
| 20 | + |
| 21 | +```python title="tkq.py" |
| 22 | +import asyncio |
| 23 | + |
| 24 | +import taskiq_aiogram |
| 25 | +from aiogram import Bot |
| 26 | +from taskiq import TaskiqDepends |
| 27 | +from taskiq_redis import ListQueueBroker |
| 28 | + |
| 29 | +broker = ListQueueBroker("redis://localhost") |
| 30 | + |
| 31 | +# This line is going to initialize everything. |
| 32 | +taskiq_aiogram.init( |
| 33 | + broker, |
| 34 | + # This is path to the dispatcher. |
| 35 | + "bot:dp", |
| 36 | + # This is path to the bot instance. |
| 37 | + "bot:bot", |
| 38 | + # You can specify more bots here. |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +@broker.task(task_name="my_task") |
| 43 | +async def my_task(chat_id: int, bot: Bot = TaskiqDepends()) -> None: |
| 44 | + print("I'm a task") |
| 45 | + await asyncio.sleep(4) |
| 46 | + await bot.send_message(chat_id, "task completed") |
| 47 | + |
| 48 | +``` |
| 49 | + |
| 50 | +Let's see how to use this integration. |
| 51 | + |
| 52 | +```python title="bot.py" |
| 53 | +import asyncio |
| 54 | +import logging |
| 55 | +import sys |
| 56 | + |
| 57 | +from aiogram import Bot, Dispatcher, types |
| 58 | +from aiogram.filters import Command |
| 59 | + |
| 60 | +from tkq import broker, my_task |
| 61 | + |
| 62 | +dp = Dispatcher() |
| 63 | +bot = Bot(token="TOKEN") |
| 64 | + |
| 65 | + |
| 66 | +# Taskiq calls this function when starting the worker. |
| 67 | +@dp.startup() |
| 68 | +async def setup_taskiq(bot: Bot, *_args, **_kwargs): |
| 69 | + # Here we check if it's a clien-side, |
| 70 | + # Becuase otherwise you're going to |
| 71 | + # create infinite loop of startup events. |
| 72 | + if not broker.is_worker_process: |
| 73 | + logging.info("Setting up taskiq") |
| 74 | + await broker.startup() |
| 75 | + |
| 76 | + |
| 77 | +# Taskiq calls this function when shutting down the worker. |
| 78 | +@dp.shutdown() |
| 79 | +async def shutdown_taskiq(bot: Bot, *_args, **_kwargs): |
| 80 | + if not broker.is_worker_process: |
| 81 | + logging.info("Shutting down taskiq") |
| 82 | + await broker.shutdown() |
| 83 | + |
| 84 | + |
| 85 | +## Simple command to handle |
| 86 | +@dp.message(Command("task")) |
| 87 | +async def message(message: types.Message): |
| 88 | + await my_task.kiq(message.chat.id) |
| 89 | + |
| 90 | + |
| 91 | +## Main function that starts the bot. |
| 92 | +async def main(): |
| 93 | + await dp.start_polling(bot) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + logging.basicConfig(level=logging.INFO, stream=sys.stdout) |
| 98 | + asyncio.run(main()) |
| 99 | + |
| 100 | +``` |
| 101 | + |
| 102 | +That's it. Now you can easily call tasks from your bots and access bots from within your tasks. |
0 commit comments