Skip to content

Instantly share code, notes, and snippets.

@yi-jiayu
Last active March 9, 2025 14:40
Show Gist options
  • Save yi-jiayu/acc31fbad5a25f746430428ce4d62c28 to your computer and use it in GitHub Desktop.
Save yi-jiayu/acc31fbad5a25f746430428ce4d62c28 to your computer and use it in GitHub Desktop.
Automatic replies for Telegram (Updated for Telethon 1.6.2)
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'
session_file = '/path/to/session/file' # use your username if unsure
password = 'YOUR_PASSWORD' # if you have two-step verification enabled
# content of the automatic reply
message = "Sorry, I'll be away until next week!"
if __name__ == '__main__':
# Create the client and connect
# use sequential_updates=True to respond to messages one at a time
client = TelegramClient(session_file, api_id, api_hash, sequential_updates=True)
@client.on(events.NewMessage(incoming=True))
async def handle_new_message(event):
if event.is_private: # only auto-reply to private chats
from_ = await event.client.get_entity(event.from_id) # this lookup will be cached by telethon
if not from_.bot: # don't auto-reply to bots
print(time.asctime(), '-', event.message) # optionally log time and message
time.sleep(1) # pause for 1 second to rate-limit automatic replies
await event.respond(message)
print(time.asctime(), '-', 'Auto-replying...')
client.start(phone, password)
client.run_until_disconnected()
print(time.asctime(), '-', 'Stopped!')
@Scappotto
Copy link

Thanks for the code, is it possible to modify it to send only one replay to a single contact? How can i do that?

@csb77
Copy link

csb77 commented Feb 4, 2020

Thanks for the code, is it possible to modify it to send only one replay to a single contact? How can i do that?

I know my response is the classic generic response but maybe learn python and coding a bit first before asking other people to do your work for you ?

@HarishRane1
Copy link

How to use please guide

@ak-695
Copy link

ak-695 commented Mar 2, 2022

🤫

@telebae
Copy link

telebae commented Mar 9, 2025

thanks for the lost accs, it just logging me out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment