| ⬅️ Back | 🏠 Docs Root |
This module defines the ContainerMiddleware, an Aiogram middleware responsible for injecting the BotContainer instance into the data dictionary of incoming updates. This allows handlers to easily access all application services and dependencies managed by the container.
ContainerMiddleware ClassThe ContainerMiddleware facilitates Dependency Injection (DI) by making the central BotContainer available to any handler that processes an update.
__init__)def __init__(self, container: BotContainer):
Initializes the ContainerMiddleware with an instance of BotContainer.
container (BotContainer): The main Dependency Injection container of the bot application.__call__ Methodasync def __call__(
self,
handler: Callable[[TelegramObject, dict[str, Any]], Awaitable[Any]],
event: TelegramObject,
data: dict[str, Any],
) -> Any:
This asynchronous method is the entry point for the middleware. It intercepts incoming TelegramObject events before they are processed by the main handlers.
handler (Callable): The next handler in the middleware chain or the final message handler.event (TelegramObject): The incoming Telegram event object.data (dict[str, Any]): A dictionary containing various data passed through the middleware chain.Process:
self.container instance (which holds all application services and clients) is added to the data dictionary under the key "container".data dictionary are then passed to the handler for further processing.Returns:
Any: The result of the handler after the BotContainer has been injected into the data dictionary.