from aiogram import Router
from aiogram.types import Message

my_router = Router(name=__name__)

async def message_handler(message: Message) -> Any:
    await message.answer('Hello from my router!')
class aiogram.dispatcher.router.Router(*, name: str | None = None)[source]#

Bases: object

Router can route update, and it nested update types like messages, callback query, polls and all other event types.

Event handlers can be registered in observer by two ways:

  • By observer method - router.<event_type>.register(handler, <filters, ...>)

  • By decorator - @router.<event_type>(<filters, ...>)

__init__(*, name: str | None = None) None[source]#

name – Optional router name, can be useful for debugging

include_router(router: Router) Router[source]#

Attach another router.




include_routers(*routers: Router) None[source]#

Attach multiple routers.




resolve_used_update_types(skip_events: Set[str] | None = None) List[str][source]#

Resolve registered event names

Is useful for getting updates only for registered event types.


skip_events – skip specified event names


set of registered names

Event observers#


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.

Here is the list of available observers and examples of how to register handlers

In these examples only decorator-style registering handlers are used, but if you don’t like @decorators just use <event type>.register(...) method instead.



Be attentive with filtering this event

You should expect that this event can be with different sets of attributes in different cases

(For example text, sticker and document are always of different content types of message)

Recommended way to check field availability before usage, for example via magic filter: F.text to handle text, F.sticker to handle stickers only and etc.

async def message_handler(message: types.Message) -> Any: pass

Edited message#

async def edited_message_handler(edited_message: types.Message) -> Any: pass

Channel post#

async def channel_post_handler(channel_post: types.Message) -> Any: pass

Edited channel post#

async def edited_channel_post_handler(edited_channel_post: types.Message) -> Any: pass

Inline query#

async def inline_query_handler(inline_query: types.InlineQuery) -> Any: pass

Chosen inline query#

async def chosen_inline_result_handler(chosen_inline_result: types.ChosenInlineResult) -> Any: pass

Callback query#

async def callback_query_handler(callback_query: types.CallbackQuery) -> Any: pass

Shipping query#

async def shipping_query_handler(shipping_query: types.ShippingQuery) -> Any: pass

Pre checkout query#

async def pre_checkout_query_handler(pre_checkout_query: types.PreCheckoutQuery) -> Any: pass


async def poll_handler(poll: types.Poll) -> Any: pass

Poll answer#

async def poll_answer_handler(poll_answer: types.PollAnswer) -> Any: pass

My chat member#

async def my_chat_member_handler(my_chat_member: types.ChatMemberUpdated) -> Any: pass

Chat member#

async def chat_member_handler(chat_member: types.ChatMemberUpdated) -> Any: pass

Chat join request#

async def chat_join_request_handler(chat_join_request: types.ChatJoinRequest) -> Any: pass

Message reaction#

async def message_reaction_handler(message_reaction: types.MessageReactionUpdated) -> Any: pass

Message reaction count#

async def message_reaction_count_handler(message_reaction_count: types.MessageReactionCountUpdated) -> Any: pass

Chat boost#

async def chat_boost_handler(chat_boost: types.ChatBoostUpdated) -> Any: pass

Remove chat boost#

async def removed_chat_boost_handler(removed_chat_boost: types.ChatBoostRemoved) -> Any: pass


async def error_handler(exception: types.ErrorEvent) -> Any: pass

Is useful for handling errors from other handlers, error event described here

Nested routers#


Routers by the way can be nested to an another routers with some limitations:

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)




router2 = Router()

@router2.message() …



from module_2 import router2

router1 = Router() router1.include_router(router2)


async def message_handler(update: types.Update) -> Any: pass


The only root Router (Dispatcher) can handle this type of event.


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.

How it works?#

For example, dispatcher has 2 routers, the last router also has one nested router:

Nested routers example

In this case update propagation flow will have form:

Nested routers example