def set_source_groups(self, group_keys): """Set `source_groups` for this client Confirm the groups is exist. Args: group_keys: list Returns: int, number of groups """ self._source_groups = [] fn.print_title('\nsetting "%s" source groups:' % self._client_name) for group_key in group_keys: print(' "%s" ...' % group_key) try: group = self._client.get_entity(group_key) fn.print_success(' %s' % group.title) self._source_groups.append(group) except telethon.errors.rpcerrorlist.InviteHashInvalidError as e: fn.stdout_error(' [InviteHashInvalidError] ') fn.print_warning(e) except ValueError as e: fn.stdout_error(' [ValueError] ') fn.print_warning(e) return len(self._source_groups)
def _init_client(self, client_session): """Init a Telegram Client Args: client_session: dict Returns: A TelegramClient Object """ fn.stdout_title('\nLaunching client "%s" ...' % client_session['name']) client = telethon.TelegramClient(client_session['name'], conf.tg_api_id, conf.tg_api_hash, proxy=client_session['proxy']) client.connect() if client.is_user_authorized(): fn.print_success(' DONE') return client else: fn.print_warning(' Login by "%s"' % client_session['phone']) client.send_code_request(client_session['phone']) me = client.sign_in( client_session['phone'], input(colorama.Fore.LIGHTMAGENTA_EX + ' Login code: ')) fn.print_success(' Login for "%s" is SUCCESSFUL' % self._get_user_display_name(me)) if client.is_user_authorized(): return client else: return self._init_client(client_session)
def start(self): if len(self._source_groups) and self._destination_group: fn.print_text('\n------ ------ ------ ------') fn.print_warning('CLIENT "%s":' % self._client_name) fn.print_title( 'START INVITING, from %d source_group(s) to 1 destination_group' % len(self._source_groups)) fn.print_text('------ ------ ------ ------\n') self._load_and_save_participants_from_destination_group() # Start working... for source_group in self._source_groups: fn.print_title('\nIMPORTING PARTICIPANTS from SOURCE_GROUP.') fn.print_text(' #%d / %s' % (source_group.id, source_group.username)) fn.stdout_warning(' %s ...' % source_group.title) participants = self._client.get_participants(source_group, aggressive=True) fn.print_success(' %d members.' % len(participants)) for user in participants: if user.bot is False: if len( self._get_user_display_name(user) ) > conf.filter_user_display_name_too_much_words_limit: # avoid spam, who has a very long name pass elif type( user.status) in conf.filter_user_status_types: # Not UserStatusOffline self._pend_user(user) elif (isinstance(user.status, UserStatusOffline) and self._is_user_status_offline_passed( user.status.was_online)): # UserStatusOffline self._pend_user(user) if len(self._pending_users) > random.randint( conf.rd_pending_users_amount_min, conf.rd_pending_users_amount_max): self._do_batch_invite() return
def _init_client(self, client_session): """Init a Telegram Client Args: client_session: dict Returns: A TelegramClient Object """ fn.stdout_title('\nLaunching client "%s" ...' % client_session['name']) client = telethon.TelegramClient(client_session['name'], conf.tg_api_id, conf.tg_api_hash, proxy=client_session['proxy']) client.connect() #from pprint import pprint #pprint(vars(client)) #print(client) if client.is_user_authorized(): fn.print_success(' DONE') return client else: fn.print_warning(' Login by "%s"' % client_session['phone']) client.send_code_request(client_session['phone']) flagsentCode = 0 sentCode = 0 if flagsentCode == 0: print("askedCode") for line in sys.stdin: sentCode = (line[:-1]) #sentCode me = client.sign_in(client_session['phone'], sentCode) fn.print_success(' Login for "%s" is SUCCESSFUL' % self._get_user_display_name(me)) if client.is_user_authorized(): return client else: return self._init_client(client_session)
def _do_batch_invite(self): fn.print_warning('\n INVITE %d users:' % len(self._pending_users)) # call the roll for user in self._pending_users: fn.print_text(' #.%d > %s' % (user.id, self._get_user_console_name(user))) # batch invite fn.stdout_warning(' BATCH INVITE...') updates = self._client( InviteToChannelRequest(self._destination_group, self._pending_users)) if len(updates.users): fn.print_success( ' %d/%d DONE' % (len(updates.users) - 1, len(self._pending_users))) # save invite to database for user in updates.users: if user.id != self._me.id: fn.print_success( ' #.%d > %s' % (user.id, self._get_user_console_name(user))) self._db.save_invite(user) # clear pool self._pending_users = [] # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' ... waiting %d secs ...' % sleeping_secs) time.sleep(sleeping_secs) else: print() for user in self._pending_users: self._do_one_invite(user) pass
def set_destination_group(self, key): """Set `destination_group` for this client Confirm the group is exist, and joined, and have the right to add a member. Args: group_keys: list Returns: int, number of groups """ fn.print_title('\nsetting "%s" destination group:' % self._client_name) fn.stdout_text(' "%s" ...' % key) # error when session user is banned try: group = self._client.get_entity(key) print(group) # Join if not IN. if group.left: self._client(JoinChannelRequest(group)) fn.print_warning(' JOINED') else: fn.print_success(' IN') # Democracy if group.democracy: # All members can add members # fn.print_success(' %s' % group.title) self._destination_group = group return True else: # Only admins can add members if (group.admin_rights is not None and group.admin_rights.invite_users): #fn.print_success(' %s' % group.title) self._destination_group = group return True else: fn.stdout_error(' Have NO admin right to add a member.') fn.print_warning(' destination group invalid.') return False except ValueError as e: fn.print_error(' ERROR') fn.print_error(' [ValueError] %s' % e) fn.print_warning(' Please make sure "%s" is NOT banned' % self._client_name)
def _do_one_invite(self, user): fn.stdout_text(' INVITE user.#%d > %s...' % (user.id, self._get_user_console_name(user))) # Invite try: self._client( InviteToChannelRequest(self._destination_group, [user])) # save to database self._db.save_invite(user) # shows done fn.stdout_success(' DONE') # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except ValueError as e: fn.print_error('\n [ValueError] > ') fn.print_text(e) # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' ...waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except telethon.errors.rpcerrorlist.UserPrivacyRestrictedError: fn.stdout_error(' error.#0: UserPrivacyRestrictedError') self._db.save_user_privacy_restricted(user) # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except telethon.errors.rpcerrorlist.ChatAdminRequiredError: fn.stdout_error(' error.#1: ChatAdminRequiredError') # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except telethon.errors.rpcerrorlist.ChatIdInvalidError: fn.stdout_error(' error.#2: ChatIdInvalidError') # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except telethon.errors.rpcerrorlist.InputUserDeactivatedError: fn.stdout_error(' error.#3: InputUserDeactivatedError') # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except telethon.errors.rpcerrorlist.PeerIdInvalidError: fn.stdout_error(' error.#4: PeerIdInvalidError') # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except telethon.errors.rpcerrorlist.UserAlreadyParticipantError: fn.stdout_error(' error.#5: UserAlreadyParticipantError') # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except telethon.errors.rpcerrorlist.UserIdInvalidError: fn.stdout_error(' error.#6: UserIdInvalidError') # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except telethon.errors.rpcerrorlist.UserNotMutualContactError: fn.stdout_error(' error.#7: UserNotMutualContactError') self._db.save_user_not_mutual(user, self._area_code) # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except telethon.errors.rpcerrorlist.UsersTooMuchError: fn.stdout_error(' error.#8: UsersTooMuchError') # CPU sleep sleeping_secs = random.randint(conf.rd_sleep_min, conf.rd_sleep_max) fn.print_info(' waiting %d secs...' % sleeping_secs) time.sleep(sleeping_secs) except telethon.errors.rpcerrorlist.PeerFloodError as e: fn.stdout_error(' error.#9: PeerFloodError') fn.print_warning(' %s' % e) raise TeleErrors.PeerFloodError