diff --git a/Update-Handling.md b/Update-Handling.md new file mode 100644 index 0000000..292ef9c --- /dev/null +++ b/Update-Handling.md @@ -0,0 +1,33 @@ +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 `Update` is received from Telegram. + +To set an update handler simply call `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: + +``` python +from pyrogram import Client + + +def callback(update): + print(update) + + +client = Client(session_name="example") +client.set_update_handler(callback) + +client.start() +client.idle() +``` + +The last line, `client.idle()` is not strictly necessary but highly recommended; it will block your script execution until you press CTRL+C and automatically call the `stop` method which stops the Client and gently close the underlying connection. + +# Examples + +- Echo: + + ``` python + def callback(update): + if isinstance(update, types.UpdateShortMessage) and not update.out: + client.send_message(update.user_id, update.message) + ``` + This checks if the update type is `UpdateShortMessage` and that the update is not generated by yourself (i.e., the message is not outgoing), then sends back the same message. \ No newline at end of file