MTPyroger/docs/source/resources/UpdateHandling.rst

72 lines
2.3 KiB
ReStructuredText
Raw Normal View History

2018-01-06 11:18:15 +00:00
Update Handling
===============
2019-04-12 13:52:06 +00:00
Let's now dive right into the core of the framework.
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, ...)
2019-04-12 13:52:06 +00:00
and are handled by registering one or more callback functions in your app using `Handlers <../pyrogram/Handlers.html>`_.
2018-04-16 17:48:50 +00:00
2019-04-12 13:52:06 +00:00
Each handler deals with a specific event and once a matching update arrives from Telegram, your registered callback
function will be called.
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
To explain how handlers work let's have a look at the most used one, the
2018-08-19 15:25:09 +00:00
:obj:`MessageHandler <pyrogram.MessageHandler>`, which will be in charge for handling :obj:`Message <pyrogram.Message>`
updates coming from all around your chats. Every other handler shares the same setup logic; you should not have troubles
settings them up once you learn from this section.
2018-04-11 21:18:17 +00:00
2019-04-12 13:52:06 +00:00
Using add_handler()
-------------------
2018-01-06 11:18:15 +00:00
2019-04-12 13:52:06 +00:00
The :meth:`add_handler() <pyrogram.Client.add_handler>` method takes any handler instance that wraps around your defined
callback function and registers it in your Client. Here's a full example that prints out the content of a message as
soon as it arrives:
2018-04-12 11:43:16 +00:00
.. code-block:: python
2019-04-12 13:52:06 +00:00
from pyrogram import Client, MessageHandler
2018-04-12 11:43:16 +00:00
2019-04-12 13:52:06 +00:00
def my_function(client, message):
2018-08-19 15:25:09 +00:00
print(message)
2018-04-16 17:48:50 +00:00
2019-04-12 13:52:06 +00:00
app = Client("my_account")
my_handler = MessageHandler(my_function)
app.add_handler(my_handler)
2018-08-19 15:25:09 +00:00
app.run()
2018-04-16 17:48:50 +00:00
2019-04-12 13:52:06 +00:00
Using Decorators
----------------
2018-04-16 17:48:50 +00:00
2019-04-12 13:52:06 +00:00
A much nicer way to register a MessageHandler is by decorating your callback function with the
:meth:`on_message() <pyrogram.Client.on_message>` decorator, which will still make use of add_handler() under the hood.
2018-04-16 17:48:50 +00:00
.. code-block:: python
2019-04-12 13:52:06 +00:00
from pyrogram import Client
app = Client("my_account")
2018-04-16 17:48:50 +00:00
2019-04-12 13:52:06 +00:00
@app.on_message()
2018-08-19 15:25:09 +00:00
def my_handler(client, message):
print(message)
2018-04-16 17:48:50 +00:00
2019-04-12 13:52:06 +00:00
app.run()
2018-04-16 17:48:50 +00:00
2019-04-12 13:52:06 +00:00
.. note::
Due to how these decorators work in Pyrogram, they will wrap your defined callback function in a tuple consisting of
``(handler, group)``; this will be the value held by your function identifier (e.g.: *my_function* from the example
above).
In case, for some reason, you want to get your own function back after it has been decorated, you need to access
``my_function[0].callback``, that is, the *callback* field of the *handler* object which is the first element in the
tuple.