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
|
|
|
|
from, one for each kind of update.
|
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
|
|
|
|
:obj:`Message <pyrogram.api.types.pyrogram.Message>` objects.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram import Client
|
|
|
|
|
2018-04-11 21:18:17 +00:00
|
|
|
app = Client("my_account")
|
|
|
|
|
|
|
|
|
|
|
|
@app.on_message()
|
|
|
|
def my_handler(client, message):
|
|
|
|
print(message)
|
|
|
|
|
|
|
|
|
|
|
|
app.start()
|
|
|
|
app.idle()
|
|
|
|
|
2018-04-12 11:43:16 +00:00
|
|
|
If you prefer not to use decorators, there is an alternative way for registering Handlers.
|
2018-04-11 21:18:17 +00:00
|
|
|
This is useful, for example, if you want to keep your callback functions in a separate file.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram import Client, MessageHandler
|
|
|
|
|
2018-04-12 11:43:16 +00:00
|
|
|
|
2018-04-11 21:18:17 +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
|
|
|
app = Client("my_account")
|
|
|
|
|
|
|
|
app.add_handler(MessageHandler(my_handler))
|
|
|
|
|
|
|
|
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-14 14:10:46 +00:00
|
|
|
:class:`Filters <pyrogram.Filters>`. The next example will show you how to handle only messages
|
2018-04-11 21:18:17 +00:00
|
|
|
containing an :obj:`Audio <pyrogram.api.types.pyrogram.Audio>` object:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram import Filters
|
|
|
|
|
2018-04-12 11:43:16 +00:00
|
|
|
|
2018-04-11 21:18:17 +00:00
|
|
|
@app.on_message(Filters.audio)
|
|
|
|
def my_handler(client, message):
|
|
|
|
print(message)
|
|
|
|
|
|
|
|
or, without decorators:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram import Filters, Messagehandler
|
|
|
|
|
2018-04-12 11:43:16 +00:00
|
|
|
|
2018-04-11 21:18:17 +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
|
|
|
app.add_handler(MessageHandler(my_handler, Filters.audio))
|
|
|
|
|
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)
|
|
|
|
|
2018-04-14 14:10:46 +00:00
|
|
|
- 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
|
|
|
|
2018-04-14 14:10:46 +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-14 14:10:46 +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"))
|
|
|
|
def my_handler(client, message):
|
|
|
|
print("New message in @PyrogramChat")
|