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-02-13 17:13:49 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-02-13 17:13:49 +00:00
|
|
|
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():
|
2018-02-13 17:13:49 +00:00
|
|
|
client = Client(session_name="example")
|
|
|
|
client.set_update_handler(update_handler)
|
2018-01-06 11:18:15 +00:00
|
|
|
|
2018-02-13 17:13:49 +00:00
|
|
|
client.start()
|
|
|
|
client.idle()
|
2018-01-06 11:18:15 +00:00
|
|
|
|
2018-02-13 17:13:49 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
2018-01-06 11:18:15 +00:00
|
|
|
|
2018-02-13 17:13:49 +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
|
|
|
|
--------
|
|
|
|
|
2018-02-13 17:13:49 +00:00
|
|
|
- Simple Echo example for **Private Messages**:
|
2018-01-06 11:18:15 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram.api import types
|
|
|
|
|
2018-02-15 20:28:45 +00:00
|
|
|
def update_handler(client, update, users, chats):
|
2018-02-13 17:13:49 +00:00
|
|
|
if isinstance(update, types.UpdateNewMessage): # Filter by UpdateNewMessage (PM and Groups)
|
|
|
|
message = update.message # type: types.Message
|
2018-01-06 11:18:15 +00:00
|
|
|
|
2018-02-13 17:13:49 +00:00
|
|
|
if isinstance(message, types.Message): # Filter by Message to exclude MessageService and MessageEmpty
|
|
|
|
if isinstance(message.to_id, types.PeerUser): # Private Messages (Message from user)
|
|
|
|
client.send_message(message.from_id, message.message, reply_to_message_id=message.id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- Advanced Echo example for both **Private Messages** and **Basic Groups** (with mentions).
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram.api import types
|
|
|
|
|
|
|
|
def update_handler(client, update, users, chats):
|
|
|
|
if isinstance(update, types.UpdateNewMessage): # Filter by UpdateNewMessage (PM and Chats)
|
|
|
|
message = update.message
|
|
|
|
|
|
|
|
if isinstance(message, types.Message): # Filter by Message to exclude MessageService and MessageEmpty
|
|
|
|
if isinstance(message.to_id, types.PeerUser): # Private Messages
|
|
|
|
text = '[{}](tg://user?id={}) said "{}" to me ([{}](tg://user?id={}))'.format(
|
|
|
|
users[message.from_id].first_name,
|
|
|
|
users[message.from_id].id,
|
|
|
|
message.message,
|
|
|
|
users[message.to_id.user_id].first_name,
|
|
|
|
users[message.to_id.user_id].id
|
|
|
|
)
|
|
|
|
|
|
|
|
client.send_message(
|
|
|
|
message.from_id, # Send the message to the private chat (from_id)
|
|
|
|
text,
|
|
|
|
reply_to_message_id=message.id
|
|
|
|
)
|
|
|
|
else: # Group chats
|
|
|
|
text = '[{}](tg://user?id={}) said "{}" in **{}** group'.format(
|
|
|
|
users[message.from_id].first_name,
|
|
|
|
users[message.from_id].id,
|
|
|
|
message.message,
|
|
|
|
chats[message.to_id.chat_id].title
|
|
|
|
)
|
|
|
|
|
|
|
|
client.send_message(
|
|
|
|
message.to_id, # Send the message to the group chat (to_id)
|
|
|
|
text,
|
|
|
|
reply_to_message_id=message.id
|
|
|
|
)
|
|
|
|
|
|
|
|
- Advanced Echo example for **Supergroups** (with mentions):
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram.api import types
|
|
|
|
|
|
|
|
def update_handler(client, update, users, chats):
|
|
|
|
if isinstance(update, types.UpdateNewChannelMessage): # Filter by UpdateNewChannelMessage (Channels/Supergroups)
|
|
|
|
message = update.message
|
|
|
|
|
|
|
|
if isinstance(message, types.Message): # Filter by Message to exclude MessageService and MessageEmpty
|
|
|
|
if chats[message.to_id.channel_id].megagroup: # Only handle messages from Supergroups not Channels
|
|
|
|
text = '[{}](tg://user?id={}) said "{}" in **{}** supergroup'.format(
|
|
|
|
users[message.from_id].first_name,
|
|
|
|
users[message.from_id].id,
|
|
|
|
message.message,
|
|
|
|
chats[message.to_id.channel_id].title
|
|
|
|
)
|
|
|
|
|
|
|
|
client.send_message(
|
|
|
|
message.to_id,
|
|
|
|
text,
|
|
|
|
reply_to_message_id=message.id
|
|
|
|
)
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
The Advanced Examples above will make you reply to **all** new messages in private chats and in every single
|
|
|
|
group/supergroup you are in. Make sure you add an extra check to filter them:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Filter Groups by ID
|
|
|
|
if message.to_id.chat_id == MY_GROUP_ID:
|
|
|
|
...
|
|
|
|
|
|
|
|
# Filter Supergroups by ID
|
|
|
|
if message.to_id.channel_id == MY_SUPERGROUP_ID:
|
|
|
|
...
|
|
|
|
|
|
|
|
# Filter Supergroups by Username
|
|
|
|
if chats[message.to_id.channel_id].username == MY_SUPERGROUP_USERNAME:
|
|
|
|
...
|