feat: use id instead of name and predefined value instead of iterate over dialogs

This commit is contained in:
tiennm99 committed 2026-02-26 11:39:13 +07:00
1 parent 685711d7b8
commit 1073e06d15
3 files changed
+17 -14

No files matched your search

+1
View File
@@ -1,6 +1,7 @@
PHONE=+1234567890
API_ID=123456789
API_HASH=0123456789abcdef0123456789abcdef
GROUP_IDS=1234567890,9876543210
PROXY_HOST=127.0.0.1
PROXY_PORT=8080
PROXY_SECRET=00000000000000000000000000000000
+2 -1
View File
@@ -12,4 +12,5 @@ proxy_host = os.getenv('PROXY_HOST')
proxy_port = int(os.getenv('PROXY_PORT', '0')) if os.getenv('PROXY_PORT') else None
proxy_secret = os.getenv('PROXY_SECRET')
group_names_to_export = ['ZingPlay Game Studios', 'ZPS HCM', 'ZPS HCM - Xin nghỉ (phép/đi trễ)'] # Add the group names you want to export here
group_ids_str = os.getenv('GROUP_IDS', '')
group_ids = [int(id.strip()) for id in group_ids_str.split(',') if id.strip()] if group_ids_str else []
+14 -13
View File
@@ -2,6 +2,7 @@ import getpass
from telethon.errors import SessionPasswordNeededError
from telethon.sync import TelegramClient
from telethon.tl.types import Chat, Channel
import TelethonFakeTLS
from common import *
@@ -25,16 +26,16 @@ if not client.is_user_authorized():
client.start(phone)
dialogs = client.get_dialogs()
for dialog in dialogs:
if not dialog.is_group:
print('skip ' + dialog.name + ' because it is not a group.')
continue
if dialog.name not in group_names_to_export:
print('skip ' + dialog.name + ' because it is not in the list of group names to export.')
continue
print('exporting ' + dialog.name)
members = client.get_participants(dialog)
members.sort(key=lambda x: x.id)
export_csv(dialog.name, members)
print('export ' + dialog.name + ' done.')
for group_id in group_ids:
try:
entity = client.get_entity(group_id)
if not isinstance(entity, (Chat, Channel)):
print(f'skip {group_id} because it is not a group.')
continue
print(f'exporting {entity.title} (ID: {group_id})')
members = client.get_participants(entity)
members.sort(key=lambda x: x.id)
export_csv(entity.title, members)
print(f'export {entity.title} done.')
except Exception as e:
print(f'error accessing group {group_id}: {e}')