mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 02:03:04 +00:00
Update middlewares docs
This commit is contained in:
parent
7f26ec9935
commit
bafc2ff341
11 changed files with 119 additions and 215 deletions
|
|
@ -39,7 +39,7 @@ dp.include_router(router1)
|
|||
## Handling updates
|
||||
All updates can be propagated to the dispatcher by `feed_update` method:
|
||||
|
||||
```
|
||||
```python3
|
||||
bot = Bot(...)
|
||||
dp = Dispathcher()
|
||||
|
||||
|
|
|
|||
95
docs/dispatcher/middlewares.md
Normal file
95
docs/dispatcher/middlewares.md
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
# Middlewares
|
||||
|
||||
**aiogram** provides powerful mechanism for customizing event handlers via middlewares.
|
||||
|
||||
Middlewares in bot framework seems like Middlewares mechanism in web-frameworks
|
||||
(like [aiohttp](https://docs.aiohttp.org/en/stable/web_advanced.html#aiohttp-web-middlewares),
|
||||
[fastapi](https://fastapi.tiangolo.com/tutorial/middleware/),
|
||||
[Django](https://docs.djangoproject.com/en/3.0/topics/http/middleware/) or etc.)
|
||||
with small difference - here is implemented two layers of middlewares (before and after filters).
|
||||
|
||||
!!! info
|
||||
Middleware is function that triggered on every event received from
|
||||
Telegram Bot API in many points on processing pipeline.
|
||||
|
||||
## Base theory
|
||||
|
||||
As many books and other literature in internet says:
|
||||
> Middleware is reusable software that leverages patterns and frameworks to bridge
|
||||
> the gap between the functional requirements of applications and the underlying operating systems,
|
||||
> network protocol stacks, and databases.
|
||||
|
||||
Middleware can modify, extend or reject processing event in many places of pipeline.
|
||||
|
||||
## Basics
|
||||
|
||||
Middleware instance can be applied for every type of Telegram Event (Update, Message, etc.) in two places
|
||||
|
||||
1. Outer scope - before processing filters (`#!python <router>.<event>.outer_middleware(...)`)
|
||||
2. Inner scope - after processing filters but before handler (`#!python <router>.<event>.middleware(...)`)
|
||||
|
||||
[](../assets/images/basics_middleware.png)
|
||||
|
||||
_(Click on image to zoom it)_
|
||||
|
||||
!!! warning
|
||||
|
||||
Middleware should be subclass of `BaseMiddleware` (`#!python3 from aiogram import BaseMiddleware`) or any async callable
|
||||
|
||||
## Arguments specification
|
||||
| Argument | Type | Description |
|
||||
| - | - | - |
|
||||
| `handler` | `#!python Callable[[T, Dict[str, Any]], Awaitable[Any]]` | Wrapped handler in middlewares chain |
|
||||
| `event` | `#!python T` | Incoming event (Subclass of `TelegramObject`) |
|
||||
| `data` | `#!python Dict[str, Any]` | Contextual data. Will be mapped to handler arguments |
|
||||
|
||||
## Examples
|
||||
|
||||
!!! danger
|
||||
|
||||
Middleware should always call `#!python await handler(event, data)` to propagate event for next middleware/handler
|
||||
|
||||
### Class-based
|
||||
```python3
|
||||
from aiogram import BaseMiddleware
|
||||
from aiogram.api.types import Message
|
||||
|
||||
|
||||
class CounterMiddleware(BaseMiddleware[Message]):
|
||||
def __init__(self) -> None:
|
||||
self.counter = 0
|
||||
|
||||
async def __call__(
|
||||
self,
|
||||
handler: Callable[[Message, Dict[str, Any]], Awaitable[Any]],
|
||||
event: Message,
|
||||
data: Dict[str, Any]
|
||||
) -> Any:
|
||||
self.counter += 1
|
||||
data['counter'] = self.counter
|
||||
return await handler(event, data)
|
||||
```
|
||||
and then
|
||||
```python3
|
||||
router = Router()
|
||||
router.message.middleware(CounterMiddleware())
|
||||
```
|
||||
|
||||
### Function-based
|
||||
```python3
|
||||
@dispatcher.update.outer_middleware()
|
||||
async def database_transaction_middleware(
|
||||
handler: Callable[[Update, Dict[str, Any]], Awaitable[Any]],
|
||||
event: Update,
|
||||
data: Dict[str, Any]
|
||||
) -> Any:
|
||||
async with database.transaction():
|
||||
return await handler(event, data)
|
||||
```
|
||||
|
||||
## Facts
|
||||
|
||||
1. Middlewares from outer scope will be called on every incoming event
|
||||
1. Middlewares from inner scope will be called only when filters pass
|
||||
1. Inner middlewares is always calls for `Update` event type in due to all incoming updates going to specific event type handler through built in update handler
|
||||
|
||||
|
|
@ -1,117 +0,0 @@
|
|||
# Basics
|
||||
|
||||
|
||||
|
||||
All middlewares should be made with `BaseMiddleware` (`#!python3 from aiogram import BaseMiddleware`) as base class.
|
||||
|
||||
For example:
|
||||
|
||||
```python3
|
||||
class MyMiddleware(BaseMiddleware): ...
|
||||
```
|
||||
|
||||
And then use next pattern in naming callback functions in middleware: `on_{step}_{event}`
|
||||
|
||||
Where is:
|
||||
|
||||
- `#!python3 step`:
|
||||
- `#!python3 pre_process`
|
||||
- `#!python3 process`
|
||||
- `#!python3 post_process`
|
||||
- `#!python3 event`:
|
||||
- `#!python3 update`
|
||||
- `#!python3 message`
|
||||
- `#!python3 edited_message`
|
||||
- `#!python3 channel_post`
|
||||
- `#!python3 edited_channel_post`
|
||||
- `#!python3 inline_query`
|
||||
- `#!python3 chosen_inline_result`
|
||||
- `#!python3 callback_query`
|
||||
- `#!python3 shipping_query`
|
||||
- `#!python3 pre_checkout_query`
|
||||
- `#!python3 poll`
|
||||
- `#!python3 poll_answer`
|
||||
- `#!python3 error`
|
||||
|
||||
## Connecting middleware with router
|
||||
|
||||
Middlewares can be connected with router by next ways:
|
||||
|
||||
1. `#!python3 router.use(MyMiddleware())` (**recommended**)
|
||||
1. `#!python3 router.middleware.setup(MyMiddleware())`
|
||||
1. `#!python3 MyMiddleware().setup(router.middleware)` (**not recommended**)
|
||||
|
||||
!!! warning
|
||||
One instance of middleware **can't** be registered twice in single or many middleware managers
|
||||
|
||||
## The specification of step callbacks
|
||||
|
||||
### Pre-process step
|
||||
|
||||
| Argument | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| event name | Any of event type (Update, Message and etc.) | Event |
|
||||
| `#!python3 data` | `#!python3 Dict[str, Any]` | Contextual data (Will be mapped to handler arguments) |
|
||||
|
||||
Returns `#!python3 Any`
|
||||
|
||||
### Process step
|
||||
|
||||
| Argument | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| event name | Any of event type (Update, Message and etc.) | Event |
|
||||
| `#!python3 data` | `#!python3 Dict[str, Any]` | Contextual data (Will be mapped to handler arguments) |
|
||||
|
||||
Returns `#!python3 Any`
|
||||
|
||||
### Post-Process step
|
||||
|
||||
| Argument | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| event name | Any of event type (Update, Message and etc.) | Event |
|
||||
| `#!python3 data` | `#!python3 Dict[str, Any]` | Contextual data (Will be mapped to handler arguments) |
|
||||
| `#!python3 result` | `#!python3 Dict[str, Any]` | Response from handlers |
|
||||
|
||||
Returns `#!python3 Any`
|
||||
|
||||
## Full list of available callbacks
|
||||
|
||||
- `#!python3 on_pre_process_update` - will be triggered on **pre process** `#!python3 update` event
|
||||
- `#!python3 on_process_update` - will be triggered on **process** `#!python3 update` event
|
||||
- `#!python3 on_post_process_update` - will be triggered on **post process** `#!python3 update` event
|
||||
- `#!python3 on_pre_process_message` - will be triggered on **pre process** `#!python3 message` event
|
||||
- `#!python3 on_process_message` - will be triggered on **process** `#!python3 message` event
|
||||
- `#!python3 on_post_process_message` - will be triggered on **post process** `#!python3 message` event
|
||||
- `#!python3 on_pre_process_edited_message` - will be triggered on **pre process** `#!python3 edited_message` event
|
||||
- `#!python3 on_process_edited_message` - will be triggered on **process** `#!python3 edited_message` event
|
||||
- `#!python3 on_post_process_edited_message` - will be triggered on **post process** `#!python3 edited_message` event
|
||||
- `#!python3 on_pre_process_channel_post` - will be triggered on **pre process** `#!python3 channel_post` event
|
||||
- `#!python3 on_process_channel_post` - will be triggered on **process** `#!python3 channel_post` event
|
||||
- `#!python3 on_post_process_channel_post` - will be triggered on **post process** `#!python3 channel_post` event
|
||||
- `#!python3 on_pre_process_edited_channel_post` - will be triggered on **pre process** `#!python3 edited_channel_post` event
|
||||
- `#!python3 on_process_edited_channel_post` - will be triggered on **process** `#!python3 edited_channel_post` event
|
||||
- `#!python3 on_post_process_edited_channel_post` - will be triggered on **post process** `#!python3 edited_channel_post` event
|
||||
- `#!python3 on_pre_process_inline_query` - will be triggered on **pre process** `#!python3 inline_query` event
|
||||
- `#!python3 on_process_inline_query` - will be triggered on **process** `#!python3 inline_query` event
|
||||
- `#!python3 on_post_process_inline_query` - will be triggered on **post process** `#!python3 inline_query` event
|
||||
- `#!python3 on_pre_process_chosen_inline_result` - will be triggered on **pre process** `#!python3 chosen_inline_result` event
|
||||
- `#!python3 on_process_chosen_inline_result` - will be triggered on **process** `#!python3 chosen_inline_result` event
|
||||
- `#!python3 on_post_process_chosen_inline_result` - will be triggered on **post process** `#!python3 chosen_inline_result` event
|
||||
- `#!python3 on_pre_process_callback_query` - will be triggered on **pre process** `#!python3 callback_query` event
|
||||
- `#!python3 on_process_callback_query` - will be triggered on **process** `#!python3 callback_query` event
|
||||
- `#!python3 on_post_process_callback_query` - will be triggered on **post process** `#!python3 callback_query` event
|
||||
- `#!python3 on_pre_process_shipping_query` - will be triggered on **pre process** `#!python3 shipping_query` event
|
||||
- `#!python3 on_process_shipping_query` - will be triggered on **process** `#!python3 shipping_query` event
|
||||
- `#!python3 on_post_process_shipping_query` - will be triggered on **post process** `#!python3 shipping_query` event
|
||||
- `#!python3 on_pre_process_pre_checkout_query` - will be triggered on **pre process** `#!python3 pre_checkout_query` event
|
||||
- `#!python3 on_process_pre_checkout_query` - will be triggered on **process** `#!python3 pre_checkout_query` event
|
||||
- `#!python3 on_post_process_pre_checkout_query` - will be triggered on **post process** `#!python3 pre_checkout_query` event
|
||||
- `#!python3 on_pre_process_poll` - will be triggered on **pre process** `#!python3 poll` event
|
||||
- `#!python3 on_process_poll` - will be triggered on **process** `#!python3 poll` event
|
||||
- `#!python3 on_post_process_poll` - will be triggered on **post process** `#!python3 poll` event
|
||||
- `#!python3 on_pre_process_poll_answer` - will be triggered on **pre process** `#!python3 poll_answer` event
|
||||
- `#!python3 on_process_poll_answer` - will be triggered on **process** `#!python3 poll_answer` event
|
||||
- `#!python3 on_post_process_poll_answer` - will be triggered on **post process** `#!python3 poll_answer` event
|
||||
- `#!python3 on_pre_process_error` - will be triggered on **pre process** `#!python3 error` event
|
||||
- `#!python3 on_process_error` - will be triggered on **process** `#!python3 error` event
|
||||
- `#!python3 on_post_process_error` - will be triggered on **post process** `#!python3 error` event
|
||||
|
|
@ -1,77 +0,0 @@
|
|||
# Overview
|
||||
|
||||
**aiogram** provides powerful mechanism for customizing event handlers via middlewares.
|
||||
|
||||
Middlewares in bot framework seems like Middlewares mechanism in web-frameworks
|
||||
(like [aiohttp](https://docs.aiohttp.org/en/stable/web_advanced.html#aiohttp-web-middlewares),
|
||||
[fastapi](https://fastapi.tiangolo.com/tutorial/middleware/),
|
||||
[Django](https://docs.djangoproject.com/en/3.0/topics/http/middleware/) or etc.)
|
||||
with small difference - here is implemented two layers of processing
|
||||
(named as [pipeline](#event-pipeline)).
|
||||
|
||||
!!! info
|
||||
Middleware is function that triggered on every event received from
|
||||
Telegram Bot API in many points on processing pipeline.
|
||||
|
||||
## Base theory
|
||||
|
||||
As many books and other literature in internet says:
|
||||
> Middleware is reusable software that leverages patterns and frameworks to bridge
|
||||
>the gap between the functional requirements of applications and the underlying operating systems,
|
||||
> network protocol stacks, and databases.
|
||||
|
||||
Middleware can modify, extend or reject processing event before-,
|
||||
on- or after- processing of that event.
|
||||
|
||||
[](../../assets/images/basics_middleware.png)
|
||||
|
||||
_(Click on image to zoom it)_
|
||||
|
||||
## Event pipeline
|
||||
|
||||
As described below middleware an interact with event in many stages of pipeline.
|
||||
|
||||
Simple workflow:
|
||||
|
||||
1. Dispatcher receive an [Update](../../api/types/update.md)
|
||||
1. Call **pre-process** update middleware in all routers tree
|
||||
1. Filter Update over handlers
|
||||
1. Call **process** update middleware in all routers tree
|
||||
1. Router detects event type (Message, Callback query, etc.)
|
||||
1. Router triggers **pre-process** <event> middleware of specific type
|
||||
1. Pass event over [filters](../filters/index.md) to detect specific handler
|
||||
1. Call **process** <event> middleware for specific type (only when handler for this event exists)
|
||||
1. *Do magick*. Call handler (Read more [Event observers](../router.md#event-observers))
|
||||
1. Call **post-process** <event> middleware
|
||||
1. Call **post-process** update middleware in all routers tree
|
||||
1. Emit response into webhook (when it needed)
|
||||
|
||||
!!! warning
|
||||
When filters does not match any handler with this event the `#!python3 process`
|
||||
step will not be called.
|
||||
|
||||
!!! warning
|
||||
When exception will be caused in handlers pipeline will be stopped immediately
|
||||
and then start processing error via errors handler and it own middleware callbacks.
|
||||
|
||||
!!! warning
|
||||
Middlewares for updates will be called for all routers in tree but callbacks for events
|
||||
will be called only for specific branch of routers.
|
||||
|
||||
### Pipeline in pictures:
|
||||
|
||||
#### Simple pipeline
|
||||
|
||||
[](../../assets/images/middleware_pipeline.png)
|
||||
|
||||
_(Click on image to zoom it)_
|
||||
|
||||
#### Nested routers pipeline
|
||||
|
||||
[](../../assets/images/middleware_pipeline_nested.png)
|
||||
|
||||
_(Click on image to zoom it)_
|
||||
|
||||
## Read more
|
||||
|
||||
- [Middleware Basics](basics.md)
|
||||
Loading…
Add table
Add a link
Reference in a new issue