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!')
@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