예제 #1
0
def Init():
    print("Getting sim card list.. ")
    sim_cards = CreateSimList()

    main_phone = sim_cards[0]
    print(f"Connecting to main phone: {main_phone.name}")
    first_client = TelegramClient(main_phone.phone, main_phone.ID,
                                  main_phone.access_hash)
    first_client.connect()

    print("Getting group entities..")
    scrape_group = first_client.get_entity(scrape_link)
    first_client(JoinChannelRequest(scrape_group))
    target_group_entity = first_client.get_entity(target_link)
    target_id = target_group_entity.id

    print("Scraping group lists..")
    scrape_participants = first_client.get_participants(scrape_group,
                                                        aggressive=True)
    target_participants = first_client.get_participants(target_group_entity,
                                                        aggressive=True)

    filtered_participants = []
    final_participants = []

    print("Filtering From Groups..")
    for user in scrape_participants:
        if user.username is not None and not user.bot:  # if has a username and not a bot
            if not user.deleted:  # if it isn't a deleted account
                if hasattr(
                        user.status, 'was_online'
                ) and start_date < user.status.was_online < end_date:
                    filtered_participants.append(user)
                elif type(user.status) == UserStatusOnline or type(
                        user.status) == UserStatusRecently:
                    filtered_participants.append(user)

    for user1 in filtered_participants:
        flag = True
        for user2 in target_participants:
            if user1.id == user2.id:
                flag = False
        if flag:
            final_participants.append(user1)

    first_client.disconnect()
    del first_client

    return sim_cards, final_participants, target_id
예제 #2
0
    def ritorna_lista_membri_gruppo(self, chat_id, username_gruppo):

        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        client = TelegramClient('session', API_ID, API_HASH)
        client.start(bot_token=TOKEN)
        try:
            dati = list(client.get_participants(username_gruppo, aggressive=True))
        except:
            self.bot.sendMessage(chat_id,"C'è stato un problema con il bot si prega di riprovare")
            client.disconnect()
            loop.stop()
            loop.close()
            return []

        lista_gruppo=[]
        for utente in dati:
            if not utente.bot:
                user=self.ritorna_utente(chat_id, utente.id, utente.first_name)
                lista_gruppo.append(user)

        client.disconnect()
        loop.stop()
        loop.close()
        return lista_gruppo
예제 #3
0
def add_user_to_channel(client: TelegramClient, user: types.User, channel) -> bool:
    # Get user entity from channel participants list
    users = list(filter(
        lambda u: u.id == user.id,
        client.get_participants(channel, search=utils.get_display_name(user))
    ))
    
    if users:
        status = "Already"


    else:
        try:
            client(channels.InviteToChannelRequest(
                channel,
                [ user ]
            ))

            status = "Done"

        except errors.FloodWaitError as e:
            status = f"Wait {e.seconds} seconds. Skipped!"

        except errors.RPCError as e:
            status = e.__class__.__name__

        except:
            status = "UnexpectedError"
            traceback.print_exc()


    channel_entity = client.get_entity(channel)
    print_output(client, channel_entity, user, status)

    if status in [
        "Done",
        "Already",
        "UserChannelsTooMuchError",
        "UsersTooMuchError",
	    "UserNotMutualContactError",
	    "UserKickedError",
        "UserBannedInChannelError",
        "ChatAdminRequiredError",
        "UserPrivacyRestrictedError"
    ]:
        return True
    else:
        return False
예제 #4
0
                                               phone,
                                               is_admin=True)
                    if to_channel_entity.broadcast:
                        main_client.edit_admin(to_channel,
                                               phone,
                                               is_admin=True)

                    while user_rows:
                        row = user_rows.pop(0)
                        user_id = int(row[0])
                        name = row[1]

                        user = list(
                            filter(
                                lambda u: u.id == user_id,
                                client.get_participants(from_channel,
                                                        search=name)))
                        if not user:
                            print_output(client,
                                         to_channel,
                                         status="Could not find user")
                            continue

                        user = user[0]
                        success = add_user_to_channel(client, user, to_channel)

                        if success:
                            time.sleep(5)
                        else:
                            user_rows.insert(0, row)
                            break
예제 #5
0
class Telegram:
    USER_TYPE_ADMIN = 'admin'
    USER_TYPE_CREATOR = 'creator'
    USER_TYPE_PARTICIPANT = 'participant'
    USER_TYPE_SELF = 'myself'
    USER_TYPE_UNKNOWN = 'unknown'

    USER_STATUS_EMPTY = ''
    USER_STATUS_RECENTLY = 'recently'
    USER_STATUS_ONLINE = 'online'
    USER_STATUS_OFFLINE = 'offline'
    USER_STATUS_LAST_WEEK = 'last week'
    USER_STATUS_LAST_MONTH = 'last month'
    USER_STATUS_UNKNOWN = 'unknown'

    _client: TelegramClient = None
    phone_number: str = None
    _api_id: int = None
    _api_hash: str = None
    _me: User = None

    def connect(self, phone_number, api_id, api_hash):
        self.phone_number = phone_number
        self._api_id = api_id
        self._api_hash = api_hash

        self._client = TelegramClient(phone_number, api_id, api_hash)
        self._client.connect()
        self._me = self._client.get_me()

    def list_channels(self,
                      only_admin=False,
                      list_from_date=None,
                      limit_size=200):
        channels = []
        result = []

        dialogs_result = self._client(
            GetDialogsRequest(offset_date=list_from_date,
                              offset_id=0,
                              offset_peer=InputPeerEmpty(),
                              limit=limit_size,
                              hash=0))
        channels.extend(dialogs_result.chats)

        for channel in channels:
            if not isinstance(channel, Chat) \
                    and not isinstance(channel, ChannelForbidden) \
                    and not isinstance(channel, ChatForbidden):
                # List all channels or only channels for which we have admin privileges
                if not only_admin or channel.admin_rights or channel.creator:
                    result.append(channel)

        return result

    def list_groups(self, list_from=None, limit_size=200):
        groups = []
        chats_result = []

        result = self._client(
            GetDialogsRequest(offset_date=list_from,
                              offset_id=0,
                              offset_peer=InputPeerEmpty(),
                              limit=limit_size,
                              hash=0))
        groups.extend(result.chats)

        for group in groups:
            if not isinstance(group, ChatForbidden) and not isinstance(
                    group, ChannelForbidden):
                if hasattr(group, 'megagroup') and group.megagroup:
                    chats_result.append(group)

        return chats_result

    @staticmethod
    def channel_participant_type(channel_participant):
        if isinstance(channel_participant, ChannelParticipant):
            return Telegram.USER_TYPE_PARTICIPANT
        elif isinstance(channel_participant, ChannelParticipantCreator):
            return Telegram.USER_TYPE_CREATOR
        elif isinstance(channel_participant, ChannelParticipantAdmin):
            return Telegram.USER_TYPE_ADMIN
        elif isinstance(channel_participant, ChannelParticipantSelf):
            return Telegram.USER_TYPE_SELF
        else:
            return Telegram.USER_TYPE_UNKNOWN

    @staticmethod
    def identify_user_status(channel_participant):
        user_status = channel_participant.status

        if isinstance(user_status, UserStatusEmpty) or user_status is None:
            return Telegram.USER_STATUS_EMPTY
        elif isinstance(user_status, UserStatusRecently):
            return Telegram.USER_STATUS_RECENTLY
        elif isinstance(user_status, UserStatusOnline):
            return Telegram.USER_STATUS_ONLINE
        elif isinstance(user_status, UserStatusOffline):
            return Telegram.USER_STATUS_OFFLINE
        elif isinstance(user_status, UserStatusLastWeek):
            return Telegram.USER_STATUS_LAST_WEEK
        elif isinstance(user_status, UserStatusLastMonth):
            return Telegram.USER_STATUS_LAST_MONTH
        else:
            return Telegram.USER_STATUS_UNKNOWN

    def full_user_info(self, user_id):
        return self._client(GetFullUserRequest(user_id))

    def is_user_authorized(self):
        return self._client.is_user_authorized()

    def request_authorization_code(self):
        self._client.send_code_request(self.phone_number)

    def verify_authorization_code(self, authorization_code):
        self._client.sign_in(self.phone_number, authorization_code)

    def get_channel_participants(self, target_group):
        return self._client.get_participants(target_group, aggressive=True)

    def download_profile_photo(self, user, to_file):
        return self._client.download_profile_photo(user, to_file)
예제 #6
0
파일: manualadd.py 프로젝트: mk175/Genisys
        user['group_id'] = row[4]
        users.append(user)
    f.close()
f = open('resume.txt', 'r')
scraped = f.readline()
f.close()
tg_group = str(input(f'{INPUT}{g} Enter group username to add[Without @]: '))
group = 't.me/' + tg_group
time.sleep(1.5)
target_group = client.get_entity(group)
entity = InputPeerChannel(target_group.id, target_group.access_hash)
group_name = target_group.title
n = 0
print(f'{info}{g} Getting entities{rs}\n')
target_m = client.get_entity(scraped)
client.get_participants(target_m, aggressive=True)
print(f'{info}{g} Adding members to {group_name}{rs}\n')
added_users = []
for user in users:
    added_users.append(user)
    n += 1
    if n % 50 == 0:
        print(f'{sleep}{g} Sleep 2 min to prevent possible account ban{rs}')
        time.sleep(120)
    try:
        print(user)
        user_to_add = client.get_entity(user['id'])
        client(InviteToChannelRequest(entity, [user_to_add]))
        us_id = user['id']
        print(f'{attempt}{g} Adding {us_id}{rs}')
        print(f'{sleep}{g} Sleep 20s{rs}')
예제 #7
0
        code = input(f'{INPUT}{lg} Enter the code for {phone}{r}: ')
        client.sign_in(phone, code)
    except PhoneNumberBannedError:
        print(f'{error}{r}{phone} is banned!{rs}')
        print(f'{error}{r} Run manager.py to filter them{rs}')
        sys.exit()
username = input(
    f'{INPUT}{lg} Enter the exact username of the public group[Without @]: {r}'
)
target_grp = 't.me/' + str(username)
group = client.get_entity(target_grp)
time = datetime.datetime.now().strftime("%H:%M")
print('\n' + info + lg + ' Started on ' + str(time) + rs)
print(f'{info}{lg} Scraping members from {rs}' + str(group.title))
members = []
members = client.get_participants(group, aggressive=True)
print(f'{info}{lg} Saving in members.csv...{rs}')
select = str(input(f'{INPUT}{lg} Do you wanna filter active users?[y/n]: '))
with open("members.csv", "w", encoding='UTF-8') as f:
    writer = csv.writer(f, delimiter=",", lineterminator="\n")
    writer.writerow(
        ['username', 'user id', 'access hash', 'group', 'group id'])
    if select == 'y':
        for member in members:
            accept = True
            if not member.status == UserStatusRecently():
                accept = False
            if accept:
                if member.username:
                    username = member.username
                else:
예제 #8
0
class TelegramInterfaceCLI:

    debug = None
    output_format = None
    output_filename = None
    telegram_client = None

    telegram_api_id = None
    telegram_api_hash = None
    telegram_api_phone =None

    __chat_objects_by_id_hack = {}

    def __init__(self, config_filename, output_filename=None, output_format='json', debug=False):

        if debug:
            loglevel_env_override = '{}_LOGLEVEL'.format(NAME.replace('_', '').replace(' ', '').upper())
            os.environ[loglevel_env_override] = 'debug'

        global logger
        logger = TelegramInterfaceCLILogger().logger
        logger.info(NAME)
        logger.info('version {}'.format(VERSION))

        global config
        try:
            tgi_config = TelegramInterfaceCLIConfig(config_filename)
            config = tgi_config.config
            logger.debug('config_filename: {}'.format(tgi_config.config_filename))
        except TelegramInterfaceCLIException as e:
            logger.warn(e)
            config = {}

        self.telegram_api_id = os.environ.get('telegram_api_id', None)
        if self.telegram_api_id is None:
            if 'telegram_api_id' in config:
                self.telegram_api_id = config['telegram_api_id']
            else:
                logger.warn('Unable to set "telegram_api_id" value from env or config!')

        self.telegram_api_hash = os.environ.get('telegram_api_hash', None)
        if self.telegram_api_hash is None:
            if 'telegram_api_hash' in config:
                self.telegram_api_hash = config['telegram_api_hash']
            else:
                logger.warn('Unable to set "telegram_api_hash" value from env or config!')

        self.telegram_api_phone = os.environ.get('telegram_api_phone', None)
        if self.telegram_api_phone is None:
            if 'telegram_api_phone' in config:
                self.telegram_api_phone = config['telegram_api_phone']
            else:
                logger.warn('Unable to set "telegram_api_phone" value from env or config!')

        self.output_format = output_format
        self.output_filename = output_filename

        logger.debug('output_format: {}'.format(self.output_format))
        logger.debug('output_filename: {}'.format(self.output_filename))
        logger.debug('telegram_api_id: {}'.format(self.telegram_api_id))
        logger.debug('telegram_api_hash: {}'.format(self.telegram_api_hash))
        logger.debug('telegram_api_phone: {}'.format(self.telegram_api_phone))

    def main(self, data_filename=None, groups=False, users=False):

        if data_filename is None:
            ts = str(datetime.datetime.utcnow()).split('.')[0].replace(' ', 'Z').replace('-', '').replace(':', '')
            data_filename = '{}-{}.json'.format(self.telegram_api_phone, ts)

        if os.path.isfile(data_filename):
            logger.info('Loading data file: {}'.format(data_filename))
            with open(data_filename, 'r') as f:
                telegram_data = json.load(f)
        else:
            logger.info('Saving to data file: {}'.format(data_filename))

            session_filename = None
            if 'session_filename' in config:
                session_filename = config['session_filename']

            try:
                connect_telegram = self.connect_telegram(session_filename=session_filename)
            except ValueError as e:
                logger.error(e)
                return

            if connect_telegram is False:
                logger.error('Failed connecting to Telegram')
                return

            telegram_data = {'chat_groups': self.get_chat_groups(expansions=['users'])}

            with open(data_filename, 'w') as f:
                json.dump(telegram_data, f)

        if users is True and groups is False:
            output_data = self.extract_users(telegram_data)
        elif users is False and groups is True:
            output_data = self.extract_groups(telegram_data)
        else:
            output_data = self.extract_groups(telegram_data, users_expansion=True)
        self.output(output_data)
        return

    def output(self, data):
        if self.output_format.lower() == 'csv':
            out = self.flatten_to_csv(data)
        else:
            out = json.dumps(data, indent=2)

        if self.output_filename == '-' or self.output_filename is None:
            print(out)
        else:
            with open(self.output_filename, 'w') as f:
                f.write(out)
            logger.info('Output written to filename: {}'.format(self.output_filename))

    def extract_users(self, telegram_data):
        users_list = []
        users_id_list = []
        for group in self.extract_groups(telegram_data, users_expansion=True):
            for user in group['users']:
                if user['id'] not in users_id_list:
                    users_id_list.append(user['id'])
                    users_list.append(user)
        return users_list

    def extract_groups(self, telegram_data, users_expansion=False):
        groups_list = []
        groups_id_list = []
        for chat_group in telegram_data['chat_groups']:
            if chat_group['id'] not in groups_id_list:
                groups_id_list.append(chat_group['id'])
                users_list = []
                if users_expansion is True:
                    for user in chat_group['users']:
                        users_list.append({
                            'id': user['id'],
                            'username': user['username'],
                            'firstname': user['first_name'],
                            'lastname': user['last_name'],
                            'phone': user['phone'],
                        })
                    groups_list.append({
                        'id': chat_group['id'],
                        'name': chat_group['title'],
                        'users': users_list
                    })
                else:
                    groups_list.append({
                        'id': chat_group['id'],
                        'name': chat_group['title']
                    })
        return groups_list

    def connect_telegram(self, session_filename=None):

        if session_filename is None:
            session_filename = '__CWD__/{}.session'.format(self.telegram_api_phone).replace('__CWD__', os.getcwd())
        else:
            session_filename = os.path.expanduser(session_filename)
        logger.info('Session filename: {}'.format(session_filename))

        self.telegram_client = TelegramClient(session_filename, self.telegram_api_id, self.telegram_api_hash)
        self.telegram_client.connect()

        if not self.telegram_client.is_user_authorized():
            self.telegram_client.send_code_request(self.telegram_api_phone)
            self.telegram_client.sign_in(
                self.telegram_api_phone,
                input('Enter the MFA code provided to you in the Telegram application: ')
            )
        logger.info('Connected to Telegram with telegram_api_id: {}'.format(self.telegram_api_id))
        return True

    def get_chat_groups(self, expansions=None, limit=9999):
        if expansions is None:
            expansions = []

        chat_channels = self.get_chats_by_attribute(attribute='participants_count', limit=limit)

        if 'users' in expansions:
            for channel_index, channel in enumerate(chat_channels):
                channel_id = str(channel['id'])
                chat_channels[channel_index]['users'] = self.get_chat_users(self.__chat_objects_by_id_hack[channel_id])

        return chat_channels

    def get_chat_users(self, chat_channel_object):
        try:
            channel_users = self.telegram_client.get_participants(chat_channel_object)
            return self.cast_jsonable(channel_users)
        except ChatAdminRequiredError as e:
            logger.warn('Failed get users from {}, admin privilege required'.format(chat_channel_object.title))
            return list()


    def get_chats_by_attribute(self, attribute, limit=10):
        chats = []
        result = self.telegram_client(
            GetDialogsRequest(
                offset_date=None,
                offset_id=0,
                offset_peer=InputPeerEmpty(),
                limit=limit,
                hash=0
            )
        )

        result_count = 0
        if hasattr(result, 'chats'):
            result_count = len(result.chats)

        if result_count > 0:
            for chat in result.chats:
                if hasattr(chat, attribute):
                    self.__chat_objects_by_id_hack[str(chat.id)] = chat
                    chats.append(self.cast_jsonable(chat))
        return chats

    def cast_jsonable(self, obj, __depth=0, __depth_limit=8):

        if __depth >= __depth_limit:
            return '<< OBJECT DEPTH LIMIT >>'

        if obj is None or type(obj) in [int, float, str, bool]:
            return obj

        if type(obj) is list or 'List' in type(obj).__name__:
            result = []
            for item in obj:
                result.append(self.cast_jsonable(item, __depth+1))
            return result

        if not hasattr(obj, '__dict__'):
            return obj.__str__()

        result = {}
        for attribute in obj.__dict__:
            result[attribute] = self.cast_jsonable(obj.__dict__[attribute], __depth+1)
        return result

    def flatten_to_csv(self, obj, delimiter='.'):
        flat_obj = self.__flatten_object(obj, delimiter=delimiter)

        data = []
        data_row = {}
        data_row_keys = []
        data_row_last = None
        line_number_previous = -1
        for flat_key in flat_obj:
            key = self.__flattened_key_parse(flat_key, method='key', delimiter=delimiter)
            line_number = self.__flattened_key_parse(flat_key, method='line', delimiter=delimiter)
            if line_number != line_number_previous:
                if data_row:
                    data.append(copy.copy(data_row))
                line_number_previous = line_number
            data_row[key] = flat_obj[flat_key]
            if key not in data_row_keys:
                data_row_keys.append(key)
            data_row_last = data_row
        data.append(copy.copy(data_row_last))

        # return json.dumps(data, indent=2)

        def __csv_row(list_items, char='"', end='\n'):
            return char + '{char},{char}'.format(char=char).join(str(x) for x in list_items) + char + end

        csv = __csv_row(data_row_keys)
        for row in data:
            row_list = []
            for data_row_key in data_row_keys:
                if data_row_key in row:
                    row_list.append(row[data_row_key])
                else:
                    row_list.append('')
            csv += __csv_row(row_list)
        return csv.rstrip('\n')

    def __flatten_object(self, obj, parent_key='', delimiter='.'):
        items = []
        if type(obj) is list:
            for list_index, value in enumerate(obj):
                new_key = '{}{}{}'.format(parent_key, delimiter, str(list_index)) if parent_key else str(list_index)
                if type(value) in (str, int, float, bool):
                    items.append((new_key, value))
                else:
                    items.extend(self.__flatten_object(value, new_key, delimiter=delimiter).items())
        elif type(obj) is dict:
            for key, value in obj.items():
                new_key = '{}{}{}'.format(parent_key, delimiter, key) if parent_key else key
                if type(value) in (str, int, float, bool) or value is None:
                    items.append((new_key, value))
                else:
                    items.extend(self.__flatten_object(value, new_key, delimiter=delimiter).items())
        else:
            raise TelegramInterfaceCLIException('Unsupported object type encountered while attempting to __flatten_object()')
        return dict(items)

    def __flattened_key_parse(self, flat_key, method='key', delimiter='.'):
        if method.lower() == 'key':
            key = ''
            for flat_key_part in flat_key.split(delimiter):
                if not flat_key_part.isdigit():
                    key = '{}{}{}'.format(key, delimiter, flat_key_part) if key else flat_key_part
            return key
        else:
            flat_key_part_numbers = []
            for flat_key_part in flat_key.split(delimiter):
                if flat_key_part.isdigit():
                    flat_key_part_numbers.append(int(flat_key_part) + 1)
            return reduce((lambda x, y: x * y), flat_key_part_numbers)
예제 #9
0
class TelethonBaseScrapper:
    global_config = get_config()
    client = None
    app_api_id = app_api_hash = app_phone = None

    def __init__(self):
        self.set_login_info()

    def get_session_name(self):
        # return self.session_name
        return self.app_phone

    def set_login_info(self):
        self.app_api_id = get_config()['TG']['APP_API_ID']
        self.app_api_hash = get_config()['TG']['APP_API_HASH']
        self.app_phone = get_config()['TG']['APP_PHONE']
        return self

    def set_app_client(self):
        self.client = TelegramClient(self.get_session_name(), self.app_api_id,
                                     self.app_api_hash)
        self.client.start()
        self.client.connect()
        return self

    def is_auth(self):
        dump('is_auth')
        if not self.client.is_user_authorized():
            self.client.send_code_request(self.get_session_name())
            self.client.sign_in(self.get_session_name(),
                                input('Enter the code: '))

    def get_groups(self):
        dump('get_groups')
        chats = []
        groups = []
        last_date = None
        chunk_size = 200

        result = self.client(
            GetDialogsRequest(offset_date=last_date,
                              offset_id=0,
                              offset_peer=InputPeerEmpty(),
                              limit=chunk_size,
                              hash=0))
        chats.extend(result.chats)

        for chat in chats:
            dump(chat)
            groups.append(chat)
            # try:
            #     if chat.megagroup is True:
            #         channels.append(chat)
            # except:
            #     channels.append(chat)
            #     continue

        i = 0
        for c in groups:
            print(str(i) + '- ' + c.title + ' - ' + str(c.id))
            i += 1

        return groups

    def get_users(self, group):
        dump('get_users')
        users = self.client.get_participants(group, aggressive=True)

        # 資料加入 channel_id
        new_users = []
        for user in users:
            # 字典合併
            new_user = {
                **user.to_dict(),
                **{
                    'group_id': group.id,
                    'group_name': group.title
                }
            }
            new_users.append(new_user)

        return new_users
예제 #10
0
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
  client.send_code_request(phone)
  code = input('Enter the code: ')
  try:
    client.sign_in(phone, code = code)
  except SessionPasswordNeededError:
    password = getpass.getpass('Enter tfa password: '******'megagroup') and g.megagroup]

print('Choose a group to scrape members from:', file = sys.stderr)
for i, g in enumerate(groups): print(f'{i}- {g.title}', file = sys.stderr)
print('Enter a Number: ', file = sys.stderr)
target_group = groups[int(input())]

writer = csv.writer(sys.stdout, dialect = 'excel-tab', lineterminator = '\n')
for user in client.get_participants(target_group, aggressive = True):
  writer.writerow([user.id, user.username, user.first_name, user.last_name])
예제 #11
0
# requesting user to select a group to scrap members in it
count = 0
print('[*] Select a group to scrap ')
# listing all available groups
for tmp in group:
    print('\t[-]' + str(count) + '-' + tmp.title)
    count = count + 1

# getting group index
group_index = input('[*] Preferred group index: ')
group_selected = group[int(group_index)]

# scraping members of selected group
print('[*] Fetching members. . . .')
all_members = []
all_members = client.get_participants(group_selected, aggressive=True)


def loading():
    print('loading...')

    for i in range(1, 101):
        time.sleep(0.1)
        print("\u001b[400D" + str(i) + '%', flush=True, end='')
    print()


# writing all members info into a CSV file
print('[*] Writing to file. . . . .')
loading()
예제 #12
0
for chat in chats:
    try:
        if chat.megagroup == True:
            groups.append(chat)
    except:
        continue
print('Choose a group to scrape members')

for i, g in zip(range(len(groups)), groups):
    print(str(i) + '- ' + g.title)

group_index = input("Enter a Number: ")
group = groups[int(group_index)]

all_participants = []
all_participants = client.get_participants(group)

print('======================Adding Members to file==================')

members_file = open("members.txt", 'a')

user_count = 0
for user in all_participants:
    if user.username == None:
        continue
    members_file.write(user.username)
    user_count += 1
    members_file.write("\n")
members_file.close()

print('{} Members scraped successfully.'.format(user_count))
예제 #13
0
def scrapper():
    if os.path.isdir('/Program Files/Telegram') == False:
        os.mkdir('/Program Files/Telegram')
    try:
        if os.path.isfile(os.path.join(os.getcwd(), 'config.json')) == True:
            pass
        else:
            config = {}
            config['api_id'] = '0'
            config['api_hash'] = 'your api hash'
            config['phone'] = '+0'
            with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                json.dump(config, config_file, indent=2)

        session = False
        for item in os.listdir():
            if '.session' in item:
                number = item.split('.')[0]
                session = True

        if session:
            while True:
                a = input(f'Do you want to recover your login session with number {number}? [y/n] ').lower()
                if a == 'y':
                    print('Program Started...')
                    with open(os.path.join(os.getcwd(), 'config.json'), 'r') as config_file:
                        config = json.load(config_file)
                        api_id = config['api_id']
                        api_hash = config['api_hash']
                        phone = config['phone']
                    break
                elif a == 'n':
                    for item in os.listdir():
                        if '.session' in item:
                            os.remove(item)
                    print('Program Started...')
                    api_id = input('Paste here your account api id: ')
                    api_hash = input('Paste here your account api hash: ')
                    phone = input('Paste here your phone number (International Format): ')
                    config = {}
                    config['api_id'] = api_id
                    config['api_hash'] = api_hash
                    config['phone'] = phone
                    with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                        json.dump(config, config_file, indent=2)
                    break

        else:
            print('No session found. Lets define a new one...')
            api_id = input('Paste here your account api id: ')
            api_hash = input('Paste here your account api hash: ')
            phone = input('Paste here your phone number (International Format): ')
            config = {}
            config['api_id'] = api_id
            config['api_hash'] = api_hash
            config['phone'] = phone
            with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                json.dump(config, config_file, indent=2)

        # ========================== FIXING BUGS ================================
        with open(os.path.join(os.getcwd(), 'config.json'), 'r') as config_file:
            config = json.load(config_file)

            if api_id == '0':
                api_id = input('Paste here your account api id: ')
                config['api_id'] = api_id
                with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                    json.dump(config, config_file, indent=2)

            if api_hash == 'your api hash':
                api_hash = input('Paste here your account api hash: ')
                config['api_hash'] = api_hash
                with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                    json.dump(config, config_file, indent=2)
            if phone == '+0':
                phone = input('Paste here your phone number (International Format): ')
                config['phone'] = phone
                with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                    json.dump(config, config_file, indent=2)

        # ====================== END OF FIXING BUGS ===============================

        client = TelegramClient(phone, api_id, api_hash)
        async def main():
            # Now you can use all client methods listed below, like for example...
            await client.send_message('me', 'Hello !!!!')
        with client:
            client.loop.run_until_complete(main())
        client.connect()
        if not client.is_user_authorized():
            client.send_code_request(phone)
            client.sign_in(phone, input('Enter verification code: '))


        chats = []
        last_date = None
        chunk_size = 200
        groups=[]

        result = client(GetDialogsRequest(
                     offset_date=last_date,
                     offset_id=0,
                     offset_peer=InputPeerEmpty(),
                     limit=chunk_size,
                     hash = 0
                 ))
        chats.extend(result.chats)

        for chat in chats:
            try:
                if chat.megagroup== True:
                    groups.append(chat)
            except:
                continue

        print('Which Group Do You Want To Scrape Members From: ')
        i=0
        for g in groups:
            g.title = g.title.encode('utf-8')
            g.title = g.title.decode('ascii', 'ignore')
            print(str(i) + '- ' + g.title)
            i+=1
            
        g_index = input("Please! Enter a Number: ")
        target_group=groups[int(g_index)]

        print('Fetching Members...')
        all_participants = []
        all_participants = client.get_participants(target_group, aggressive=True)

        print('Saving In file...')
        # with open('/Program Files/Telegram/Scrapped.csv',"w+",encoding='UTF-8') as f:#Enter your file name.
        #     writer = csv.writer(f,delimiter=",",lineterminator="\n")
        #     writer.writerow(['username','user id', 'access hash','name','group', 'group id'])
        #     for user in all_participants:
        #         if user.username:
        #             username= user.username
        #         else:
        #             username= ""
        #         if user.first_name:
        #             first_name= user.first_name
        #         else:
        #             first_name= ""
        #         if user.last_name:
        #             last_name= user.last_name
        #         else:
        #             last_name= ""
        #         name= (first_name + ' ' + last_name).strip()
        #         writer.writerow([username,user.id,user.access_hash,name,target_group.title, target_group.id])

        with open(os.path.join(os.getcwd(), 'Scraped.json'), 'w+') as f:
            users = []
            jsonuser = {}
            for user in all_participants:
                jsonuser.clear()
                if user.username:
                    username= user.username
                else:
                    username= ""
                if user.first_name:
                    first_name= user.first_name
                else:
                    first_name= ""
                if user.last_name:
                    last_name= user.last_name
                else:
                    last_name= ""
                name= (first_name + ' ' + last_name).strip()
                jsonuser['username'] = username
                jsonuser['id'] = user.id
                jsonuser['access_hash'] = user.access_hash
                jsonuser['name'] = name
                users.append(jsonuser.copy())
            json.dump(users, f, indent=2)

        print('Members scraped successfully.......')
        client.disconnect()
        # print('Please, close this window...')
        # time.sleep(10000)

    except Exception as e:
        e = str(e)
        client.disconnect()
        print(e)
        time.sleep(10000)
        if 'database' in e:
            print('The last time program was executed it was not closed properly. Please delete the files ending with .session, and restart the program.')
            time.sleep(10000)
def main():
    chats = []
    last_date = None
    chunk_size = 200
    groups = []
    phone = 'your phone'  # for example +84769556886
    client = TelegramClient(phone, api_id, api_hash)
    client.connect()
    if not client.is_user_authorized():
        client.send_code_request(phone)
        client.sign_in(phone, input('Enter the code:'))

    result = client(
        GetDialogsRequest(offset_date=last_date,
                          offset_id=0,
                          offset_peer=InputPeerEmpty(),
                          limit=chunk_size,
                          hash=0))
    chats.extend(result.chats)
    for chat in chats:
        print(chat)
        try:
            if chat.megagroup == True:
                groups.append(chat)
        except:
            continue
    print('Choose a group to scrape members from:')
    i = 0
    for g in groups:
        print(str(i) + '- ' + g.title)
        i += 1
    g_index = input("Enter a Number: ")
    target_group = groups[int(g_index)]

    print('Fetching Members...')
    all_participants = []
    all_participants = client.get_participants(target_group, aggressive=True)

    print('Saving In file...')

    with open("members.csv", "w", encoding='UTF-8') as f:
        writer = csv.writer(f, delimiter=",", lineterminator="\n")
        writer.writerow([
            'username', 'user id', 'access hash', 'name', 'group', 'group id'
        ])
        for user in all_participants:
            if user.username:
                username = user.username
            else:
                username = ""
            if user.first_name:
                first_name = user.first_name
            else:
                first_name = ""
            if user.last_name:
                last_name = user.last_name
            else:
                last_name = ""
            name = (first_name + ' ' + last_name).strip()
            writer.writerow([
                username, user.id, user.access_hash, name, target_group.title,
                target_group.id
            ])
    print('Members scraped successfully.')
    pass
예제 #15
0
            groups.append(chat)
    except:
        #groups.append(chat)
        continue

i=0

for g in groups:
    try:
        print(str(i) + '-' + str(g.id) + '-' + g.title)
    except UnicodeEncodeError as e:
        print(str(i) + '-' + str(g.id) + '-' + g.title.translate(non_bmp_map))
    i += 1
    print('Fetching Members...')
    all_participants = []
    all_participants = client.get_participants(g, aggressive=True)
    try:
        print(g.title + 'Saving')
    except UnicodeEncodeError as e:
        print(g.title.translate(non_bmp_map) + 'Saving')
    filename = str(g.id) + '-' + g.title.replace(r'*', '').replace(r'/', '').replace(r'\\', '').replace(r'>', '').replace(r'<', '').replace(r'"', '').replace(r':', '').replace(r'?', '').replace(r'|', '') + '.csv'
    with open(filename,"w",encoding='UTF-8') as f:
        writer = csv.writer(f,delimiter=",",lineterminator="\n")
        writer.writerow(['phone','username','user id','access hash','name','group', 'group id', 'time'])
        for user in all_participants:
            if user.username:
                username = user.username
            else:
                username = ""
            if user.first_name:
               first_name = user.first_name
예제 #16
0
def get_members(client: TelegramClient, target_group: Channel, aggressive: bool = True) -> List:
    all_participants = []
    all_participants = client.get_participants(target_group, aggressive=aggressive)
    return all_participants
예제 #17
0
def e():
    try:
        r = c.RawConfigParser()  #Read Group Keys
        r.read('degerler.veri')
        cfg = r['veri']['config_no']
        group_username = r['veri']['kanal_grup_adi']
        ags = r['veri']['aktif_gun_sayisi']
        bsk = r['veri']['beklenicek_sure_kucuk']
        bsb = r['veri']['beklenicek_sure_buyuk']
        intags = int(ags)
        intbsk = int(bsk)
        intbsb = int(bsb)
        cpass = c.RawConfigParser()
        cpass.read(cfg)
    except:
        print("Something is wrong with degerler.veri!!")
    try:
        id = cpass['cred']['id']
        h = cpass['cred']['hash']
        p = cpass['cred']['phone']
        client = TelegramClient(p, id, h)
    except KeyError:
        print("[!] Api access id phone or hash is not correct!!\n")
        sys.exit(1)
    client.connect()
    if not client.is_user_authorized():
        client.send_code_request(p)
        client.sign_in(p, input('[+] Enter code sent from telegram : '))
    client(JoinChannelRequest(channel=group_username))
    chats = []
    last_date = None
    chunk_size = 200
    groups = []
    result = client(
        GetDialogsRequest(offset_date=last_date,
                          offset_id=0,
                          offset_peer=InputPeerEmpty(),
                          limit=chunk_size,
                          hash=0))
    chats.extend(result.chats)
    for chat in chats:
        try:
            check = client.get_entity(group_username)
            if check.id == chat.id:
                groups.append(chat)
        except:
            continue
    i = 0
    for group in groups:
        print('[' + str(i) + ']' '-' + group.title)
        i += 1
    g_index = 0
    aktif_gun_sayisi = intags
    target_group = groups[int(g_index)]
    all_participants = []
    time.sleep(random.randrange(11, 23))
    all_participants = client.get_participants(
        target_group
    )  #patched because telegram seems to ban while using it! , aggressive=True)
    with open("users.nya", "w", encoding='UTF-8') as f:
        writer = csv.writer(f, delimiter=",", lineterminator="\n")
        writer.writerow([
            'username', 'user id', 'access hash', 'name', 'group', 'group id'
        ])
        for user in all_participants:
            #contacts = client(GetContactsRequest(0))
            #client(AddChatUserRequest(chat_id=chat.id,user_id=user.id,fwd_limit=10))
            accept = True
            try:
                lastDate = user.status.was_online
                ay_kontrolu = (datetime.now().month - lastDate.month)
                gun_kontrolu = (datetime.now().day - lastDate.day)
                if (ay_kontrolu > 0 or gun_kontrolu > aktif_gun_sayisi
                        or datetime.now().year != lastDate.year):
                    accept = False
            except:
                pass
            if (accept):
                if user.username:
                    username = user.username
                else:
                    username = ""
                if user.first_name:
                    first_name = user.first_name
                else:
                    first_name = ""
                if user.last_name:
                    last_name = user.last_name
                else:
                    last_name = ""
                name = (first_name + ' ' + last_name).strip()
                writer.writerow([
                    username, user.id, user.access_hash, name,
                    target_group.title, target_group.id
                ])
    client(LeaveChannelRequest(channel=group_username))
    os._exit(1)
예제 #18
0
for chat in chats:
    try:
        if chat.megagroup == True:
            groups.append(chat)
    except:
        continue

print(gr + '\n\n[+] Choose a group to add members:\n' + ye)
i = 0
j = 0
for group in groups:
    print(gr + '[' + cy + str(i) + gr + ']' + cy + ' - ' + group.title)
    i += 1
g_index = input(gr + "\n[+] Enter a Number: " + cy)
target_group = groups[int(g_index)]
my_participants = client.get_participants(target_group)
my_participants_id = []
for my_participant in my_participants:
    my_participants_id.append(my_participant.id)

target_group_entity = InputPeerChannel(target_group.id,
                                       target_group.access_hash)

mode = int(input(gr + "Enter 1 to add by username or 2 to add by ID: " + cy))

n = 0

for user in users:
    n += 1
    if n % 50 == 0:
        print(re + "Added too many users. Let me sleep for 60 seconds...")
예제 #19
0
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s',
                    level=logging.WARNING)


#LOAD API & BOT SECRETS
with open("/home/bot/voting_bot/discourse-voting-bot-telegram/telegram-secrets.json", 'r') as secrets_file:
    secret = json.load(secrets_file)

api_id = secret["id"]
api_hash = secret["hash"]
bot_token = secret["token"]

# We have to manually call "start" if we want an explicit bot token
bot = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)

admins = bot.get_participants(chat, filter=ChannelParticipantsAdmins)
@bot.on(events.NewMessage(from_users=admins,pattern='/start'+'@'+bot.get_me().username))
async def start(event):
    """Send a message when the command /start is issued."""
    await event.respond('Hi!')
    raise events.StopPropagation

@bot.on(events.NewMessage(pattern='/help'+'@'+bot.get_me().username))
async def start(event):
    """Send a message when the command /help is issued."""
    await event.respond("The following commands are at your disposal: /start, /tip and /balance \n \n Initiating command /tip has a specfic format,\n use it like so:" + "\n \n Parameters: \n <user> = target user to tip \n <amount> = amount of votes to send \n \n Tipping format: \n /tip <user> <amount>")
    raise events.StopPropagation

@bot.on(events.NewMessage(pattern='/tip'+'@'+bot.get_me().username))
async def start(event):
    """Send a message when the command /tip is issued."""
예제 #20
0
def addermulti():
    g_scrape_id = ''
    g_id = ''

    def add_credentials():
        if os.path.isfile(os.path.join(os.getcwd(),
                                       'multiconfig.json')) == True:
            with open(os.path.join(os.getcwd(), 'multiconfig.json'),
                      'r') as config_file:
                data = json.load(config_file)

                print(f'Credentials found, for {len(data)} Telegram accounts')
                for cred in data:
                    print(
                        f'Checking if two factor authentication is already done with {cred["phone"]}'
                    )
                    client = TelegramClient(cred['phone'], cred['api_id'],
                                            cred['api_hash'])

                    async def main():
                        # Now you can use all client methods listed below, like for example...
                        await client.send_message('me', 'Hello !!!!!')

                    with client:
                        client.loop.run_until_complete(main())
                    client.connect()
                    if not client.is_user_authorized():
                        print(
                            f'Sending request code to {cred["phone"]}, please authenticate'
                        )
                        client.send_code_request(phone)
                        client.sign_in(cred["phone"], input('40779'))
                    else:
                        print(
                            f'Good! Client {cred["phone"]} is authenticated, I can use it.'
                        )
                    client.disconnect()

            print(f'Credentials found, for {len(data)} Telegram accounts')
            while True:
                question = input(
                    'Do you want to use these credentials?[y/n] ').lower()
                if question == 'y':
                    break
                elif question == 'n':
                    print('Good, lets define new credentials...')
                    ammount_of_credentials = int(
                        input('How many accounts do you want to add?'))
                    credentials = []
                    for i in range(ammount_of_credentials):
                        phone = input('Type the phone number: ')
                        api_id = input(f'Type {phone} api id: ')
                        api_hash = input(f'Type {phone} api hash: ')

                        config = {}
                        config['api_id'] = api_id
                        config['api_hash'] = api_hash
                        config['phone'] = phone
                        credentials.append(config.copy())

                        with open(
                                os.path.join(os.getcwd(), 'multiconfig.json'),
                                'w') as config_file:
                            json.dump(credentials, config_file, indent=2)

                        client = TelegramClient(phone, api_id, api_hash)

                        async def main():
                            # Now you can use all client methods listed below, like for example...
                            await client.send_message('me', 'Hello !!!!!')

                        with client:
                            client.loop.run_until_complete(main())
                        client.connect()
                        if not client.is_user_authorized():
                            print(
                                f'Sending request code to {phone}, please authenticate'
                            )
                            client.send_code_request(phone)
                            client.sign_in(phone, input('40779'))
                        else:
                            print(
                                f'Good! Client {phone} is authenticated, I can use it.'
                            )
                        client.disconnect()
                    break
        else:
            print('No credentials found. Lets define new ones')
            ammount_of_credentials = int(
                input('How many accounts do you want to add?'))
            credentials = []
            for i in range(ammount_of_credentials):
                phone = input('Type the phone number: ')
                api_id = input(f'Type {phone} api id: ')
                api_hash = input(f'Type {phone} api hash: ')

                config = {}
                config['api_id'] = api_id
                config['api_hash'] = api_hash
                config['phone'] = phone
                credentials.append(config.copy())

                with open(os.path.join(os.getcwd(), 'multiconfig.json'),
                          'w') as config_file:
                    json.dump(credentials, config_file, indent=2)

                client = TelegramClient(phone, api_id, api_hash)

                async def main():
                    # Now you can use all client methods listed below, like for example...
                    await client.send_message('me', 'Hello !!!!!')

                with client:
                    client.loop.run_until_complete(main())
                client.connect()
                if not client.is_user_authorized():
                    print(
                        f'Sending request code to {phone}, please authenticate'
                    )
                    client.send_code_request(phone)
                    client.sign_in(phone, input('40779'))
                else:
                    print(
                        f'Good! Client {phone} is authenticated, I can use it.'
                    )
                client.disconnect()

    add_credentials()

    with open(os.path.join(os.getcwd(), 'multiconfig.json'),
              'r') as config_file:
        credentials = json.load(config_file)

    for config in credentials:
        api_id = config['api_id']
        api_hash = config['api_hash']
        phone = config['phone']

        AMMOUNT_OF_FLOOD_ERROS = 0
        AMMOUNT_OF_USERS_ADDED = 0
        print(f'Now trying use account {phone}')

        try:
            client = TelegramClient(phone, api_id, api_hash)

            async def main():
                # Now you can use all client methods listed below, like for example...
                await client.send_message('me', 'Hello !!!!!')

            SLEEP_TIME_1 = 100
            SLEEP_TIME_2 = 100
            with client:
                client.loop.run_until_complete(main())
            client.connect()
            if not client.is_user_authorized():
                client.send_code_request(phone)
                client.sign_in(phone, input('40779'))

            chats = []
            last_date = None
            chunk_size = 200
            groups = []

            result = client(
                GetDialogsRequest(offset_date=last_date,
                                  offset_id=0,
                                  offset_peer=InputPeerEmpty(),
                                  limit=chunk_size,
                                  hash=0))
            chats.extend(result.chats)

            for chat in chats:
                try:
                    if chat.megagroup == True:
                        groups.append(chat)
                except:
                    continue

            try:
                print('Which Group Do You Want To Scrape Members From: ')
                i = 0

                for g in groups:
                    g.title = g.title.encode('utf-8')
                    g.title = g.title.decode('ascii', 'ignore')
                    print(f'[Group]: {str(g.title)} [Id]: {str(g.id)}')
                    i += 1

                if g_scrape_id == '':
                    g_scrape_id = input(
                        "Please! Enter the group to scrape id: ").strip()

                for group in groups:
                    if str(group.id) == g_scrape_id:
                        target_group_scrape = group

                print('Fetching Members...')
                all_participants_to_scrape = []
                all_participants_to_scrape = client.get_participants(
                    target_group_scrape, aggressive=True)

                print('Saving In file...')

                with open(os.path.join(os.getcwd(), 'Scraped.json'),
                          'w+') as f:
                    users = []
                    jsonuser = {}
                    for user in all_participants_to_scrape:
                        jsonuser.clear()
                        if user.username:
                            username = user.username
                        else:
                            username = ""
                        if user.first_name:
                            first_name = user.first_name
                        else:
                            first_name = ""
                        if user.last_name:
                            last_name = user.last_name
                        else:
                            last_name = ""
                        name = (first_name + ' ' + last_name).strip()
                        jsonuser['username'] = username
                        jsonuser['id'] = user.id
                        jsonuser['access_hash'] = user.access_hash
                        jsonuser['name'] = name
                        users.append(jsonuser.copy())
                    json.dump(users, f, indent=2)

                print('Members scraped successfully.......')

                users = []
                with open(os.path.join(os.getcwd(), 'Scraped.json'),
                          "r",
                          encoding='utf-8',
                          errors='ignore') as f:
                    list = json.load(f, strict=False)
                    for dict in list:
                        user = {}
                        user['username'] = dict['username']
                        user['id'] = dict['id']
                        user['access_hash'] = dict['access_hash']
                        user['name'] = dict['name']
                        users.append(user)

            except Exception as e:
                print(e)

            print('Choose a group to add members:')
            i = 0
            for group in groups:
                group.title = group.title.encode('utf-8')
                group.title = group.title.decode('ascii', 'ignore')
                print(f'[Group]: {str(group.title)} [Id]: {str(group.id)}')
                i += 1

            if g_id == '':
                g_id = input("Enter the group Id: ")

            for group in groups:
                if g_id == str(group.id):
                    target_group = group

            #Start of scrappe members from that group to avoid repetition

            try:
                all_participants = []
                all_participants = client.get_participants(target_group,
                                                           aggressive=True)

                scrapedusers = []
                jsonuser = {}
                for user in all_participants:
                    jsonuser.clear()
                    if user.username:
                        username = user.username
                    else:
                        username = ""
                    if user.first_name:
                        first_name = user.first_name
                    else:
                        first_name = ""
                    if user.last_name:
                        last_name = user.last_name
                    else:
                        last_name = ""
                    name = (first_name + ' ' + last_name).strip()
                    jsonuser['username'] = username
                    jsonuser['id'] = user.id
                    jsonuser['access_hash'] = user.access_hash
                    jsonuser['name'] = name
                    scrapedusers.append(jsonuser.copy())

                print('Members scraped successfully.......')
            except:
                print(
                    'Error scrapping members of this group. Danger of false positives.'
                )

            #End of scrappe members of that group to avoid repetition

            target_group_entity = InputPeerChannel(target_group.id,
                                                   target_group.access_hash)

            # mode = int(input("Enter 1 to add by username or 2 to add by ID: "))
            mode = 2

            n = 0

            try:
                with open(os.path.join(os.getcwd(), 'tried.json'),
                          'r') as file:
                    tried = json.load(file)
            except:
                tried = []

            for user in users:
                if AMMOUNT_OF_FLOOD_ERROS > 10:
                    print(
                        'UPS, GOT 10 FLOOD ERRORS, SWITCHING TO THE NEXT ACCOUNT'
                    )
                    break
                if AMMOUNT_OF_USERS_ADDED >= 45:
                    print(
                        'GREAT! ADDED 45 USERS WITH THIS NUMBER TODAY. SWITCHING TO THE NEXT ACCOUNT'
                    )
                    break
                if user not in scrapedusers:
                    if user not in tried:
                        tried.append(user.copy())
                        with open(os.path.join(os.getcwd(), 'tried.json'),
                                  'w+') as file:
                            json.dump(tried, file, indent=2)
                        try:
                            n += 1
                            if n % 80 == 0:
                                sleep(60)
                            try:
                                print("Trying to add user {}".format(
                                    user['id']))
                                if mode == 1:
                                    if user['username'] == "":
                                        continue
                                    user_to_add = client.get_input_entity(
                                        user['username'])
                                elif mode == 2:
                                    user_to_add = InputPeerUser(
                                        user['id'], user['access_hash'])
                                else:
                                    sys.exit(
                                        "Invalid Mode Selected. Please Try Again."
                                    )
                                client(
                                    InviteToChannelRequest(
                                        target_group_entity, [user_to_add]))
                                print("Waiting for 60-180 Seconds...")
                                time.sleep(random.randrange(60, 90))
                            except PeerFloodError:
                                AMMOUNT_OF_FLOOD_ERROS += 1
                                print(
                                    "Getting Flood Error from telegram. Script is stopping now. Please try again after some time."
                                )
                                print(
                                    "Waiting {} seconds".format(SLEEP_TIME_2))
                                time.sleep(SLEEP_TIME_2)
                                continue  #continues adicionado por mim
                            except UserPrivacyRestrictedError:
                                print(
                                    "The user's privacy settings do not allow you to do this. Skipping."
                                )
                                print("Waiting for 5 Seconds...")
                                time.sleep(random.randint(
                                    0, 5))  #Alterei, antes era randrange(5,0)
                                continue  # adicionado por mim
                            except UserNotMutualContactError:
                                continue
                            except UserChannelsTooMuchError:
                                print(
                                    'This user is already in too many channels/supergroups.'
                                )
                                continue
                            except Exception as e:
                                # traceback.print_exc()
                                print(f"Unexpected Error: {e}")
                                continue
                            AMMOUNT_OF_USERS_ADDED += 1

                            try:
                                with open(
                                        os.path.join(os.getcwd(),
                                                     'added.json'),
                                        'r') as file:
                                    added = json.load(file)
                                    added.append(user.copy())
                            except:
                                added = []

                            with open(os.path.join(os.getcwd(), 'added.json'),
                                      'w+') as file:
                                json.dump(added, file, indent=2)
                                try:
                                    print(
                                        f'User {user["name"]} with id: {user["id"]} has been sucessfully added to your group.'
                                    )
                                except UnicodeEncodeError:
                                    print(
                                        f'User with id: {user["id"]} has been sucessfully added your group.'
                                    )

                        except Exception as e:
                            print(f'An unnespected error ocureed: {e}')

                    else:
                        print(
                            f'This user has been checked by me before. Skipping. If you want o erase data, delete "tried.json".'
                        )
                else:
                    print('This user already is in this group. Skipping.')
        except Exception as e:
            e = str(e)
            print(e)
            try:
                client.disconnect()
            except:
                print('Unable to disconnect client')
                time.sleep(30000)
            if 'database' in e:
                print(
                    'The last time program was executed it was not closed properly. Please delete the .session files and restart the program.'
                )
                time.sleep(30000)
                try:
                    client.disconnect()
                except:
                    print('Unable to disconnect client')

        try:
            client.disconnect()
        except:
            print('Unable to disconnect client')
예제 #21
0
def start(config):
    try:
        api_id = config['api_id']
        session_name = config["session_name"]
        api_hash = config["api_hash"]
        phone_number = config['phone_number']
    
        client = TelegramClient(session_name, api_id, api_hash)
    except KeyError:
        print("Unable to get Telegram developer id.\nPlease register for telegram developer.")
        sys.exit(1)    
    client.connect()
    client.start()
    if not client.is_user_authorized():
        client.send_code_request(phone_number)
        client.sign_in(phone_number, input('Enter the code: '))

    chats = []
    last_date = None
    chunk_size = 200
    groups=[]
 
    result = client(GetDialogsRequest(
             offset_date=last_date,
             offset_id=0,
             offset_peer=InputPeerEmpty(),
             limit=chunk_size,
             hash = 0
         ))
    chats.extend(result.chats)
    for chat in chats:
        try:
            if chat.megagroup== True:
                groups.append(chat)
        except:
            continue
    print('Choose a group to scrape members :')
    i=0
    for g in groups:
        print('['+str(i)+']'+' - '+ g.title)
        i+=1
 
    print('')
    g_index = input("Enter a Number : ")
    target_group=groups[int(g_index)]
 
    print('Fetching Members...')
    time.sleep(1)
    all_participants = []
    all_participants = client.get_participants(target_group, aggressive=True)
 
    print('Saving In file...')
    time.sleep(1)
    with open("members.csv","w",encoding='UTF-8') as f:
        writer = csv.writer(f,delimiter=",",lineterminator="\n")
        writer.writerow(['username','user_id', 'access_hash','name','group', 'group_id','status'])
        for user in all_participants:
            if user.username:
                username= user.username
            else:
                username= ""
            if user.first_name:
                first_name= user.first_name
            else:
                first_name= ""
            if user.last_name:
                last_name= user.last_name
            else:
                last_name= ""
            name= (first_name + ' ' + last_name).strip()
            writer.writerow([username,user.id,user.access_hash,name,target_group.title, target_group.id, 'N'])      
    print('Members scraped successfully.')
예제 #22
0
def scrapper():
    api_id = input('Type your api id: ')
    api_hash = input('Type your api hash: ')
    phone = input('Type your phone number starting with +: ')
    client = TelegramClient(phone, api_id, api_hash)

    async def main():
        # Now you can use all client methods listed below, like for example...
        await client.send_message('me', 'Hello !!!!')

    with client:
        client.loop.run_until_complete(main())
    client.connect()
    if not client.is_user_authorized():
        client.send_code_request(phone)
        client.sign_in(phone, input('Enter verification code: '))

    chats = []
    last_date = None
    chunk_size = 200
    groups = []

    result = client(
        GetDialogsRequest(offset_date=last_date,
                          offset_id=0,
                          offset_peer=InputPeerEmpty(),
                          limit=chunk_size,
                          hash=0))
    chats.extend(result.chats)

    for chat in chats:
        try:
            if chat.megagroup == True:
                groups.append(chat)
        except:
            continue

    print('From Which Group Yow Want To Scrap A Members:')
    i = 0
    for g in groups:
        g.title = g.title.encode('utf-8')
        g.title = g.title.decode('ascii', 'ignore')
        print(str(i) + '- ' + g.title)
        i += 1

    g_index = input("Please! Enter a Number: ")
    target_group = groups[int(g_index)]

    print('Fetching Members...')
    all_participants = []
    all_participants = client.get_participants(target_group, aggressive=True)

    print('Saving In file...')
    with open("Scrapped.csv", "w",
              encoding='UTF-8') as f:  #Enter your file name.
        writer = csv.writer(f, delimiter=",", lineterminator="\n")
        writer.writerow([
            'username', 'user id', 'access hash', 'name', 'group', 'group id'
        ])
        for user in all_participants:
            if user.username:
                username = user.username
            else:
                username = ""
            if user.first_name:
                first_name = user.first_name
            else:
                first_name = ""
            if user.last_name:
                last_name = user.last_name
            else:
                last_name = ""
            name = (first_name + ' ' + last_name).strip()
            writer.writerow([
                username, user.id, user.access_hash, name, target_group.title,
                target_group.id
            ])
    print('Members scraped successfully.......')
예제 #23
0
def scraper():
    Name_banner()
    cpass = configparser.RawConfigParser()
    cpass.read('configScrap.data')
    try:
        api_id = cpass['cred']['id']
        api_hash = cpass['cred']['hash']
        phone = cpass['cred']['phone']
        client = TelegramClient(phone, api_id, api_hash)
    except KeyError:
        Name_banner
        print(re + "[!] Go to menu and install application  !!\n")
        sys.exit(1)
    client.connect()
    if not client.is_user_authorized():
        client.send_code_request(phone)

        Name_banner
        client.sign_in(phone, input(gr + '[+] Enter the code: ' + re))

    os.system('clear')
    Name_banner
    chats = []
    last_date = None
    chunk_size = 500
    groups = []

    result = client(
        GetDialogsRequest(offset_date=last_date,
                          offset_id=0,
                          offset_peer=InputPeerEmpty(),
                          limit=chunk_size,
                          hash=0))
    chats.extend(result.chats)

    for chat in chats:
        try:
            if chat.megagroup == True:
                groups.append(chat)
        except:
            continue

    print(gr + '[+] Choose a group to scrape members :' + re)
    i = 0
    for g in groups:
        print(gr + '[' + cy + str(i) + ']' + ' - ' + g.title)
        i += 1

    print('')
    g_index = input(gr + "[+] Enter a Number : " + re)
    target_group = groups[int(g_index)]

    print(gr + '[+] Fetching Members...')
    time.sleep(1)
    all_participants = []
    all_participants = client.get_participants(target_group, aggressive=True)

    print(gr + '[+] Saving In file...')
    time.sleep(1)
    with open("members.csv", "w", encoding='UTF-8') as f:
        writer = csv.writer(f, delimiter=",", lineterminator="\n")
        writer.writerow([
            'username', 'user id', 'access hash', 'name', 'group', 'group id'
        ])
        for user in all_participants:
            if user.username:
                username = user.username
            else:
                username = ""
            if user.first_name:
                first_name = user.first_name
            else:
                first_name = ""
            if user.last_name:
                last_name = user.last_name
            else:
                last_name = ""
            name = (first_name + ' ' + last_name).strip()
            writer.writerow([
                username, user.id, user.access_hash, name, target_group.title,
                target_group.id
            ])
    os.system('clear')
    print(gr + '[+] Members scraped successfully.  ')
    print(gr + '[+] Wait 5 second....  menu is Opening ')
    menu()
예제 #24
0
        if chat.megagroup:
            groups.append(chat)
    except:
        continue

# print('Choose a group to scrape members from:')
i = 0
for g in groups:
    print(str(i) + '/ ' + g.title)
    print(g)
    i += 1

# g_index = input("Enter a Number: ")
# target_group = groups[int(g_index)]
target_group = groups[0]
main_group = groups[1]

target_group_entity = InputPeerChannel(target_group.id,
                                       target_group.access_hash)

print('Fetching Members...')

participants = client.get_participants(main_group, aggressive=True)

for user in participants:
    print(user)
    user_entity = client.get_input_entity(user)
    user_to_add.append(client.get_input_entity(user))

client(InviteToChannelRequest(target_group_entity, user_to_add))
예제 #25
0
def kaydet():
    nya = c.RawConfigParser()  #Grup keylerini oku
    nya.read('degerler.veri')
    cfg = nya['veri']['config_no']
    satno = nya['veri']['satir_no']
    kissay = nya['veri']['kisi_sayisi']
    group_username = nya['veri']['kanal_grup_adi']
    ags = nya['veri']['aktif_gun_sayisi']
    bsk = nya['veri']['beklenicek_sure_kucuk']
    bsb = nya['veri']['beklenicek_sure_buyuk']
    intsatno = int(satno)
    intkissay = int(kissay)
    intags = int(ags)
    intbsk = int(bsk)
    intbsb = int(bsb)
    cpass = c.RawConfigParser()
    cpass.read(cfg)
    try:
        api_id = cpass['cred']['id']
        api_hash = cpass['cred']['hash']
        phone = cpass['cred']['phone']
        client = TelegramClient(phone, api_id, api_hash)
    except KeyError:
        os.system('cls')
        print("[!] Gereklilikler yuklu degil. !!\n")
        sys.exit(1)
    client.connect()
    if not client.is_user_authorized():
        client.send_code_request(phone)
        client.sign_in(phone, input('[+] Telegramdan gelen kodu gir : '))
    client(JoinChannelRequest(channel=group_username))
    chats = []
    last_date = None
    chunk_size = 200
    groups = []
    result = client(
        GetDialogsRequest(offset_date=last_date,
                          offset_id=0,
                          offset_peer=InputPeerEmpty(),
                          limit=chunk_size,
                          hash=0))
    chats.extend(result.chats)
    for chat in chats:
        try:
            check = client.get_entity(group_username)
            if check.id == chat.id:
                groups.append(chat)
        except:
            continue
    i = 0
    for group in groups:
        print('[' + str(i) + ']' '-' + group.title)
        i += 1
    g_index = 0
    aktif_gun_sayisi = intags
    target_group = groups[int(g_index)]
    all_participants = []
    all_participants = client.get_participants(target_group, aggressive=True)
    with open("grup_uyeleri.csv", "w", encoding='UTF-8') as f:
        writer = csv.writer(f, delimiter=",", lineterminator="\n")
        writer.writerow([
            'username', 'user id', 'access hash', 'name', 'group', 'group id'
        ])
        for user in all_participants:
            accept = True
            try:
                lastDate = user.status.was_online
                ay_kontrolu = (datetime.now().month - lastDate.month)
                gun_kontrolu = (datetime.now().day - lastDate.day)
                if (ay_kontrolu > 0 or gun_kontrolu > aktif_gun_sayisi
                        or datetime.now().year != lastDate.year):
                    accept = False
            except:
                continue
            if (accept):
                if user.username:
                    username = user.username
                else:
                    username = ""
                if user.first_name:
                    first_name = user.first_name
                else:
                    first_name = ""
                if user.last_name:
                    last_name = user.last_name
                else:
                    last_name = ""
                name = (first_name + ' ' + last_name).strip()
                writer.writerow([
                    username, user.id, user.access_hash, name,
                    target_group.title, target_group.id
                ])
    client(LeaveChannelRequest(channel=group_username))
    os._exit(1)
예제 #26
0
client.connect()

# code using client...

with client:
    #client.loop.run_until_complete(main())
    async def join(client):
        # ch = '@andeh_ir'
        try:
            await client(JoinChannelRequest(channel))
            print('[+] Joined The Channel')
        except:
            print('[-] skiped')

    #with TelegramClient('sessionname', api_id, api_hash) as client:
    #   result = client(functions.channels.JoinChannelRequest(
    #      channel='@pruebastienda'
    # ))
    #print(result.stringify())

misusuarios = client.get_participants('@pruebastienda')
while True:
    try:
        update = client.updates.poll()
        if not update:

            continue

        print('I received', update)
    except KeyboardInterrupt:
        break
예제 #27
0
def adder():
    if os.path.isdir('/Program Files/Telegram') == False:
        os.mkdir('/Program Files/Telegram')
    try:
        if os.path.isfile(os.path.join(os.getcwd(), 'added.json')) == True:
            pass
        else:
            added = []
            person = {}

            person['username'] = ''
            person['user_id'] = ''
            added.append(person.copy())

            person['username'] = ''
            person['user_id'] = ''
            added.append(person.copy())

            with open(os.path.join(os.getcwd(), 'added.json'), 'w') as file:
                json.dump(added, file, indent=2)


        if os.path.isfile(os.path.join(os.getcwd(), 'tried.json')) == True:
            pass
        else:
            added = []
            person = {}

            person['username'] = ''
            person['user_id'] = ''
            added.append(person.copy())

            person['username'] = ''
            person['user_id'] = ''
            added.append(person.copy())

            with open(os.path.join(os.getcwd(), 'tried.json'), 'w') as file:
                json.dump(added, file, indent=2)


        if os.path.isfile(os.path.join(os.getcwd(), 'config.json')) == True:
            pass
        else:
            config = {}
            config['api_id'] = '0'
            config['api_hash'] = 'your api hash'
            config['phone'] = '+0'
            with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                json.dump(config, config_file, indent=2)

        session = False
        for item in os.listdir():
            if '.session' in item:
                number = item.split('.')[0]
                session = True

        if session:
            while True:
                a = input(f'Do you want to recover your login session with number {number}? [y/n] ').lower()
                if a == 'y':
                    print('Program Started...')
                    with open(os.path.join(os.getcwd(), 'config.json'), 'r') as config_file:
                        config = json.load(config_file)
                        api_id = config['api_id']
                        api_hash = config['api_hash']
                        phone = config['phone']
                    break
                elif a == 'n':
                    for item in os.listdir():
                        if '.session' in item:
                            os.remove(item)
                    print('Program Started...')
                    api_id = input('Paste here your account api id: ')
                    api_hash = input('Paste here your account api hash: ')
                    phone = input('Paste here your phone number (International Format): ')
                    config = {}
                    config['api_id'] = api_id
                    config['api_hash'] = api_hash
                    config['phone'] = phone
                    with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                        json.dump(config, config_file, indent=2)
                    break

        else:
            print('No session found. Lets define a new one...')
            api_id = input('Paste here your account api id: ')
            api_hash = input('Paste here your account api hash: ')
            phone = input('Paste here your phone number (International Format): ')
            config = {}
            config['api_id'] = api_id
            config['api_hash'] = api_hash
            config['phone'] = phone
            with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                json.dump(config, config_file, indent=2)

        # ========================== FIXING BUGS ================================
        with open(os.path.join(os.getcwd(), 'config.json'), 'r') as config_file:
            config = json.load(config_file)

            if api_id == '0':
                api_id = input('Paste here your account api id: ')
                config['api_id'] = api_id
                with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                    json.dump(config, config_file, indent=2)

            if api_hash == 'your api hash':
                api_hash = input('Paste here your account api hash: ')
                config['api_hash'] = api_hash
                with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                    json.dump(config, config_file, indent=2)
            if phone == '+0':
                phone = input('Paste here your phone number (International Format): ')
                config['phone'] = phone
                with open(os.path.join(os.getcwd(), 'config.json'), 'w') as config_file:
                    json.dump(config, config_file, indent=2)

        # ====================== END OF FIXING BUGS ===============================

        client = TelegramClient(phone, api_id, api_hash)
        async def main():
            # Now you can use all client methods listed below, like for example...
            await client.send_message('me', 'Hello !!!!!')


        SLEEP_TIME_1 = 100
        SLEEP_TIME_2 = 100
        with client:
            client.loop.run_until_complete(main())
        client.connect()
        if not client.is_user_authorized():
            client.send_code_request(phone)
            client.sign_in(phone, input('40779'))

        users = []
        # with open(r"/Program Files/Telegram/Scrapped.csv", encoding='UTF-8') as f:  #Enter your file name
        #     rows = csv.reader(f,delimiter=",",lineterminator="\n")
        #     next(rows, None)
        #     for row in rows:
        #         user = {}
        #         user['username'] = row[0]
        #         user['id'] = int(row[1])
        #         user['access_hash'] = int(row[2])
        #         user['name'] = row[3]
        #         users.append(user)

        with open(os.path.join(os.getcwd(), 'Scraped.json'), "r", encoding='utf-8', errors='ignore') as f:
            list = json.load(f, strict=False)
            for dict in list:
                user = {}
                user['username'] = dict['username']
                user['id'] = dict['id']
                user['access_hash'] = dict['access_hash']
                user['name'] = dict['name']
                users.append(user)

        chats = []
        last_date = None
        chunk_size = 200
        groups = []

        result = client(GetDialogsRequest(
            offset_date=last_date,
            offset_id=0,
            offset_peer=InputPeerEmpty(),
            limit=chunk_size,
            hash=0
        ))
        chats.extend(result.chats)

        for chat in chats:
            try:
                if chat.megagroup == True:
                    groups.append(chat)
            except:
                continue

        print('Choose a group to add members:')
        i = 0
        for group in groups:
            group.title = group.title.encode('utf-8')
            group.title = group.title.decode('ascii', 'ignore')
            print(f'{str(i)} - {str(group.title)}')
            i += 1

        g_index = input("Enter a Number: ")
        target_group = groups[int(g_index)]

        #Start of scrappe members from that group to avoid repetition

        try:
            all_participants = []
            all_participants = client.get_participants(target_group, aggressive=True)

            scrapedusers = []
            jsonuser = {}
            for user in all_participants:
                jsonuser.clear()
                if user.username:
                    username= user.username
                else:
                    username= ""
                if user.first_name:
                    first_name= user.first_name
                else:
                    first_name= ""
                if user.last_name:
                    last_name= user.last_name
                else:
                    last_name= ""
                name= (first_name + ' ' + last_name).strip()
                jsonuser['username'] = username
                jsonuser['id'] = user.id
                jsonuser['access_hash'] = user.access_hash
                jsonuser['name'] = name
                scrapedusers.append(jsonuser.copy())

            print('Members scraped successfully.......')
        except:
            print('Error scrapping members of this group. Danger of false positives.')

        #End of scrappe members of that group to avoid repetition

        target_group_entity = InputPeerChannel(target_group.id, target_group.access_hash)

        # mode = int(input("Enter 1 to add by username or 2 to add by ID: "))
        mode = 2

        n = 0

        with open(os.path.join(os.getcwd(), 'tried.json'), 'r') as file:
            tried = json.load(file)

        for user in users:
            if user not in scrapedusers:
                if user not in tried:
                    tried.append(user.copy())
                    with open(os.path.join(os.getcwd(), 'tried.json'), 'w') as file:
                        json.dump(tried, file, indent=2)
                    try:
                        n += 1
                        if n % 80 == 0:
                            sleep(60)
                        try:
                            print("Trying to add user {}".format(user['id']))
                            if mode == 1:
                                if user['username'] == "":
                                    continue
                                user_to_add = client.get_input_entity(user['username'])
                            elif mode == 2:
                                user_to_add = InputPeerUser(user['id'], user['access_hash'])
                            else:
                                sys.exit("Invalid Mode Selected. Please Try Again.")
                            client(InviteToChannelRequest(target_group_entity, [user_to_add]))
                            print("Waiting for 60-180 Seconds...")
                            time.sleep(random.randrange(60, 90))
                        except PeerFloodError:
                            print("Getting Flood Error from telegram. Script is stopping now. Please try again after some time.")
                            print("Waiting {} seconds".format(SLEEP_TIME_2))
                            time.sleep(SLEEP_TIME_2)
                            continue #continues adicionado por mim
                        except UserPrivacyRestrictedError:
                            print("The user's privacy settings do not allow you to do this. Skipping.")
                            print("Waiting for 5 Seconds...")
                            time.sleep(random.randint(0, 5)) #Alterei, antes era randrange(5,0)
                            continue # adicionado por mim
                        except UserNotMutualContactError:
                            continue
                        except UserChannelsTooMuchError:
                            print('This user is already in too many channels/supergroups.')
                            continue
                        except Exception as e:
                            # traceback.print_exc()
                            print(f"Unexpected Error: {e}")
                            continue

                        with open(os.path.join(os.getcwd(), 'added.json'), 'r') as file:
                            added = json.load(file)
                            added.append(user.copy())

                        with open(os.path.join(os.getcwd(), 'added.json'), 'w') as file:
                            json.dump(added, file, indent=2)
                            try:
                                print(f'User {user["name"]} with id: {user["id"]} has been sucessfully added to your group.')
                            except UnicodeEncodeError:
                                print(f'User with id: {user["id"]} has been sucessfully added your group.')

                    except Exception as e:
                        print(f'An unnespected error ocureed: {e}')

                else:
                    print(f'This user has been checked by me before. Skipping. If you want o erase data, delete "tried.json".')
            else:
                print('This user already is in this group. Skipping.')
    except Exception as e:
        e = str(e)
        print(e)
        try:
            client.disconnect()
        except:
            print('Unable to disconnect client')
            time.sleep(30000)
        if 'database' in e:
            print('The last time program was executed it was not closed properly. Please delete the .session files and restart the program.')
            time.sleep(30000)
            try:
                client.disconnect()
            except:
                print('Unable to disconnect client')

    try:
        client.disconnect()
    except:
        print('Unable to disconnect client')
예제 #28
0
            groups.append(chat)
    except:
        continue
 
print('choose a group to scrape members from:')
i=0
for g in groups:
    print(str(i) + '- ' + g.title)
    i+=1
 
g_index = input("Number Enter kar: ")
target_group=groups[int(g_index)]
 
print('Fetching Members...')
all_participants = []
all_participants = client.get_participants(target_group, aggressive=True)
 
print('member.csv me save ho raha hai...')
with open("member.csv","w",encoding='UTF-8') as f:
    writer = csv.writer(f,delimiter=",",lineterminator="\n")
    writer.writerow(['username','user id', 'access hash','name','group', 'group id'])
    for user in all_participants:
        if user.username:
            username= user.username
        else:
            username= ""
        if user.first_name:
            first_name= user.first_name
        else:
            first_name= ""
        if user.last_name:
예제 #29
0
class TelegramInterface:

    args = None
    name = 'Telegram Interface'
    telegram_client = None
    __chat_objects_by_id_hack = {}

    def __init__(self):
        parser = argparse.ArgumentParser(description=self.name)

        parser.add_argument('-e', '--env', action='store_true', default=False,
                            help='Output the current environment variable values and exit.')

        parser.add_argument('-f', type=str, metavar='<filename>', default=None,
                            help='Data filename to use, if the file already exists it will be loaded as input without '
                                 'connecting to Telegram.  By default auto-generates a filename in the <cwd>.')

        parser.add_argument('-o', type=str, metavar='<filename>', default='-',
                            help='Output filename, by default to <stdout>')

        parser.add_argument('-c', '--csv', action='store_true', default=False,
                            help='Output in flattened CSV format')

        parser.add_argument('-g', action='store_true', default=False,
                            help='Output groups, can be used with -u to obtain users in groups')

        parser.add_argument('-u', action='store_true', default=False,
                            help='Output users')

        self.args = parser.parse_args()
        if self.args.env is False and self.args.g is False and self.args.u is False:
            parser.print_help()
            exit(1)

    def main(self):
        self.message_stderr(self.name, color='grey')

        if self.args.env is True:
            self.output(self.get_environment_variables())
            return

        telegram_data_filename = self.args.f
        if telegram_data_filename is None:
            api_phone = self.get_environment_variables()['telegram_api_phone']
            if not api_phone:
                self.message_stderr('Environment variable "telegram_api_phone" not set!', color='red')
                return
            telegram_data_filename = '{}-{}.json'.format(api_phone, self.timestamp())

        telegram_data = None
        if os.path.isfile(telegram_data_filename):
            self.message_stderr('Loading data file: {}'.format(telegram_data_filename), color='grey')
            with open(telegram_data_filename, 'r') as f:
                telegram_data = json.load(f)
        else:
            self.message_stderr('Saving to data file: {}'.format(telegram_data_filename), color='grey')
            if self.connect_telegram() is False:
                self.message_stderr('Failed connecting to Telegram', color='red')
                return
            telegram_data = {
                'chat_groups': self.get_chat_groups(expansions=['users'])
            }
            with open(telegram_data_filename, 'w') as f:
                json.dump(telegram_data, f)

        output_data = None
        if self.args.u is True and self.args.g is False:
            output_data = self.extract_users(telegram_data)
        elif self.args.u is False and self.args.g is True:
            output_data = self.extract_groups(telegram_data)
        else:
            output_data = self.extract_groups(telegram_data, users_expansion=True)

        self.output(output_data)
        return

    def timestamp(self):
        return str(datetime.datetime.utcnow()).split('.')[0].replace(' ', 'Z').replace('-', '').replace(':', '')

    def get_environment_variables(self):
        return {
            'telegram_api_id': os.environ.get('telegram_api_id', None),
            'telegram_api_hash': os.environ.get('telegram_api_hash', None),
            'telegram_api_phone': os.environ.get('telegram_api_phone', None),
        }

    def output(self, data):
        if self.args.csv is True:
            out = self.flatten_to_csv(data)
        else:
            out = json.dumps(data, indent=2)

        if self.args.o == '-':
            print(out)
        else:
            with open(self.args.o, 'w') as f:
                f.write(out)
            self.message_stderr('Output written to filename: {}'.format(self.args.o), color='grey')

    def message_stderr(self, message, timestamp=True, color='default', end='\n'):

        if color.lower() == 'red':
            color_code = '\x1b[31m'
        elif color.lower() == 'blue':
            color_code = '\x1b[34m'
        elif color.lower() == 'green':
            color_code = '\x1b[32m'
        elif color.lower() == 'yellow':
            color_code = '\x1b[33m'
        elif color.lower() in ['grey', 'gray']:
            color_code = '\x1b[90m'
        elif color.lower() == 'white':
            color_code = '\x1b[97m'
        else:
            color_code = '\x1b[39m'

        if timestamp:
            message = '{} - {}'.format(self.timestamp(), message.strip())

        color_default = '\x1b[0m'

        sys.stderr.write(color_code + message + color_default + end)
        return

    def extract_users(self, telegram_data):
        users_list = []
        users_id_list = []
        for group in self.extract_groups(telegram_data, users_expansion=True):
            for user in group['users']:
                if user['id'] not in users_id_list:
                    users_id_list.append(user['id'])
                    users_list.append(user)
        return users_list

    def extract_groups(self, telegram_data, users_expansion=False):
        groups_list = []
        groups_id_list = []
        for chat_group in telegram_data['chat_groups']:
            if chat_group['id'] not in groups_id_list:
                groups_id_list.append(chat_group['id'])
                users_list = []
                if users_expansion is True:
                    for user in chat_group['users']:
                        users_list.append({
                            'id': user['id'],
                            'username': user['username'],
                            'firstname': user['first_name'],
                            'lastname': user['last_name'],
                        })
                    groups_list.append({
                        'id': chat_group['id'],
                        'name': chat_group['title'],
                        'users': users_list
                    })
                else:
                    groups_list.append({
                        'id': chat_group['id'],
                        'name': chat_group['title']
                    })
        return groups_list

    def connect_telegram(self):
        env = self.get_environment_variables()

        if env['telegram_api_id']:
            api_id = env['telegram_api_id']
        else:
            self.message_stderr('Environment variable "telegram_api_id" not set!', color='red')
            return False

        if env['telegram_api_hash']:
            api_hash = env['telegram_api_hash']
        else:
            self.message_stderr('Environment variable "telegram_api_hash" not set!', color='red')
            return False

        if env['telegram_api_phone']:
            api_phone = env['telegram_api_phone']
        else:
            self.message_stderr('Environment variable "telegram_api_phone" not set!', color='red')
            return False

        self.telegram_client = TelegramClient(api_phone, api_id, api_hash)

        self.telegram_client.connect()
        if not self.telegram_client.is_user_authorized():
            self.telegram_client.send_code_request(os.environ.get('telegram_api_phone'))
            self.telegram_client.sign_in(
                os.environ.get('telegram_api_phone'),
                input('Enter the MFA code provided to you in the Telegram application: ')
            )
        self.message_stderr('Connected to Telegram with api_id: {}'.format(api_id), color='grey')
        return True

    def get_chat_groups(self, expansions=None, limit=9999):
        if expansions is None:
            expansions = []

        chat_channels = self.get_chats_by_attribute(attribute='participants_count', limit=limit)

        if 'users' in expansions:
            for channel_index, channel in enumerate(chat_channels):
                channel_id = str(channel['id'])
                chat_channels[channel_index]['users'] = self.get_chat_users(self.__chat_objects_by_id_hack[channel_id])

        return chat_channels

    def get_chat_users(self, chat_channel_object):
        channel_users = self.telegram_client.get_participants(chat_channel_object)
        return self.cast_jsonable(channel_users)

    def get_chats_by_attribute(self, attribute, limit=10):
        chats = []
        result = self.telegram_client(
            GetDialogsRequest(
                offset_date=None,
                offset_id=0,
                offset_peer=InputPeerEmpty(),
                limit=limit,
                hash=0
            )
        )

        result_count = 0
        if hasattr(result, 'chats'):
            result_count = len(result.chats)

        if result_count > 0:
            for chat in result.chats:
                if hasattr(chat, attribute):
                    self.__chat_objects_by_id_hack[str(chat.id)] = chat
                    chats.append(self.cast_jsonable(chat))
        return chats

    def cast_jsonable(self, obj, __depth=0, __depth_limit=8):

        if __depth >= __depth_limit:
            return '<< OBJECT DEPTH LIMIT >>'

        if obj is None or type(obj) in [int, float, str, bool]:
            return obj

        if type(obj) is list or 'List' in type(obj).__name__:
            result = []
            for item in obj:
                result.append(self.cast_jsonable(item, __depth+1))
            return result

        if not hasattr(obj, '__dict__'):
            return obj.__str__()

        result = {}
        for attribute in obj.__dict__:
            result[attribute] = self.cast_jsonable(obj.__dict__[attribute], __depth+1)
        return result

    def flatten_to_csv(self, obj, delimiter='.'):
        flat_obj = self.__flatten_object(obj, delimiter=delimiter)

        data = []
        data_row = {}
        data_row_keys = []
        data_row_last = None
        line_number_previous = -1
        for flat_key in flat_obj:
            key = self.__flattened_key_parse(flat_key, method='key', delimiter=delimiter)
            line_number = self.__flattened_key_parse(flat_key, method='line', delimiter=delimiter)
            if line_number != line_number_previous:
                if data_row:
                    data.append(copy.copy(data_row))
                line_number_previous = line_number
            data_row[key] = flat_obj[flat_key]
            if key not in data_row_keys:
                data_row_keys.append(key)
            data_row_last = data_row
        data.append(copy.copy(data_row_last))

        # return json.dumps(data, indent=2)

        def __csv_row(list_items, char='"', end='\n'):
            return char + '{char},{char}'.format(char=char).join(str(x) for x in list_items) + char + end

        csv = __csv_row(data_row_keys)
        for row in data:
            row_list = []
            for data_row_key in data_row_keys:
                if data_row_key in row:
                    row_list.append(row[data_row_key])
                else:
                    row_list.append('')
            csv += __csv_row(row_list)
        return csv.rstrip('\n')

    def __flatten_object(self, obj, parent_key='', delimiter='.'):
        items = []
        if type(obj) is list:
            for list_index, value in enumerate(obj):
                new_key = '{}{}{}'.format(parent_key, delimiter, str(list_index)) if parent_key else str(list_index)
                if type(value) in (str, int, float, bool):
                    items.append((new_key, value))
                else:
                    items.extend(self.__flatten_object(value, new_key, delimiter=delimiter).items())
        elif type(obj) is dict:
            for key, value in obj.items():
                new_key = '{}{}{}'.format(parent_key, delimiter, key) if parent_key else key
                if type(value) in (str, int, float, bool) or value is None:
                    items.append((new_key, value))
                else:
                    items.extend(self.__flatten_object(value, new_key, delimiter=delimiter).items())
        else:
            raise TelegramInterfaceException('Unsupported object type encountered while attempting to __flatten_object()')
        return dict(items)

    def __flattened_key_parse(self, flat_key, method='key', delimiter='.'):
        if method.lower() == 'key':
            key = ''
            for flat_key_part in flat_key.split(delimiter):
                if not flat_key_part.isdigit():
                    key = '{}{}{}'.format(key, delimiter, flat_key_part) if key else flat_key_part
            return key
        else:
            flat_key_part_numbers = []
            for flat_key_part in flat_key.split(delimiter):
                if flat_key_part.isdigit():
                    flat_key_part_numbers.append(int(flat_key_part) + 1)
            return reduce((lambda x, y: x * y), flat_key_part_numbers)