Created
September 24, 2019 14:49
-
-
Save alexandernst/832baf98104dd583b011fad64a11dca2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from telethon import TelegramClient | |
api_id = 123456789 | |
api_hash = 'abcdef123456789' | |
client = TelegramClient('session_name', api_id, api_hash) | |
client.session.report_errors = True | |
client.start() | |
delete_me_from = [ | |
"#BOFHers", | |
] | |
delta_time = 0.5 # Messages sent more than X hours ago | |
async def main(): | |
try: | |
chats = client.iter_dialogs() | |
async for chat in chats: | |
if chat.name in delete_me_from: | |
messages_exist = False | |
deleted_any_message = False | |
while True: | |
messages_exist = False | |
deleted_any_message = False | |
messages = client.iter_messages(chat, 100, from_user='me', wait_time=5) | |
now = datetime.datetime.now(tz=datetime.timezone.utc) | |
async for message in messages: | |
messages_exist = True | |
delta = now - message.date | |
time = delta.total_seconds() / 60 / 60 | |
if time > delta_time: | |
print(message.text) | |
await message.delete(revoke=True) | |
deleted_any_message = True | |
if not messages_exist: | |
break | |
if messages_exist and not deleted_any_message: | |
break | |
except Exception as e: | |
pass | |
with client: | |
client.loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment