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 find in next pages:
- class aiogram.dispatcher.dispatcher.Dispatcher(*, storage: BaseStorage | None = None, fsm_strategy: FSMStrategy = FSMStrategy.USER_IN_CHAT, events_isolation: BaseEventIsolation | None = None, disable_fsm: bool = False, name: str | None = None, **kwargs: Any)[source]¶
Root router
- __init__(*, storage: BaseStorage | None = None, fsm_strategy: FSMStrategy = FSMStrategy.USER_IN_CHAT, events_isolation: BaseEventIsolation | None = None, disable_fsm: bool = False, name: str | None = 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: List[str] | _SentinelObject | None = sentinel.UNSET, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any) None [source]¶
Run many bots with polling
- Parameters:
bots – Bot instances (one or more)
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: List[str] | _SentinelObject | None = sentinel.UNSET, handle_signals: bool = True, close_bot_session: bool = True, **kwargs: Any) None [source]¶
Polling runner
- Parameters:
bots – Bot instances (one or more)
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 By default, all used update types are enabled (resolved from handlers)
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 feed_update()
method:
from aiogram import Bot, Dispatcher
async def update_handler(update: Update, bot: Bot, dispatcher: Dispatcher):
result = await dp.feed_update(bot, update)
Also you can feed raw update (dictionary) object to the dispatcher by feed_raw_update()
method:
from aiogram import Bot, Dispatcher
async def update_handler(raw_update: dict[str, Any], bot: Bot, dispatcher: Dispatcher):
result = await dp.feed_raw_update(bot, raw_update)