@@ -22,41 +22,47 @@ startup and shutdown events, you have to manually define your executor.
2222For example:
2323
2424``` python
25- from aiogram import Bot, Dispatcher, executor
26- # Your taskiq broker
27- from your_project.tkq import broker
25+ import asyncio
26+ import logging
27+ import sys
2828
29+ from aiogram import Bot, Dispatcher
2930
30- bot = Bot(token = " API_TOKEN" )
31+ # Your taskiq broker
32+ from your_project.tkq import broker
3133
32- dispatcher = Dispatcher(bot)
34+ dp = Dispatcher()
35+ bot = Bot(token = " TOKEN" )
3336
3437
35- async def setup_taskiq (_ : Dispatcher):
38+ @dp.startup ()
39+ async def setup_taskiq (bot : Bot, * _args , ** _kwargs ):
3640 # Here we check if it's a clien-side,
3741 # Becuase otherwise you're going to
3842 # create infinite loop of startup events.
3943 if not broker.is_worker_process:
40- print (" Setting up taskiq" )
44+ logging.info (" Setting up taskiq" )
4145 await broker.startup()
4246
4347
44- async def shutdown_taskiq (_ : Dispatcher):
48+ @dp.shutdown ()
49+ async def shutdown_taskiq (bot : Bot, * _args , ** _kwargs ):
4550 if not broker.is_worker_process:
46- print (" Shutting down taskiq" )
51+ logging.info (" Shutting down taskiq" )
4752 await broker.shutdown()
4853
49- # Here we defined our executor.
50- bot_executor = executor.Executor( dispatcher = dispatcher)
51- bot_executor.on_startup([setup_taskiq] )
52- bot_executor.on_shutdown([shutdown_taskiq])
54+
55+ async def main ():
56+ await dp.start_polling(bot )
57+
5358
5459if __name__ == " __main__" :
55- bot_executor.start_polling()
60+ logging.basicConfig(level = logging.INFO , stream = sys.stdout)
61+ asyncio.run(main())
5662
5763```
5864
59- The only thing that left is to add one line to your broker definition.
65+ The only thing that left is to add few lines to your broker definition.
6066
6167
6268``` python
@@ -69,39 +75,53 @@ broker = MyBroker()
6975# This line is going to initialize everything.
7076taskiq_aiogram.init(
7177 broker,
72- # Here we define path to your executor.
73- # This format is similar to uvicorn or gunicorn.
74- " your_project.__main__:bot_executor" ,
75- pooling = True ,
76- webhook = False ,
78+ " your_project.__main__:dp" ,
79+ " your_project.__main__:bot" ,
7780)
7881```
7982
80- Aiogram defines startup events for pooling and webhooks separately. So you need to
81- explicitly specify which mode suites you. BTW, by default aiogram adds handler events to
82- both, so setting only pooling to True should be enought for almost any case.
83-
8483That's it.
8584
86- Let's create some tasks!
85+ Let's create some tasks! I created task in a separate module,
86+ named ` tasks.py ` .
8787
8888``` python
89- # Sometimes python imports wrong task names.
90- # If that happens, please set a task_name explicitly.
89+ from aiogram import Bot
90+ from your_project.tkq import broker
91+
9192@broker.task (task_name = " my_task" )
9293async def my_task (chat_id : int , bot : Bot = TaskiqDepends()) -> None :
9394 print (" I'm a task" )
9495 await asyncio.sleep(4 )
9596 await bot.send_message(chat_id, " task completed" )
9697
98+ ```
99+
100+ Now let's call our new task somewhere in bot commands.
97101
98- @dispatcher.message_handler (commands = [" task" ])
99- async def send_task (message : types.Message):
100- await message.reply(" Sending a task" )
102+ ``` python
103+ from aiogram import types
104+ from aiogram.filters import Command
105+
106+ from tasks import my_task
107+
108+
109+ @dp.message (Command(" task" ))
110+ async def message (message : types.Message):
101111 await my_task.kiq(message.chat.id)
102112
103113```
104114
105- And it works!
115+ To start the worker, please type:
116+
117+ ```
118+ taskiq worker your_project.tkq:broker --fs-discover
119+ ```
120+
121+ We use ` --fs-discover ` to find all tasks.py modules recursively
122+ and import all tasks into broker.
123+
124+
125+ Now we can fire the task and see everything in action.
106126
107127![ Showcase.jpg] ( https://raw.githubusercontent.com/taskiq-python/taskiq-aiogram/master/imgs/showcase.jpg )
0 commit comments