-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
56 lines (48 loc) · 1.51 KB
/
main.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
from telethon import TelegramClient, events, functions
import time
import json
import asyncio
async def main():
api_id = int(input('Api ID =>'))
api_hash = input('Api Hash =>')
client = TelegramClient('client', api_id, api_hash)
await client.start()
dialogs = await client.get_dialogs()
data = { "users": [], "channels": [], "groups": [], "unsaved": { "channels": [], "groups": [] } }
for dialog in dialogs:
if dialog.is_user and not (dialog.name == '' and not dialog.entity.phone and not dialog.entity.phone):
data['users'].append({
"name": dialog.name,
"username": dialog.entity.username,
"phone": dialog.entity.phone,
"bot": dialog.entity.bot
})
elif dialog.is_group:
try:
result = await client(functions.messages.ExportChatInviteRequest(dialog.entity.id))
data['groups'].append({
"name": dialog.name,
"title": dialog.entity.title,
"link": result.link
})
except:
data['unsaved']['groups'].append({
"name": dialog.name,
})
elif dialog.is_channel:
if not dialog.entity.username:
data['unsaved']['channels'].append({
"name": dialog.name
})
else:
data['channels'].append({
"name": dialog.name,
"username": dialog.entity.username,
"link": f'https://t.me/{dialog.entity.username}',
"participants_count": dialog.entity.participants_count
})
filename = f'backup-{round(time.time())}.json'
with open(filename, "w") as fs:
fs.write(json.dumps(data, indent=4))
print(f'Saved into: {filename}')
asyncio.run(main())