kxcoze

satie_get_contacts

Dec 22nd, 2022
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. import csv
  2.  
  3. from telethon import TelegramClient
  4. # pip install telethon
  5.  
  6. api_id = 123456789
  7. api_hash = '0123456789abcdef0123456789abcdef'
  8.  
  9. client = TelegramClient('anon', api_id, api_hash)
  10.  
  11. async def main():
  12.     me = await client.get_me()
  13.     print(f'Привет, {me.first_name}!')
  14.     channel_name = input('Введите название канала, без "@": ')
  15.     al = await client.get_dialogs()
  16.     try:
  17.         participants = await client.get_participants(channel_name)
  18.     except:
  19.         print('Invalid channel_name passed! Try again!')
  20.         raise SystemExit(1)
  21.  
  22.     with open('users.csv', 'w', newline='') as file:
  23.         fieldnames = ['Имя пользователя', 'Имя', 'Фамилия', 'Номер телефона']
  24.         csvwriter = csv.DictWriter(file, fieldnames=fieldnames)
  25.         csvwriter.writeheader()
  26.         for participant in participants:
  27.             print('Username:', participant.username)
  28.             print('First name:', participant.first_name)
  29.             print('Last name:', participant.last_name)
  30.             print('Phone number:', participant.phone, end='\n\n')
  31.             csvwriter.writerow({'Имя пользователя': participant.username,
  32.                                 'Имя': participant.first_name,
  33.                                 'Фамилия': participant.last_name,
  34.                                 'Номер телефона': participant.phone})
  35.  
  36. with client:
  37.     client.loop.run_until_complete(main())
  38.  
Advertisement
Add Comment
Please, Sign In to add comment