2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-31 06:16:06 +00:00

Update filters: Make the name argument optional

This commit is contained in:
Dan
2019-06-28 11:11:59 +02:00
parent 506253e506
commit 155580649a
2 changed files with 92 additions and 96 deletions

View File

@@ -24,7 +24,7 @@ button:
app.send_message(
"username", # Change this to your username or id
"Pyrogram's custom filter test",
"Pyrogram custom filter test",
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton("Press me", "pyrogram")]]
)
@@ -33,61 +33,54 @@ button:
Basic Filters
-------------
For this basic filter we will be using only the first two parameters of :meth:`~pyrogram.Filters.create`.
For this basic filter we will be using only the first parameter of :meth:`~pyrogram.Filters.create`.
The code below creates a simple filter for hardcoded, static callback data. This filter will only allow callback queries
containing "Pyrogram" as data, that is, the function *func* you pass returns True in case the callback query data
equals to ``"Pyrogram"``.
containing "pyrogram" as data, that is, the function *func* you pass returns True in case the callback query data
equals to ``"pyrogram"``.
.. code-block:: python
static_data = Filters.create(
name="StaticdData",
func=lambda flt, query: query.data == "Pyrogram"
)
static_data_filter = Filters.create(lambda _, query: query.data == "pyrogram")
The ``lambda`` operator in python is used to create small anonymous functions and is perfect for this example, the same
could be achieved with a normal function, but we don't really need it as it makes sense only inside the filter's scope:
could be achieved with a normal function, but we don't really need it as it makes sense only inside the filter scope:
.. code-block:: python
def func(flt, query):
return query.data == "Pyrogram"
def func(_, query):
return query.data == "pyrogram"
static_data = Filters.create(
name="StaticData",
func=func
)
static_data_filter = Filters.create(func)
The filter usage remains the same:
.. code-block:: python
@app.on_callback_query(static_data)
@app.on_callback_query(static_data_filter)
def pyrogram_data(_, query):
query.answer("it works!")
Filters with Arguments
----------------------
A much cooler filter would be one that accepts "Pyrogram" or any other data as argument at usage time.
A dynamic filter like this will make use of the third parameter of :meth:`~pyrogram.Filters.create`.
A much cooler filter would be one that accepts "pyrogram" or any other data as argument at usage time.
A dynamic filter like this will make use of named arguments for the :meth:`~pyrogram.Filters.create` method.
This is how a dynamic custom filter looks like:
.. code-block:: python
def dynamic_data(data):
def dynamic_data_filter(data):
return Filters.create(
name="DynamicData",
func=lambda flt, query: flt.data == query.data,
data=data # "data" kwarg is accessed with "flt.data"
lambda flt, query: flt.data == query.data,
data=data # "data" kwarg is accessed with "flt.data" above
)
And its usage:
.. code-block:: python
@app.on_callback_query(dynamic_data("Pyrogram"))
@app.on_callback_query(dynamic_data_filter("pyrogram"))
def pyrogram_data(_, query):
query.answer("it works!")