コード例 #1
0
    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)
コード例 #2
0
ファイル: TeleInviter.py プロジェクト: ozdude68/TeleInviter
    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)
コード例 #3
0
    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)
コード例 #4
0
    def _load_and_save_participants_from_destination_group(self):
        fn.print_title('\nLOAD & SAVE PARTICIPANTS from DESTINATION_GROUP.')
        fn.stdout_text('  "%s" ...' % self._destination_group.username)

        participants = self._client.get_participants(self._destination_group,
                                                     aggressive=True)

        fn.print_success(' %d members.' % len(participants))

        i = 0
        for u in participants:
            if u.bot is False:
                self._db.save_invite(u)
                i += 1
                fn.stdout_warning('\r    %d saved.' % i)
                fn.stdout_text(' not include any bot.')
                sys.stdout.flush()
        print()
コード例 #5
0
    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
コード例 #6
0
    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)
コード例 #7
0
    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