2021-01-26 21:20:52 +02:00
######
Router
######
.. autoclass :: aiogram.dispatcher.router.Router
2023-07-29 22:36:12 +03:00
:members: __init__, include_router, include_routers, resolve_used_update_types
2021-01-26 21:20:52 +02:00
:show-inheritance:
2023-07-29 22:36:12 +03:00
.. _Event observers:
2021-01-26 21:20:52 +02:00
Event observers
===============
.. warning ::
2022-01-26 20:30:27 +02:00
All handlers always should be asynchronous.
The name of the handler function is not important. The event argument name is also not important but it is recommended to not overlap the name with contextual data in due to function can not accept two arguments with the same name.
2021-01-26 21:20:52 +02:00
2022-01-26 20:30:27 +02:00
Here is the list of available observers and examples of how to register handlers
2021-01-26 21:20:52 +02:00
2022-01-26 20:30:27 +02:00
In these examples only decorator-style registering handlers are used, but if you don't like @decorators just use :obj: `<event type>.register(...)` method instead.
2021-01-26 21:20:52 +02:00
Message
-------
2021-08-20 02:39:03 +03:00
.. attention ::
Be attentive with filtering this event
2021-09-07 00:32:43 +03:00
2022-01-26 20:30:27 +02:00
You should expect that this event can be with different sets of attributes in different cases
2021-08-20 02:39:03 +03:00
2022-01-26 20:30:27 +02:00
(For example text, sticker and document are always of different content types of message)
2021-08-20 02:39:03 +03:00
2022-10-02 00:04:31 +03:00
Recommended way to check field availability before usage, for example via :ref: `magic filter <magic-filters>` :
:code: `F.text` to handle text, :code: `F.sticker` to handle stickers only and etc.
2021-08-20 02:39:03 +03:00
2021-01-26 21:20:52 +02:00
.. code-block :: python
@router.message()
async def message_handler(message: types.Message) -> Any: pass
Edited message
--------------
.. code-block :: python
@router.edited_message()
async def edited_message_handler(edited_message: types.Message) -> Any: pass
Channel post
------------
.. code-block :: python
@router.channel_post()
async def channel_post_handler(channel_post: types.Message) -> Any: pass
Edited channel post
-------------------
.. code-block :: python
@router.edited_channel_post()
async def edited_channel_post_handler(edited_channel_post: types.Message) -> Any: pass
Inline query
------------
.. code-block :: python
@router.inline_query()
2022-10-11 20:04:28 +03:00
async def inline_query_handler(inline_query: types.InlineQuery) -> Any: pass
2021-01-26 21:20:52 +02:00
Chosen inline query
-------------------
.. code-block :: python
@router.chosen_inline_result()
async def chosen_inline_result_handler(chosen_inline_result: types.ChosenInlineResult) -> Any: pass
Callback query
--------------
.. code-block :: python
@router.callback_query()
async def callback_query_handler(callback_query: types.CallbackQuery) -> Any: pass
Shipping query
--------------
.. code-block :: python
@router.shipping_query()
async def shipping_query_handler(shipping_query: types.ShippingQuery) -> Any: pass
Pre checkout query
------------------
.. code-block :: python
@router.pre_checkout_query()
async def pre_checkout_query_handler(pre_checkout_query: types.PreCheckoutQuery) -> Any: pass
Poll
----
.. code-block :: python
@router.poll()
async def poll_handler(poll: types.Poll) -> Any: pass
Poll answer
-----------
.. code-block :: python
@router.poll_answer()
async def poll_answer_handler(poll_answer: types.PollAnswer) -> Any: pass
Errors
------
.. code-block :: python
@router.errors()
2022-10-02 00:04:31 +03:00
async def error_handler(exception: ErrorEvent) -> Any: pass
2021-01-26 21:20:52 +02:00
2023-07-29 22:36:12 +03:00
Is useful for handling errors from other handlers, error event described :ref: `here <error-event>`
2021-01-26 21:20:52 +02:00
2023-07-29 22:36:12 +03:00
.. _Nested routers:
2021-01-26 21:20:52 +02:00
Nested routers
==============
.. warning ::
Routers by the way can be nested to an another routers with some limitations:
2022-10-02 00:04:31 +03:00
1. Router **CAN NOT** include itself
1. Routers **CAN NOT** be used for circular including (router 1 include router 2, router 2 include router 3, router 3 include router 1)
2021-01-26 21:20:52 +02:00
Example:
.. code-block :: python
2023-07-29 22:36:12 +03:00
:caption: module_1.py
:name: module_1
2021-01-26 21:20:52 +02:00
router2 = Router()
@router2.message()
...
.. code-block :: python
2022-10-02 00:04:31 +03:00
:caption: module_2.py
2023-07-29 22:36:12 +03:00
:name: module_2
2021-01-26 21:20:52 +02:00
from module_2 import router2
router1 = Router()
router1.include_router(router2)
2023-07-29 22:36:12 +03:00
Update
------
.. code-block :: python
@dispatcher.update()
async def message_handler(update: types.Update) -> Any: pass
.. warning ::
The only root Router (Dispatcher) can handle this type of event.
.. note ::
Dispatcher already has default handler for this event type, so you can use it for handling all updates that are not handled by any other handlers.
2021-01-26 21:20:52 +02:00
How it works?
-------------
2022-01-26 20:30:27 +02:00
For example, dispatcher has 2 routers, the last router also has one nested router:
2021-01-26 21:20:52 +02:00
.. image :: ../_static/nested_routers_example.png
:alt: Nested routers example
In this case update propagation flow will have form:
.. image :: ../_static/update_propagation_flow.png
:alt: Nested routers example