mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
bound filters resolving rework, filters with default argument
* bound filters resolving rework, filters with default argument
This commit is contained in:
parent
3931253a88
commit
7484086d12
4 changed files with 136 additions and 16 deletions
|
|
@ -75,3 +75,30 @@ For example if you need to make simple text filter:
|
|||
Bound filters is always recursive propagates to the nested routers but will be available
|
||||
in nested routers only after attaching routers so that's mean you will need to
|
||||
include routers before registering handlers.
|
||||
|
||||
Resolving filters with default value
|
||||
====================================
|
||||
|
||||
Bound Filters with only default arguments will be automatically applied with default values
|
||||
to each handler in the router and nested routers to which this filter is bound.
|
||||
|
||||
For example, although we do not specify chat_type in the handler filters,
|
||||
but since the filter has a default value, the filter will be applied to the handler
|
||||
with a default value :code:`private`:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
class ChatType(BaseFilter):
|
||||
chat_type: str = "private"
|
||||
|
||||
async def __call__(self, message: Message , event_chat: Chat) -> bool:
|
||||
if event_chat:
|
||||
return event_chat.type == chat_type
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
router.message.bind_filter(ChatType)
|
||||
|
||||
@router.message()
|
||||
async def my_handler(message: Message): ...
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue