TelegramDelete/src/AutoDelete.py

74 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
from telethon import TelegramClient
import configparser
from datetime import datetime
import os
dialogs = [] # list of known chats
me = None # the own entity
client = None
def printDialog(id , d):
# print (d.id, " ", d.name," ", d.pinned)
print('{0:2d} | {1:14d} | {2:30} | {3:1}'.format(id, d.id,d.name,d.pinned))
# ==================================
# MAIN program
# read config
try:
config = configparser.ConfigParser()
config.read('config.ini')
api_id = int(config.get('Main', 'api_id'))
api_hash = config.get('Main', 'api_hash')
workers = int(config.get('Main', 'workers'))
session_name = config.get('Main', 'user')
#print('apiid:', api_id, 'hash:', api_hash, 'worker', workers, 'sessionname', session_name)
except (configparser.NoSectionError, configparser.NoOptionError, ValueError):
print('invalid config.ini')
exit(3)
# create connection
client = TelegramClient(session_name, api_id, api_hash)
client.start()
print('client started')
me = client.get_me()
print(me)
# get dialogs
dialogs = client.get_dialogs(limit=100)
print(len(dialogs), ' Chats loaded.')
id = 0
#table header
print ('ID | Internal ID | Username | pinned\n———+————————————————+————————————————————————————————+———————')
#content
for d in dialogs:
printDialog(id,d)
id = id+1
get = int(input("Please Enter Chat ID: "))
if get < 0 or get >= id:
print ("Unknown Chat ID!")
exit(1)
selectedDialog = dialogs[get]
print ("selected: ", selectedDialog.name, 'retriving chat!')
chat = client.get_messages(selectedDialog, limit=2000000)
print("retrived ", len(chat), 'Messages.\nEnter a Date in the format: yyyy-mm-dd after wich all msgs should be deleted.')
datein = input("Date:")
date = datetime.strptime(datein, '%Y-%m-%d').replace(tzinfo=None)
print('Deleting...')
messagecount = 0
for msg in chat:
if msg != None:
if msg.date.replace(tzinfo=None) < date:
messagecount = messagecount+1
msg.delete()
print(messagecount, 'Messages deleted.')
print('End.')