Dispatcher class¶
-
class
aiogram.dispatcher.Dispatcher(bot, loop=None, storage: typing.Union[aiogram.dispatcher.storage.BaseStorage, NoneType] = None)[исходный код]¶ - Базовые классы:
objectSimple Updates dispatcher
It will be can process incoming updates, messages, edited messages, channel posts, edited channels posts, inline query, chosen inline result, callback query, shipping query, pre-checkout query. Provide next step handler and etc.
-
async_task(func)[исходный код]¶ Execute handler as task and return None. Use that decorator for slow handlers (with timeouts)
@dp.message_handler(commands=['command']) @dp.async_task async def cmd_with_timeout(message: types.Message): await asyncio.sleep(120) return SendMessage(message.chat.id, 'KABOOM').reply(message)
Параметры: func – Результат:
-
callback_query_handler(*, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Add callback query handler
Example:
@dp.callback_query_handler(func=lambda callback_query: True) async def handler(callback_query: types.CallbackQuery)
Параметры: - func – custom any callable object
- state –
- custom_filters –
- kwargs –
-
channel_post_handler(*, commands=None, regexp=None, content_types=None, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Register channels posts handler
Параметры: - commands – list of commands
- regexp – REGEXP
- content_types – List of content types.
- func – custom any callable object
- state –
- custom_filters – list of custom filters
- kwargs –
Результат: decorated function
-
chosen_inline_handler(*, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Register chosen inline handler
Example:
@dp.chosen_inline_handler(func=lambda chosen_inline_query: True) async def handler(chosen_inline_query: types.ChosenInlineResult)
Параметры: - func – custom any callable object
- state –
- custom_filters –
- kwargs –
Результат:
-
edited_channel_post_handler(*, commands=None, regexp=None, content_types=None, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Register handler for edited channels posts
Параметры: - commands – list of commands
- regexp – REGEXP
- content_types – List of content types.
- func – custom any callable object
- custom_filters – list of custom filters
- state –
- kwargs –
Результат: decorated function
-
edited_message_handler(*, commands=None, regexp=None, content_types=None, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Analog of message_handler but only for edited messages
You can use combination of different handlers
@dp.message_handler() @dp.edited_message_handler() async def msg_handler(message: types.Message):
Параметры: - commands – list of commands
- regexp – REGEXP
- content_types – List of content types.
- func – custom any callable object
- state –
- custom_filters – list of custom filters
- kwargs –
Результат: decorated function
-
inline_handler(*, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Handle inline query
Example:
@dp.inline_handler(func=lambda inline_query: True) async def handler(inline_query: types.InlineQuery)
Параметры: - func – custom any callable object
- state –
- custom_filters – list of custom filters
- kwargs –
Результат: decorated function
-
message_handler(*, commands=None, regexp=None, content_types=None, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Decorator for messages handler
Examples:
Simple commands handler:
@dp.messages_handler(commands=['start', 'welcome', 'about']) async def cmd_handler(message: types.Message):
Filter messages by regular expression:
@dp.messages_handler(rexexp='^[a-z]+-[0-9]+') async def msg_handler(message: types.Message):
Filter by content type:
@dp.messages_handler(content_types=ContentType.PHOTO | ContentType.DOCUMENT) async def audio_handler(message: types.Message):
Filter by custom function:
@dp.messages_handler(func=lambda message: message.text and 'hello' in message.text.lower()) async def text_handler(message: types.Message):
Use multiple filters:
@dp.messages_handler(commands=['command'], content_types=ContentType.TEXT) async def text_handler(message: types.Message):
Register multiple filters set for one handler:
@dp.messages_handler(commands=['command']) @dp.messages_handler(func=lambda message: demojize(message.text) == ':new_moon_with_face:') async def text_handler(message: types.Message):
This handler will be called if the message starts with ‘/command’ OR is some emoji
By default content_type is
ContentType.TEXTПараметры: - commands – list of commands
- regexp – REGEXP
- content_types – List of content types.
- func – custom any callable object
- custom_filters – list of custom filters
- kwargs –
- state –
Результат: decorated function
-
pre_checkout_query_handler(*, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Add shipping query handler
Example:
@dp.shipping_query_handler(func=lambda shipping_query: True) async def handler(shipping_query: types.ShippingQuery)
Параметры: - func – custom any callable object
- state –
- custom_filters –
- kwargs –
-
process_update(update)[исходный код]¶ Process single update object
Параметры: update – Результат:
-
process_updates(updates)[исходный код]¶ Process list of updates
Параметры: updates – Результат:
-
register_callback_query_handler(callback, *, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Add callback query handler
Example:
@dp.callback_query_handler(func=lambda callback_query: True) async def handler(callback_query: types.CallbackQuery)
Параметры: - callback –
- func – custom any callable object
- state –
- custom_filters –
- kwargs –
-
register_channel_post_handler(callback, *, commands=None, regexp=None, content_types=None, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Register channels posts handler
Параметры: - callback –
- commands – list of commands
- regexp – REGEXP
- content_types – List of content types.
- func – custom any callable object
- state –
- custom_filters – list of custom filters
- kwargs –
Результат: decorated function
-
register_chosen_inline_handler(callback, *, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Register chosen inline handler
Example:
@dp.chosen_inline_handler(func=lambda chosen_inline_query: True) async def handler(chosen_inline_query: types.ChosenInlineResult)
Параметры: - callback –
- func – custom any callable object
- state –
- custom_filters –
- kwargs –
Результат:
-
register_edited_channel_post_handler(callback, *, commands=None, regexp=None, content_types=None, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Register handler for edited channels posts
Параметры: - callback –
- commands – list of commands
- regexp – REGEXP
- content_types – List of content types.
- func – custom any callable object
- state –
- custom_filters – list of custom filters
- kwargs –
Результат: decorated function
-
register_edited_message_handler(callback, *, commands=None, regexp=None, content_types=None, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Analog of message_handler but only for edited messages
Параметры: - callback –
- commands – list of commands
- regexp – REGEXP
- content_types – List of content types.
- func – custom any callable object
- state –
- custom_filters – list of custom filters
- kwargs –
Результат: decorated function
-
register_inline_handler(callback, *, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Handle inline query
Example:
@dp.inline_handler(func=lambda inline_query: True) async def handler(inline_query: types.InlineQuery)
Параметры: - callback –
- func – custom any callable object
- custom_filters – list of custom filters
- state –
- kwargs –
Результат: decorated function
-
register_message_handler(callback, *, commands=None, regexp=None, content_types=None, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ You can register messages handler by this method
# This handler works only is state is None (by default). dp.register_message_handler(cmd_start, commands=['start', 'about']) dp.register_message_handler(entry_point, commands=['setup']) # That handler works only if current state is "first_step" dp.register_message_handler(step_handler_1, state="first_step") # If you want to handle all states by one handler then use state="*". dp.register_message_handler(cancel_handler, commands=['cancel'], state="*") dp.register_message_handler(cancel_handler, func=lambda msg: msg.text.lower() == 'cancel', state="*")
Параметры: - callback –
- commands – list of commands
- regexp – REGEXP
- content_types – List of content types.
- func – custom any callable object
- custom_filters – list of custom filters
- kwargs –
- state –
Результат: decorated function
-
register_pre_checkout_query_handler(callback, *, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Add shipping query handler
Example:
@dp.shipping_query_handler(func=lambda shipping_query: True) async def handler(shipping_query: types.ShippingQuery)
Параметры: - callback –
- func – custom any callable object
- state –
- custom_filters –
- kwargs –
-
register_shipping_query_handler(callback, *, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Add shipping query handler
Example:
@dp.shipping_query_handler(func=lambda shipping_query: True) async def handler(shipping_query: types.ShippingQuery)
Параметры: - callback –
- func – custom any callable object
- state –
- custom_filters –
- kwargs –
-
shipping_query_handler(*, func=None, state=None, custom_filters=None, **kwargs)[исходный код]¶ Add shipping query handler
Example:
@dp.shipping_query_handler(func=lambda shipping_query: True) async def handler(shipping_query: types.ShippingQuery)
Параметры: - func – custom any callable object
- state –
- custom_filters –
- kwargs –
-
skip_updates()[исходный код]¶ You can skip old incoming updates from queue. This method is not recommended to use if you use payments or you bot has high-load.
Результат: count of skipped updates
-
start_pooling(timeout=20, relax=0.1, limit=None)[исходный код]¶ Start long-pooling
Параметры: - timeout –
- relax –
- limit –
Результат:
-
stop_pooling()[исходный код]¶ Break long-pooling process. :return:
-