From b6cd30d281cb1124cda58aecdf62d8670c24a148 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 26 Feb 2018 16:26:11 +0100 Subject: [PATCH] Add simple_echo example --- examples/simple_echo.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 examples/simple_echo.py diff --git a/examples/simple_echo.py b/examples/simple_echo.py new file mode 100644 index 00000000..14abce2e --- /dev/null +++ b/examples/simple_echo.py @@ -0,0 +1,34 @@ +from pyrogram import Client +from pyrogram.api import types + +"""This simple example bot will reply to all private text messages""" + + +def update_handler(client, update, users, chats): + if isinstance(update, types.UpdateNewMessage): # Filter by UpdateNewMessage (Private Messages) + message = update.message # type: types.Message + + 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( + chat_id=message.from_id, + text=message.message, + 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()