Updating from another chat or background¶
There are several cases when you do not have proper dialog_manager instance:
background tasks
http/broker handlers
handlers for another user/chat
In all these cases you should use so called “background manager” which can be obtained from BgManagerFactory or another dialog_manager instance using .bg() method.
Note, that you cannot use normal dialog_manager outside of aiogram dialog handler.
Using BgManager¶
Background manager can be used to asynchronously update dialog stack. All these actions are handled in background and you have not access to current state or dialog data.
await bg_manager.start(state)
await bg_manager.done()
await bg_manager.switch_to(state)
await bg_manager.update(data)
Or you can enter a context to use normal dialog manager accessing all necessary data:
with bg_manager.fg() as dialog_manager:
dialog_manager.dialog_data["key"] = value
dialog_manager.update()
Obtaining BgManagerFactory¶
In aiogram handlers:
async def handler(update: Any, dialog_bg_factory: BgManagerFactory) -> None: ...
In middleware. Works if the middleware is registered after setup_dialogs. In 99% of cases, you won’t need this.
async def __call__(self, handler, event, data):
dialog_bg_factory = data[BG_FACTORY_KEY]
As a result of the setup_dialogs function:
dialog_bg_factory = setup_dialogs(dispatcher)
In aiogram dialog handlers:
async def handler(update: Any, widget: Widget, manager: DialogManager) -> None:
dialog_bg_factory = manager.middleware_data["dialog_bg_factory"]
Getting a BgManager¶
Using DialogManager (another background manager will work as well):
bg_manager = dialog_manager.bg(
user_id=other_user_id,
chat_id=other_chat_id,
)
Using BgManagerFactory
bg_manager = dialog_manager.bg(
bot,
user_id=other_user_id,
chat_id=other_chat_id,
)
Dealing with middlewares¶
Background manager sends custom events using aiogram dispatcher, so you have to your middlewares won’t be triggered by default. To fix it register you middleware for dialog events:
from aiogram_dialog import DIALOG_EVENT_NAME
dispatcher.observers[DIALOG_EVENT_NAME].middleware(...)
Classes and signatures¶
- class aiogram_dialog.api.protocols.BaseDialogManager(*args, **kwargs)¶
- abstractmethod bg(user_id=None, chat_id=None, stack_id=None, thread_id=UnsetId.UNSET, business_connection_id=UnsetId.UNSET, load=False)¶
Get background manager for specified chat
- Parameters:
user_id (int | None)
chat_id (int | None)
stack_id (str | None)
thread_id (int | UnsetId | None)
business_connection_id (str | UnsetId | None)
load (bool)
- Return type:
- abstractmethod async done(result=None, show_mode=None)¶
Close current dialog and show underlying dialog in the stack
- Parameters:
result (Any)
show_mode (ShowMode | None)
- Return type:
None
- abstractmethod fg()¶
Get full-featured dialog manager
- Return type:
AbstractAsyncContextManager[DialogManager]
- abstractmethod async start(state, data=None, mode=StartMode.NORMAL, show_mode=None, access_settings=None)¶
Add new dialog to the stack and show it
- Parameters:
state (State)
data (dict | list | int | str | float | None)
mode (StartMode)
show_mode (ShowMode | None)
access_settings (AccessSettings | None)
- Return type:
None
- class aiogram_dialog.api.protocols.BgManagerFactory(*args, **kwargs)¶
- abstractmethod bg(bot, user_id, chat_id, stack_id=None, thread_id=None, business_connection_id=None, load=False)¶
Get background manager for specified chat
- Parameters:
bot (Bot)
user_id (int)
chat_id (int)
stack_id (str | None)
thread_id (int | None)
business_connection_id (str | None)
load (bool)
- Return type: