Skip to content

Instantly share code, notes, and snippets.

@dolohow
Last active March 5, 2023 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolohow/9f02960428020d2ec1ba33053ee61659 to your computer and use it in GitHub Desktop.
Save dolohow/9f02960428020d2ec1ba33053ee61659 to your computer and use it in GitHub Desktop.
Backup and delete old messages on telegram
#!/usr/bin/python3
import pymongo
import json
import asyncio
from pyrogram import Client
client = pymongo.MongoClient("localhost", 27017)
db = client.telegram
# Configure here
api_id =
api_hash = ''
chat_id = '-'
app = Client("my_account", api_id, api_hash)
async def main():
async with app:
async for message in app.get_chat_history(chat_id):
if message.date.year < 2023 and message.text and not message.media:
db.lapsy.insert_one(json.loads(str(message)))
asyncio.create_task(message.delete())
app.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment