Skip to content

Instantly share code, notes, and snippets.

@RichStone
Forked from yi-jiayu/autoreplier.py
Created October 30, 2018 20:46
Show Gist options
  • Select an option

  • Save RichStone/f1d1b1bd8763519cdbd2f3aef54b0d08 to your computer and use it in GitHub Desktop.

Select an option

Save RichStone/f1d1b1bd8763519cdbd2f3aef54b0d08 to your computer and use it in GitHub Desktop.
Automatic replies for Telegram
import time
from telethon import TelegramClient, events
# sample API_ID from https://github.com/telegramdesktop/tdesktop/blob/f98fdeab3fb2ba6f55daf8481595f879729d1b84/Telegram/SourceFiles/config.h#L220
# or use your own
api_id = 17349
api_hash = '344583e45741c457fe1862106095a5eb'
# fill in your own details here
phone = 'YOUR_PHONE_NUMBER'
username = 'YOUR_USERNAME'
password = 'YOUR_PASSWORD' # if you have two-step verification enabled
# content of the automatic reply
message = "Sorry, I'll be away until next week!"
def main():
# Create the client and connect
client = TelegramClient(username, api_id, api_hash, update_workers=1, spawn_read_thread=False)
client.start(phone, password)
@client.on(events.NewMessage(incoming=True))
def _(event):
if event.is_private:
print(time.asctime(), '-', event.message) # optionally log time and message
time.sleep(1) # pause for 1 second to rate-limit automatic replies
client.send_message(event.message.from_id, message)
print(time.asctime(), '-', 'Auto-replying...')
client.idle()
client.disconnect()
print(time.asctime(), '-', 'Stopped!')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment