@@ -13,9 +13,10 @@ Custom Filters
1313--------------
1414
1515An example to demonstrate how custom filters work is to show how to create and use one for the
16- :class: `~pyrogram.CallbackQueryHandler `. Note that callback queries updates are only received by bots; create and
17- :doc: `authorize your bot <../start/auth >`, then send a message with an inline keyboard to yourself. This allows you to
18- test your filter by pressing the inline button:
16+ :class: `~pyrogram.CallbackQueryHandler `. Note that callback queries updates are only received by bots as result of a
17+ user pressing an inline button attached to the bot's message; create and :doc: `authorize your bot <../start/auth >`,
18+ then send a message with an inline keyboard to yourself. This allows you to test your filter by pressing the inline
19+ button:
1920
2021.. code-block :: python
2122
@@ -25,7 +26,7 @@ test your filter by pressing the inline button:
2526 " username" , # Change this to your username or id
2627 " Pyrogram's custom filter test" ,
2728 reply_markup = InlineKeyboardMarkup(
28- [[InlineKeyboardButton(" Press me" , b " pyrogram" )]]
29+ [[InlineKeyboardButton(" Press me" , " pyrogram" )]]
2930 )
3031 )
3132
@@ -36,22 +37,22 @@ For this basic filter we will be using only the first two parameters of :meth:`~
3637
3738The code below creates a simple filter for hardcoded, static callback data. This filter will only allow callback queries
3839containing "Pyrogram" as data, that is, the function *func * you pass returns True in case the callback query data
39- equals to ``b "Pyrogram" ``.
40+ equals to ``"Pyrogram" ``.
4041
4142.. code-block :: python
4243
4344 static_data = Filters.create(
4445 name = " StaticdData" ,
45- func = lambda flt , callback_query : callback_query .data == b " Pyrogram"
46+ func = lambda flt , query : query .data == " Pyrogram"
4647 )
4748
4849 The ``lambda `` operator in python is used to create small anonymous functions and is perfect for this example, the same
4950could be achieved with a normal function, but we don't really need it as it makes sense only inside the filter's scope:
5051
5152.. code-block :: python
5253
53- def func (flt , callback_query ):
54- return callback_query .data == b " Pyrogram"
54+ def func (flt , query ):
55+ return query .data == " Pyrogram"
5556
5657 static_data = Filters.create(
5758 name = " StaticData" ,
@@ -63,8 +64,8 @@ The filter usage remains the same:
6364.. code-block :: python
6465
6566 @app.on_callback_query (static_data)
66- def pyrogram_data (client , callback_query ):
67- client.answer_callback_query(callback_query.id, " it works!" )
67+ def pyrogram_data (_ , query ):
68+ query.answer( " it works!" )
6869
6970 Filters with Arguments
7071----------------------
@@ -79,14 +80,14 @@ This is how a dynamic custom filter looks like:
7980 def dynamic_data (data ):
8081 return Filters.create(
8182 name = " DynamicData" ,
82- func = lambda flt , callback_query : flt.data == callback_query .data,
83- data = data # "data" kwarg is accessed with "filter .data"
83+ func = lambda flt , query : flt.data == query .data,
84+ data = data # "data" kwarg is accessed with "flt .data"
8485 )
8586
8687 And its usage:
8788
8889.. code-block :: python
8990
90- @app.on_callback_query (dynamic_data(b " Pyrogram" ))
91- def pyrogram_data (client , callback_query ):
92- client.answer_callback_query(callback_query.id, " it works!" )
91+ @app.on_callback_query (dynamic_data(" Pyrogram" ))
92+ def pyrogram_data (_ , query ):
93+ query.answer( " it works!" )
0 commit comments