From 5cab0b42bb9984641277835894d15f20cedf4d63 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 26 Feb 2018 16:49:14 +0100 Subject: [PATCH] Add advanced_echo2 example --- examples/advanced_echo2.py | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/advanced_echo2.py diff --git a/examples/advanced_echo2.py b/examples/advanced_echo2.py new file mode 100644 index 00000000..460c4cf8 --- /dev/null +++ b/examples/advanced_echo2.py @@ -0,0 +1,55 @@ +from pyrogram import Client +from pyrogram.api import types + +"""This example is similar to advanced_echo.py, except for the fact that it will reply to Supergroup text messages only. + +Beware! This script will make you reply to ALL new messages in every single supergroup you are in. +Make sure you add an extra check to filter them: + +# 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: + ... +""" + + +def update_handler(client, update, users, chats): + # Channels and Supergroups share the same type (Channel). The .megagroup field is used to tell them apart, and is + # True for Supegroups, False for Channels. + 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 + ) + + +def main(): + # Pyrogram setup + client = Client("example") + + # Set the update_handler callback function + client.set_update_handler(update_handler) + client.start() + + # Blocks the program execution until you press CTRL+C then + # automatically stops the Client by closing the underlying connection + client.idle() + + +if __name__ == "__main__": + main()