2019-05-12 19:26:55 +02:00
|
|
|
Handling Updates
|
|
|
|
================
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2022-04-24 11:56:07 +02:00
|
|
|
:doc:`Invoking API methods <invoking>` sequentially is one way to use Pyrogram. This page deals with Telegram updates
|
|
|
|
and how to handle new incoming messages or other events in Pyrogram.
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2020-04-01 20:08:46 +02:00
|
|
|
.. contents:: Contents
|
|
|
|
:backlinks: none
|
2020-08-22 08:05:05 +02:00
|
|
|
:depth: 1
|
2020-04-01 20:08:46 +02:00
|
|
|
:local:
|
|
|
|
|
|
|
|
-----
|
|
|
|
|
2019-05-13 18:04:44 +02:00
|
|
|
Defining Updates
|
|
|
|
----------------
|
|
|
|
|
2022-04-24 11:56:07 +02:00
|
|
|
As hinted already, updates are events that happen in your Telegram account (incoming messages, new members join,
|
2022-01-07 10:18:51 +01:00
|
|
|
bot button presses, etc.), which are meant to notify you about a new specific state that has changed. These updates are
|
|
|
|
handled by registering one or more callback functions in your app using :doc:`Handlers <../api/handlers>`.
|
2019-05-10 16:14:10 +02:00
|
|
|
|
|
|
|
Each handler deals with a specific event and once a matching update arrives from Telegram, your registered callback
|
2019-05-18 01:45:01 +02:00
|
|
|
function will be called back by the framework and its body executed.
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2019-05-20 19:19:26 +02:00
|
|
|
Registering a Handler
|
|
|
|
---------------------
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
To explain how handlers work let's examine the one which will be in charge for handling :class:`~pyrogram.types.Message`
|
2020-08-22 16:09:38 +02:00
|
|
|
updates coming from all around your chats. Every other kind of handler shares the same setup logic and you should not
|
|
|
|
have troubles settings them up once you learn from this section.
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
Using Decorators
|
|
|
|
^^^^^^^^^^^^^^^^
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
The most elegant way to register a message handler is by using the :meth:`~pyrogram.Client.on_message` decorator:
|
2019-05-10 16:14:10 +02:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
from pyrogram import Client
|
2019-05-10 16:14:10 +02:00
|
|
|
|
|
|
|
app = Client("my_account")
|
|
|
|
|
2022-04-24 11:56:07 +02:00
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
@app.on_message()
|
2022-01-07 10:18:51 +01:00
|
|
|
async def my_handler(client, message):
|
|
|
|
await message.forward("me")
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2022-04-24 11:56:07 +02:00
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
app.run()
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
The defined function ``my_handler``, which accepts the two arguments *(client, message)*, will be the function that gets
|
|
|
|
executed every time a new message arrives.
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2022-01-07 10:18:51 +01:00
|
|
|
In the last line we see again the :meth:`~pyrogram.Client.run` method, this time used without any argument.
|
|
|
|
Its purpose here is simply to automatically :meth:`~pyrogram.Client.start`, keep the Client online so that it can listen
|
|
|
|
for updates and :meth:`~pyrogram.Client.stop` it once you hit ``CTRL+C``.
|
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
Using add_handler()
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
The :meth:`~pyrogram.Client.add_handler` method takes any handler instance that wraps around your defined callback
|
2022-01-07 10:18:51 +01:00
|
|
|
function and registers it in your Client. It is useful in case you want to programmatically add handlers.
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
.. code-block:: python
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
from pyrogram import Client
|
|
|
|
from pyrogram.handlers import MessageHandler
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2022-04-24 11:56:07 +02:00
|
|
|
|
2022-01-07 10:18:51 +01:00
|
|
|
async def my_function(client, message):
|
|
|
|
await message.forward("me")
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2022-04-24 11:56:07 +02:00
|
|
|
|
2019-05-10 16:14:10 +02:00
|
|
|
app = Client("my_account")
|
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
my_handler = MessageHandler(my_function)
|
|
|
|
app.add_handler(my_handler)
|
2019-05-10 16:14:10 +02:00
|
|
|
|
2020-08-22 08:05:05 +02:00
|
|
|
app.run()
|