MTPyroger/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, ...)
and can be handled by using a callback function, that is, a function called every time an :obj:`Update` is received from
2018-01-06 11:18:15 +00:00
Telegram.
To set an update handler simply call :obj:`set_update_handler <pyrogram.Client.set_update_handler>`
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
The last line of the main() function, :obj:`client.idle() <pyrogram.Client.idle>`, is not strictly necessary but highly
2018-02-15 20:28:45 +00:00
recommended when using the update handler; it will block your script execution until you press :obj:`CTRL`:obj:`C` and
automatically call the :obj:`stop <pyrogram.Client.stop>` method which stops the Client and gently close the underlying
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>`_