|
| 1 | +Creating Filters |
| 2 | +================ |
| 3 | + |
| 4 | +Pyrogram already provides lots of built-in :class:`~pyrogram.Filters` to work with, but in case you can't find |
| 5 | +a specific one for your needs or want to build a custom filter by yourself (to be used in a different kind of handler, |
| 6 | +for example) you can use :meth:`~pyrogram.Filters.create`. |
| 7 | + |
| 8 | +.. note:: |
| 9 | + |
| 10 | + At the moment, the built-in filters are intended to be used with the :class:`~pyrogram.MessageHandler` only. |
| 11 | + |
| 12 | +Custom Filters |
| 13 | +-------------- |
| 14 | + |
| 15 | +An 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: |
| 19 | + |
| 20 | +.. code-block:: python |
| 21 | +
|
| 22 | + from pyrogram import InlineKeyboardMarkup, InlineKeyboardButton |
| 23 | +
|
| 24 | + app.send_message( |
| 25 | + "username", # Change this to your username or id |
| 26 | + "Pyrogram's custom filter test", |
| 27 | + reply_markup=InlineKeyboardMarkup( |
| 28 | + [[InlineKeyboardButton("Press me", b"pyrogram")]] |
| 29 | + ) |
| 30 | + ) |
| 31 | +
|
| 32 | +Basic Filters |
| 33 | +------------- |
| 34 | + |
| 35 | +For this basic filter we will be using only the first two parameters of :meth:`~pyrogram.Filters.create`. |
| 36 | + |
| 37 | +The code below creates a simple filter for hardcoded, static callback data. This filter will only allow callback queries |
| 38 | +containing "Pyrogram" as data, that is, the function *func* you pass returns True in case the callback query data |
| 39 | +equals to ``b"Pyrogram"``. |
| 40 | + |
| 41 | +.. code-block:: python |
| 42 | +
|
| 43 | + static_data = Filters.create( |
| 44 | + name="StaticdData", |
| 45 | + func=lambda flt, callback_query: callback_query.data == b"Pyrogram" |
| 46 | + ) |
| 47 | +
|
| 48 | +The ``lambda`` operator in python is used to create small anonymous functions and is perfect for this example, the same |
| 49 | +could be achieved with a normal function, but we don't really need it as it makes sense only inside the filter's scope: |
| 50 | + |
| 51 | +.. code-block:: python |
| 52 | +
|
| 53 | + def func(flt, callback_query): |
| 54 | + return callback_query.data == b"Pyrogram" |
| 55 | +
|
| 56 | + static_data = Filters.create( |
| 57 | + name="StaticData", |
| 58 | + func=func |
| 59 | + ) |
| 60 | +
|
| 61 | +The filter usage remains the same: |
| 62 | + |
| 63 | +.. code-block:: python |
| 64 | +
|
| 65 | + @app.on_callback_query(static_data) |
| 66 | + def pyrogram_data(client, callback_query): |
| 67 | + client.answer_callback_query(callback_query.id, "it works!") |
| 68 | +
|
| 69 | +Filters with Arguments |
| 70 | +---------------------- |
| 71 | + |
| 72 | +A much cooler filter would be one that accepts "Pyrogram" or any other data as argument at usage time. |
| 73 | +A dynamic filter like this will make use of the third parameter of :meth:`~pyrogram.Filters.create`. |
| 74 | + |
| 75 | +This is how a dynamic custom filter looks like: |
| 76 | + |
| 77 | +.. code-block:: python |
| 78 | +
|
| 79 | + def dynamic_data(data): |
| 80 | + return Filters.create( |
| 81 | + name="DynamicData", |
| 82 | + func=lambda flt, callback_query: flt.data == callback_query.data, |
| 83 | + data=data # "data" kwarg is accessed with "filter.data" |
| 84 | + ) |
| 85 | +
|
| 86 | +And its usage: |
| 87 | + |
| 88 | +.. code-block:: python |
| 89 | +
|
| 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!") |
0 commit comments