pyrogram/docs/source/resources/UpdateHandling.rst

42 lines
1.5 KiB
ReStructuredText
Raw Normal View History

2018-01-06 11:18:15 +00:00
Update Handling
===============
Updates are events that happen in your Telegram account (incoming messages, new channel posts, user name changes, ...)
2018-03-25 19:30:39 +00:00
and can be handled by using a callback function, that is, a function called every time an ``Update`` is received from
2018-01-06 11:18:15 +00:00
Telegram.
2018-03-25 19:30:39 +00:00
To set an update handler simply call :meth:`set_update_handler <pyrogram.Client.set_update_handler>`
2018-01-06 11:18:15 +00:00
by passing the name of your defined callback function as argument *before* you start the Client.
Here's a complete example on how to set it up:
.. code-block:: python
from pyrogram import Client
def update_handler(client, update, users, chats):
2018-01-06 11:18:15 +00:00
print(update)
2018-02-16 18:07:03 +00:00
def main():
client = Client(session_name="example")
client.set_update_handler(update_handler)
2018-01-06 11:18:15 +00:00
client.start()
client.idle()
2018-01-06 11:18:15 +00:00
if __name__ == "__main__":
main()
2018-01-06 11:18:15 +00:00
2018-03-25 19:30:39 +00:00
The last line of the main function, :meth:`client.idle() <pyrogram.Client.idle>`, is not strictly necessary but highly
recommended when using the update handler; it will block your script execution until you press ``CTRL+C`` and
automatically call the :meth:`stop <pyrogram.Client.stop>` method which stops the Client and gently close the underlying
2018-02-15 20:28:45 +00:00
connection.
2018-01-06 11:18:15 +00:00
Examples
--------
- `Simple Echo <https://github.com/pyrogram/pyrogram/blob/master/examples/simple_echo.py>`_
- `Advanced Echo <https://github.com/pyrogram/pyrogram/blob/master/examples/advanced_echo.py>`_
- `Advanced Echo 2 <https://github.com/pyrogram/pyrogram/blob/master/examples/advanced_echo2.py>`_