pyrogram/docs/source/resources/UpdateHandling.rst

189 lines
5.3 KiB
ReStructuredText
Raw Normal View History

2018-01-06 11:18:15 +00:00
Update Handling
===============
2018-04-12 11:43:16 +00:00
Updates are events that happen in your Telegram account (incoming messages, new channel posts, new members join, ...)
and are handled by registering one or more callback functions with an Handler. There are multiple Handlers to choose
2018-04-16 17:48:50 +00:00
from, one for each kind of update:
2018-05-02 14:00:48 +00:00
- `MessageHandler <../pyrogram/handlers/MessageHandler.html>`_
- `CallbackQueryHandler <../pyrogram/handlers/CallbackQueryHandler.html>`_
- `RawUpdateHandler <../pyrogram/handlers/RawUpdateHandler.html>`_
2018-01-06 11:18:15 +00:00
2018-04-11 21:18:17 +00:00
Registering an Handler
----------------------
2018-01-06 11:18:15 +00:00
2018-04-11 21:18:17 +00:00
We shall examine the :obj:`MessageHandler <pyrogram.MessageHandler>`, which will be in charge for handling
2018-05-02 14:00:48 +00:00
:obj:`Message <pyrogram.Message>` objects.
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
- The easiest and nicest way to register a MessageHandler is by decorating your function with the
:meth:`on_message() <pyrogram.Client.on_message>` decorator. Here's a full example that prints out the content
of a message as soon as it arrives.
2018-01-06 11:18:15 +00:00
2018-04-15 18:56:06 +00:00
.. code-block:: python
2018-01-06 11:18:15 +00:00
2018-04-15 18:56:06 +00:00
from pyrogram import Client
2018-01-06 11:18:15 +00:00
2018-04-15 18:56:06 +00:00
app = Client("my_account")
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
@app.on_message()
def my_handler(client, message):
print(message)
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
app.start()
app.idle()
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
- If you prefer not to use decorators, there is an alternative way for registering Handlers.
2018-04-16 17:48:50 +00:00
This is useful, for example, when you want to keep your callback functions in separate files.
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
.. code-block:: python
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
from pyrogram import Client, MessageHandler
2018-04-12 11:43:16 +00:00
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
def my_handler(client, message):
print(message)
2018-04-12 11:43:16 +00:00
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
app = Client("my_account")
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
app.add_handler(MessageHandler(my_handler))
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
app.start()
app.idle()
2018-04-12 11:43:16 +00:00
2018-04-11 21:18:17 +00:00
Using Filters
-------------
2018-04-12 11:43:16 +00:00
For a finer grained control over what kind of messages will be allowed or not in your callback functions, you can use
2018-04-15 18:56:06 +00:00
:class:`Filters <pyrogram.Filters>`.
2018-04-11 21:18:17 +00:00
2018-04-16 17:48:50 +00:00
- This example will show you how to **only** handle messages containing an
2018-05-02 14:00:48 +00:00
:obj:`Audio <pyrogram.Audio>` object and filter out any other message:
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
.. code-block:: python
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
from pyrogram import Filters
2018-04-12 11:43:16 +00:00
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
@app.on_message(Filters.audio)
def my_handler(client, message):
print(message)
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
- or, without decorators:
2018-04-11 21:18:17 +00:00
2018-04-15 18:56:06 +00:00
.. code-block:: python
2018-04-16 17:48:50 +00:00
from pyrogram import Filters, MessageHandler
2018-04-11 21:18:17 +00:00
2018-04-12 11:43:16 +00:00
2018-04-15 18:56:06 +00:00
def my_handler(client, message):
print(message)
2018-04-11 21:18:17 +00:00
2018-04-12 11:43:16 +00:00
2018-04-15 18:56:06 +00:00
app.add_handler(MessageHandler(my_handler, Filters.audio))
2018-04-11 21:18:17 +00:00
2018-04-12 11:43:16 +00:00
Combining Filters
-----------------
2018-04-11 21:18:17 +00:00
Filters can also be used in a more advanced way by combining more filters together using bitwise operators:
- Use ``~`` to invert a filter (behaves like the ``not`` operator).
2018-04-12 11:43:16 +00:00
- Use ``&`` and ``|`` to merge two filters (behave like ``and``, ``or`` operators respectively).
2018-04-11 21:18:17 +00:00
Here are some examples:
- Message is a **text** message **and** is **not edited**.
.. code-block:: python
@app.on_message(Filters.text & ~Filters.edited)
def my_handler(client, message):
print(message)
- Message is a **sticker** **and** is coming from a **channel or** a **private** chat.
2018-04-11 21:18:17 +00:00
.. code-block:: python
@app.on_message(Filters.sticker & (Filters.channel | Filters.private))
def my_handler(client, message):
print(message)
2018-01-06 11:18:15 +00:00
2018-04-12 11:43:16 +00:00
Advanced Filters
----------------
Some filters, like :obj:`command() <pyrogram.Filters.command>` or :obj:`regex() <pyrogram.Filters.regex>`
can also accept arguments:
2018-01-06 11:18:15 +00:00
2018-04-12 11:43:16 +00:00
- Message is either a */start* or */help* **command**.
2018-01-06 11:18:15 +00:00
2018-04-11 21:18:17 +00:00
.. code-block:: python
2018-01-06 11:18:15 +00:00
2018-04-11 21:18:17 +00:00
@app.on_message(Filters.command(["start", "help"]))
def my_handler(client, message):
print(message)
2018-01-06 11:18:15 +00:00
- Message is a **text** message matching the given **regex** pattern.
2018-01-06 11:18:15 +00:00
2018-04-11 21:18:17 +00:00
.. code-block:: python
2018-01-06 11:18:15 +00:00
2018-04-11 21:18:17 +00:00
@app.on_message(Filters.regex("pyrogram"))
def my_handler(client, message):
2018-04-12 11:43:16 +00:00
print(message)
2018-04-16 17:48:50 +00:00
More handlers using different filters can also live together.
2018-04-12 11:43:16 +00:00
.. code-block:: python
@app.on_message(Filters.command("start"))
def start_command(client, message):
print("This is the /start command")
@app.on_message(Filters.command("help"))
def help_command(client, message):
print("This is the /help command")
@app.on_message(Filters.chat("PyrogramChat"))
2018-04-15 18:56:06 +00:00
def from_pyrogramchat(client, message):
2018-04-12 11:43:16 +00:00
print("New message in @PyrogramChat")
2018-04-16 17:48:50 +00:00
Handler Groups
--------------
If you register handlers with overlapping filters, only the first one is executed and any other handler will be ignored.
In order to process the same message more than once, you can register your handler in a different group.
Groups are identified by a number (number 0 being the default) and are sorted. This means that a lower group number has
a higher priority.
For example, in:
.. code-block:: python
@app.on_message(Filters.text | Filters.sticker)
def text_or_sticker(client, message):
print("Text or Sticker")
@app.on_message(Filters.text)
def just_text(client, message):
print("Just Text")
``just_text`` is never executed. To enable it, simply register the function using a different group:
.. code-block:: python
@app.on_message(Filters.text, group=1)
def just_text(client, message):
print("Just Text")
or, if you want ``just_text`` to be fired *before* ``text_or_sticker``:
.. code-block:: python
@app.on_message(Filters.text, group=-1)
def just_text(client, message):
print("Just Text")