Dispatcher#

Dispatcher is root Router and in code Dispatcher can be used directly for routing updates or attach another routers into dispatcher.

Here is only listed base information about Dispatcher. All about writing handlers, filters and etc. you can found in next pages:

class aiogram.dispatcher.dispatcher.Dispatcher(*, storage: Optional[BaseStorage] = None, fsm_strategy: FSMStrategy = FSMStrategy.USER_IN_CHAT, events_isolation: Optional[BaseEventIsolation] = None, disable_fsm: bool = False, name: Optional[str] = None, **kwargs: Any)[source]#

Root router

__init__(*, storage: Optional[BaseStorage] = None, fsm_strategy: FSMStrategy = FSMStrategy.USER_IN_CHAT, events_isolation: Optional[BaseEventIsolation] = None, disable_fsm: bool = False, name: Optional[str] = None, **kwargs: Any) None[source]#

Root router

Parameters:
  • storage – Storage for FSM

  • fsm_strategy – FSM strategy

  • events_isolation – Events isolation

  • disable_fsm – Disable FSM, note that if you disable FSM then you should not use storage and events isolation

  • kwargs – Other arguments, will be passed as keyword arguments to handlers

async feed_raw_update(bot: Bot, update: Dict[str, Any], **kwargs: Any) Any[source]#

Main entry point for incoming updates with automatic Dict->Update serializer

Parameters:
  • bot

  • update

  • kwargs

async feed_update(bot: Bot, update: Update, **kwargs: Any) Any[source]#

Main entry point for incoming updates Response of this method can be used as Webhook response

Parameters:
  • bot

  • update

run_polling(*bots: Bot, polling_timeout: int = 10, handle_as_tasks: bool = True, backoff_config: BackoffConfig = BackoffConfig(min_delay=1.0, max_delay=5.0, factor=1.3, jitter=0.1), allowed_updates: Optional[List[str]] = None, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any) None[source]#

Run many bots with polling

Parameters:
  • bots – Bot instances (one or mre)

  • polling_timeout – Long-polling wait time

  • handle_as_tasks – Run task for each event and no wait result

  • backoff_config – backoff-retry config

  • allowed_updates – List of the update types you want your bot to receive

  • handle_signals – handle signals (SIGINT/SIGTERM)

  • close_bot_session – close bot sessions on shutdown

  • kwargs – contextual data

Returns:

async start_polling(*bots: Bot, polling_timeout: int = 10, handle_as_tasks: bool = True, backoff_config: BackoffConfig = BackoffConfig(min_delay=1.0, max_delay=5.0, factor=1.3, jitter=0.1), allowed_updates: Optional[List[str]] = None, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any) None[source]#

Polling runner

Parameters:
  • bots – Bot instances (one or mre)

  • polling_timeout – Long-polling wait time

  • handle_as_tasks – Run task for each event and no wait result

  • backoff_config – backoff-retry config

  • allowed_updates – List of the update types you want your bot to receive

  • handle_signals – handle signals (SIGINT/SIGTERM)

  • close_bot_session – close bot sessions on shutdown

  • kwargs – contextual data

Returns:

Simple usage#

Example:

dp = Dispatcher()

@dp.message()
async def message_handler(message: types.Message) -> None:
    await SendMessage(chat_id=message.from_user.id, text=message.text)

Including routers

Example:

dp = Dispatcher()
router1 = Router()
dp.include_router(router1)

Handling updates#

All updates can be propagated to the dispatcher by Dispatcher.feed_update(bot=..., update=...) method:

bot = Bot(...)
dp = Dispathcher()

...

result = await dp.feed_update(bot=bot, update=incoming_update)