-
Notifications
You must be signed in to change notification settings - Fork 4
/
telegram_message_cleaner.py
148 lines (104 loc) · 4.29 KB
/
telegram_message_cleaner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
import argparse
import asyncio
import sys
from prettytable import PrettyTable
from telethon import TelegramClient
# Use your own values here
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('Telegram Message Cleaner', api_id, api_hash)
async def get_info(chat):
messages = (
await client.get_messages(chat.entity, limit=None, from_user='me') if hasattr(chat, 'entity')
else await client.get_messages(chat, limit=None, from_user='me')
)
id_ = chat.id
name = chat.title
total_messages = messages.total
return id_, name, total_messages
async def group_messages_statistic(ids):
info = []
await client.start()
dialogs = await client.get_dialogs()
groups = (
[await client.get_entity(int(dialog_id)) for dialog_id in ids] if ids
else [dialog for dialog in dialogs if dialog.is_group]
)
for future in asyncio.as_completed(map(get_info, groups)):
id_, name, total_messages = await asyncio.shield(future)
info.append((str(id_).lstrip("-"), name, total_messages))
return info
def get_group_messages_statistic(ids):
loop = asyncio.get_event_loop()
stats = loop.run_until_complete(group_messages_statistic(ids))
table = PrettyTable()
table.field_names = ['ID', 'Group', 'Messages']
table.align['ID'] = 'c'
table.align['Group'] = 'c'
table.align['Messages'] = 'l'
table.sortby = 'Messages'
table.reversesort = True
for group in stats:
if group[2] == 0:
continue
table.add_row(list(group))
print(table)
async def delete_messages(ids):
await client.start()
await client.get_dialogs()
for id_ in ids:
chat = await client.get_entity(int(id_))
messages = await client.get_messages(chat, limit=None, from_user='me')
for message in messages:
await message.delete()
def delete(ids):
loop = asyncio.get_event_loop()
loop.run_until_complete(delete_messages(ids))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
usage='%(prog)s [-s [id id ...]] [-d [id id ...]]',
description='Show stats for your messages in all/specified group chats\nor delete all your messages in specified group chat(-s).',
epilog="""\
Examples:
1.Show stats for all your group chats:
./telegram_message_cleaner.py -s
+------------+---------------+----------+
| ID | Group | Messages |
+------------+---------------+----------+
| 1300197304 | Work Group | 234 |
| 1210787553 | Bob and Alice | 75 |
| 1274948231 | Project | 68 |
+------------+---------------+----------+
2.Show stats only for 'Work Group' chat:
./telegram_message_cleaner.py -s 1300197304
+------------+---------------+----------+
| ID | Group | Messages |
+------------+---------------+----------+
| 1300197304 | Work Group | 234 |
+------------+---------------+----------+
3.Show stats only for 'Bob and Alice' and 'Project' chats:
./telegram_message_cleaner.py -s 1210787553 1274948231
+------------+---------------+----------+
| ID | Group | Messages |
+------------+---------------+----------+
| 1210787553 | Bob and Alice | 75 |
| 1274948231 | Project | 68 |
+------------+---------------+----------+
4.Delete messages from 'Bob and Alice' chat:
./telegram_message_cleaner.py -d 1210787553
5.Delete messages from 'Bob and Alice', 'Project' chats:
./telegram_message_cleaner.py -d 1210787553 1274948231""")
parser.add_argument('-s', '--statistic', nargs='*', default=None,
metavar='id', help='if you do not specify ID as an argument, it will show stats of your messages for all group chats')
parser.add_argument('-d', '--delete', nargs='*', default=None,
metavar='id', help='deletes all your messages from the specified group chat ID(-s)')
if len(sys.argv) <= 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
if args.statistic or args.statistic == []:
get_group_messages_statistic(args.statistic)
elif args.delete:
delete(args.delete)