Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from telethon.tl.types import InputChannel, InputPeerChat
- from telethon import TelegramClient
- from telethon.tl.functions.channels import GetParticipantsRequest, JoinChannelRequest
- from telethon.tl.functions.messages import ImportChatInviteRequest
- from telethon.tl.types import ChannelParticipantsRecent, ChannelParticipantsSearch, Channel, Chat
- from telethon.errors.rpc_error_list import UserAlreadyParticipantError, FloodWaitError, InviteHashInvalidError
- from telethon.tl.functions.messages import GetFullChatRequest
- from os import path
- from time import sleep
- import re
- import random
- ###########################################
- api_id = 177852
- api_hash = '71a9ac62d0f349458d464c09eff832e8'
- phone = '+79237927536'
- username = 'uname'
- links_file = 'links.txt'
- result_file = 'result.txt'
- SPLITTER = '#'
- ###########################################
- current_path = path.dirname(path.realpath(__file__))
- join_links = []
- try:
- with open(path.join(current_path,'fails.txt'), 'r'):
- pass
- except Exception:
- with open(path.join(current_path,'fails.txt'), 'w'):
- pass
- client = TelegramClient(username, api_id, api_hash)
- def auth():
- client.connect()
- if not client.is_user_authorized():
- client.send_code_request(phone)
- try:
- client.sign_in(phone, input('Enter the code: '))
- except:
- client.sign_in(password=input('Password: '))
- def get_links(file=links_file):
- global join_links
- with open(file, 'r') as o:
- join_links = o.readlines()
- def get_participants(ch):
- all_participants = []
- if isinstance(ch, Channel):
- offset = 0
- limit = 100
- # print('[getting participants... loop]')
- while True:
- participants = client(GetParticipantsRequest(
- ch, ChannelParticipantsSearch(''), offset, limit, hash=0
- ))
- if not participants.users:
- break
- all_participants.extend(participants.users)
- offset += len(participants.users)
- all_participants = [str(x.id) for x in all_participants]
- elif isinstance(ch, Chat):
- a = client(GetFullChatRequest(ch.id))
- # for i in a.participants:
- # print(i)
- print(a)
- participants = a.full_chat.participants.participants
- # print(participants)
- for i in participants:
- all_participants.append(i)
- all_participants = [str(x.id) for x in all_participants]
- else:
- print(type(ch), 'not supported')
- return all_participants
- def work():
- if join_links:
- a = random.randint(1, 3)
- print('[Flood protection; waiting %i seconds]' % a)
- sleep(a)
- for _, join_link in enumerate(join_links):
- join_link = join_link.strip()
- join_link, msg, file, caption = join_link.split(SPLITTER)
- link_hash = join_link.rsplit('/')[-1]
- print('[Processing %s]' % link_hash)
- if 'joinchat' not in join_link:
- print('[The invite hash is invalid]', link_hash)
- continue
- try:
- client(ImportChatInviteRequest(link_hash))
- print('[Succefully joined %s]' % link_hash)
- except FloodWaitError as f:
- error = str(f)
- try:
- t = int(re.findall(r'A wait of (\d+?) seconds is required', error)[0])
- except:
- pass
- else:
- print('[Flood protection; waiting %i seconds]' % t)
- sleep(t)
- except InviteHashInvalidError:
- print('[The invite hash is invalid]', link_hash)
- continue
- except UserAlreadyParticipantError:
- print('[Already joined %s]' % link_hash)
- except Exception as e:
- print(e, link_hash)
- continue
- try:
- channel = client.get_entity(join_link)
- participants = get_participants(channel)
- participants.insert(0, str(channel.id))
- res = ','.join(participants)
- if msg:
- client.send_message(channel, msg)
- if file:
- client.send_file(channel, file, caption=caption)
- except ValueError as v:
- with open(path.join(current_path,'fails.txt'), 'a') as fails:
- fails.write(join_link.strip()+'\n')
- print('[%s failed by flood protection caused often joins; see fails.txt]' % link_hash)
- continue
- except Exception as e:
- print(e, link_hash)
- continue
- with open(path.join(current_path, result_file), 'a') as o:
- o.write(res.strip() + '\n')
- print('%i/%i [Finished %s]\n' % (_ + 1, len(join_links), link_hash))
- a = random.randint(1, 3)
- print('[Flood protection; waiting %i seconds]' % a)
- sleep(a)
- else:
- exit('[No links provided]')
- if __name__ == '__main__':
- auth()
- get_links()
- work()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement