Tell me, please, can you somehow get a list of all participants in a certain chat in Telegram? Interested in precisely "useame" users.
question@mail.ru
·
01.01.1970 03:00
Can I get a list of all chat participants in Telegram?
answer@mail.ru
·
01.01.1970 03:00
Yes, perhaps I will attach my own script that copes with this task,
I will also attach the code here with explanations., in case of repository deletion:
We make the necessary imports:
import sysfrom getpass import getpassfrom time import sleepe# pip install telethon==0.11.5from telethon import TelegramClientfrom telethon.errors importpan class="">from telethon.errors import SessionPasswordNeededErrorfrom telethon.errors.rpc_errors_400 import/span> UseameNotOccupiedErrorfrom telethon.errors.rpc_errors_420 import FloodWaitErrorfrom telethon.tl.functions.channels import GetParticipantsRequestfrom telethon.tl.functions.contacts import.functions.contacts import ResolveUseameRequestfrom telethon.tl.types import ChannelParticipantsSearch, InputChannelNext, we need to create an account here and fill in the following fields:
api_id = 0api_hash = "phone =pan>phone = ''limit = 100Now we need to log in.:
def main(): # requass=""># requesting chat channel_name = input('Input a channel name, without ""@"": ') client = TelegramClient('current-session', api_id, api_hash) client.connect() # checking if you are already logged in # as you can see above, we created a session called current-session # after the first authorization, you can use it if if not client.is_user_authorized(): try: # sending confirmation code client.send_code_request(phone) client.sign_in(phone, coden client.sign_in(phone, code=input('Enter code: ')) # sometimes telegram blocks access to +- 80k sec # so be careful and don't send too many requests except FloodWaitError as fluderror: printrror: print('Flood wait: {}.'.format(fluderror)) sys.exit() #checking if the user has a password # if so, requesting it and logging in exceptexcept SessionPasswordNeededError: client.sign_in(password=getpass('Enter password:')) #I'll tell you about this function below dump_users(get_chat_info(channel_name, client), client)We are starting to collect users, for this we need the chat ID and access_hash as in the documentation:
def /span> get_chat_info(useame, client): try: chat = client(ResolveUseameRequest(useame)) exceptass="">except UseameNotOccupiedError: print('Chat/channel not found!') sys.exit() result = { 'chat_id': chat.peer.channel_id, 'access_hash': chat.chats[0].access_hash } returetu resultWe have received the chat data., it remains to collect users:
def/span> dump_usersp_users(chat, client):ran>): counter = 0s="">0 offset = 0 #we need to make a chat object, as stated in the documentathe documentation chat_object = InputChannel(chat['chat_id'], chat['access_hash']) all_participants = [] while>while True: # here we get users # we can't get them all at once, so we need offset participants = client.invoke(GetParticipantsRequest( chat_object, ChannelParticipantsSearch("), offset, limit )) # if there are no users left, i.e. we have collected all, exit if="">if not participants.users: break all_participants.extend(['end(['{} {}'.format(x.id, x.useame) for/span> x in participants.users]) users_users_count = len(participants.users) #increasing offset by the number of users we have collected offset += users_count counter += users_count print('{} users collected'.format(counter)) # do not forget do not forget to delay to avoid blocking sleep(2) # save to a file with">with open('users.txt', 'w') as file: file.write('\n'.join('\n'.join(map(str, all_participants))) We get a cozy text editor in the format: