mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 18:19:34 +00:00
Combining filters (again) (#1018)
* Added explicit logic filters, added slots to all other filters * Update translation files * Update docs
This commit is contained in:
parent
1a3e2a8991
commit
b0f251a8b8
26 changed files with 823 additions and 361 deletions
|
|
@ -53,3 +53,50 @@ Own filter example
|
|||
For example if you need to make simple text filter:
|
||||
|
||||
.. literalinclude:: ../../../examples/own_filter.py
|
||||
|
||||
.. _combining-filters:
|
||||
|
||||
Combining Filters
|
||||
=================
|
||||
|
||||
In general, all filters can be combined in two ways
|
||||
|
||||
|
||||
Recommended way
|
||||
---------------
|
||||
|
||||
If you specify multiple filters in a row, it will be checked with an "and" condition:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@<router>.message(Text(startswith="show"), Text(endswith="example"))
|
||||
|
||||
|
||||
Also, if you want to use two alternative ways to run the sage handler ("or" condition)
|
||||
you can register the handler twice or more times as you like
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@<router>.message(Text(text="hi"))
|
||||
@<router>.message(CommandStart())
|
||||
|
||||
|
||||
Also sometimes you will need to invert the filter result, for example you have an *IsAdmin* filter
|
||||
and you want to check if the user is not an admin
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@<router>.message(~IsAdmin())
|
||||
|
||||
|
||||
Another possible way
|
||||
--------------------
|
||||
|
||||
An alternative way is to combine using special functions (:func:`and_f`, :func:`or_f`, :func:`invert_f` from :code:`aiogram.filters` module):
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
and_f(Text(startswith="show"), Text(endswith="example"))
|
||||
or_f(Text(text="hi"), CommandStart())
|
||||
invert_f(IsAdmin())
|
||||
and_f(<A>, or_f(<B>, <C>))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue