Skip to content

Instantly share code, notes, and snippets.

@yusufpapurcu
Created May 6, 2021 10:41
Show Gist options
  • Save yusufpapurcu/02452b52f3f07e7b9bb08ffd5a09f11b to your computer and use it in GitHub Desktop.
Save yusufpapurcu/02452b52f3f07e7b9bb08ffd5a09f11b to your computer and use it in GitHub Desktop.
Get last 1 hour of telegram chat with python and telethon
from telethon import TelegramClient
from datetime import datetime, timedelta, timezone
# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon.session', api_id, api_hash) # You need run twice because this part create session in first run.
client.start()
user_name = "me" # Write here username you want to get last 1 hour of chat.
for message in client.iter_messages(user_name,offset_date=datetime.now(tz=timezone.utc) - timedelta(hours=1),reverse=True):
print(message.sender_id, ':', message.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment