diff --git a/.env.example b/.env.example index 49481f5..9bbab1d 100644 --- a/.env.example +++ b/.env.example @@ -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 diff --git a/config.py b/config.py index 85e56b2..b8002c1 100644 --- a/config.py +++ b/config.py @@ -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 [] diff --git a/main.py b/main.py index 1d4ba2c..d2e311d 100644 --- a/main.py +++ b/main.py @@ -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}')