def make_config(): chunk_size = 200 channels_in = [] channels_admin = [] result = client( GetDialogsRequest(offset_date=None, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size, hash=0)) for chat in result.chats: try: if chat.broadcast: channels_in.append(chat) except: continue channels_admin = channels_in print("Choose Channels To Forward Message From: ") ch_in_id = select_channels(channels_in) print("Choose Channels To Send Message To: ") ch_own_id = select_channels(channels_admin) data = {"from": ch_in_id, "to": ch_own_id} with open("config.json", "w") as config_out: json.dump(data, config_out) return data
def list_dialogs(self): users = [] chats = [] last_date = None chunk_size = 100 while True: result = self.client( GetDialogsRequest(offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size)) if not result.dialogs: break users.extend(result.users) chats.extend(result.chats) last_date = min(msg.date for msg in result.messages) sleep(.5) self.entities = chats + users # dialogs, self.entities = self.client.get_dialogs(1000) for i, entity in enumerate(self.entities, start=0): display_name = Application.get_display_name(entity) try: self.listbox_chats.insert(i, display_name) except: self.listbox_chats.insert(i, convert65536(display_name)) print( 'Chat list ready. Please select dialogs from the list and press OK.' )
def __init__(self, api_id, api_hash, phone, username, token, chat_id, user_set=None, dialogs_limit=20): """ requires telegram api id, hash, the user's phone number, username, the bot token and the chat id with the bot (in order for the bot to send messages directly to the user). User_set is used to limit the chat search to certain users, and dialogs_limit is used to limit the number of chats to be searched. """ self._client = TelegramClient('session', api_id, api_hash) self._phone = phone self._username = username self._token = token self._chat_id = chat_id self._url = f'https://api.telegram.org/bot{token}/sendMessage' self._get_dialogs = GetDialogsRequest(offset_date=None, offset_id=0, offset_peer=InputPeerEmpty(), limit=dialogs_limit, hash=0) self._user_set = set( user_set ) if user_set is not None else None # must be telegram usernames (e.g. @Username)
def get_chats(): global from_groups, group_s, to_group, dict1 chats = [] last_date = None chunk_size = 200 group_s = [] from_groups = [] to_group = [] list_len = [] 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: group_s.append(chat) except: continue i = 0 for group in group_s: a = (str(i) + '- ' + group.title[:15]) from_groups.append(a) list_len.append(i) i += 1 for c in from_groups: dict1 = { from_groups[c]: list_len[c] for c in range(0, len(group_s), 1) } print("get chat finished") dropdown_menu()
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 __init__(self, name, api_id, api_hash, phone, password): self._name = name self._api_id = api_id self._api_hash = api_hash self._phone_number = phone self._password = password self._client = TelegramClient(self._name, self._api_id, self._api_hash) self._client.connect() if not self._client.is_user_authorized(): print('Not authorized') self._client.send_code_request(self._phone_number) try: self._client.sign_in(self._phone_number, input('Enter code:\n')) except SessionPasswordNeededError: self._client.sign_in(password=self._password) if self._client.is_user_authorized(): print('Authorized') self._dialogs = self._client( GetDialogsRequest(offset_date=None, offset_id=0, offset_peer=InputPeerEmpty(), limit=500, hash=1))
def Channels(client): 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.broadcast == True: # megagroup groups.append(chat) except: continue print('From Which Group Yow Want To Scrap A Members:') i = 0 for g in groups: print(str(i) + '- ' + g.title) i += 1 return groups
def GetMembers(client): 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 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) return all_participants, target_group
def start_tg(): try: apiidint = int(api_id) except ValueError: ## print('ValueError') return client = TelegramClient(phone, api_id, api_hash) try: client.start(phone) except: sys.exit(0) chats = [] groups = [] result = client( GetDialogsRequest(offset_date=None, offset_id=0, offset_peer=InputPeerEmpty(), limit=200, hash=0)) chats.extend(result.chats) for chat in chats: if isinstance(chat, ChatForbidden): continue groups.append(chat) if chat.id == report_tchannel_id: report_tchannel = chat return (client, report_tchannel)
def get_groups(client: TelegramClient): 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 return groups
def ext_all(): 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 extract all members:') 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("ext_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 extracted successfully please check ext_members ')
def list_users_in_group(): 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: print(chat) groups.append(chat) # if chat.megagroup== True: 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('\n\nGrupo elegido:\t' + groups[int(g_index)].title) print('Fetching Members...') all_participants = [] all_participants = client.get_participants(target_group, aggressive=True) print('Saving In file...') with open("members-" + re.sub("-+","-",re.sub("[^a-zA-Z]","-",str.lower(target_group.title))) + ".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.')
def get(chat_num): #print(chat_num) 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 g_index = chat_num target_group = groups[int(g_index)] filename = target_group.title print('Fetching Members from {} ...'.format(filename)) all_participants = [] all_participants = client.get_participants(target_group, aggressive=True) print('Saving In file...') #print(target_group.title) filename = target_group.title with open(("{}.csv".format(filename)), "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 from {} .'.format(filename))
def get_dialogs(self, count=10, offset_date=None, offset_id=0, offset_peer=InputPeerEmpty()): """Returns a tuple of lists ([dialogs], [entities]) with 'count' items each. The `entity` represents the user, chat or channel corresponding to that dialog""" r = self.invoke(GetDialogsRequest(offset_date=offset_date, offset_id=offset_id, offset_peer=offset_peer, limit=count)) return (r.dialogs, [self.find_user_or_chat(d.peer, r.users, r.chats) for d in r.dialogs])
def GET(self): # Check auth if not checkauth(): return "error_noauthdata" # Handle cache clientcachedir = cachedir + "/" + session.client_id if not os.path.isdir(clientcachedir): os.makedirs(clientcachedir) chats_shelve = shelve.open(clientcachedir + "/chats.shelve") chats_shelve.sync() shelve_ctime = time.gmtime( os.path.getctime(clientcachedir + "/chats.shelve")) web.header('X-last-fetched-data', time.strftime("%d %b %Y %H:%M:%S %z", shelve_ctime)) # Only get data if not cached already if not "data" in chats_shelve: # This operation is expensive, throttle if not throttle_status_ok(web): return "pleasecooldown" # Get Telegram Client client = getclient() if isinstance(client, str): return client # Get chats groups = [] groups_raw = {} tgr = client( GetDialogsRequest(offset_date=None, offset_id=0, offset_peer=InputPeerEmpty(), limit=60)) for chat in tgr.chats: logging.debug("admintools:getchats:GET Parsing %s..." % chat) if (isinstance(chat, Chat) or isinstance(chat,Channel)) and \ (chat.participants_count is None or chat.participants_count>2) and \ (not hasattr(chat, "migrated_to")) and \ ((hasattr(chat, "admin") and chat.admin is True) or (hasattr(chat, "admin_rights") and chat.admin_rights is not None) or \ (hasattr(chat, "creator") and chat.creator is True)) and \ (existsGroup(chat.id) or existsGroup(int("-100" + str(chat.id)))): groups.append([chat.id, chat.title]) logging.debug("admintools:getchats:GET ACCEPTED") else: logging.debug("admintools:getchats:GET REJECTED") chats_shelve["data"] = groups groups = chats_shelve["data"] chats_shelve.close() return json.dumps(groups)
def get_all_conversations(tg_client, only_groups=False, last_date=None, chat_size=200): ''' Ottiene tutte le conversazioni aperte sul client impostato. Params: @tg_client: Client Telegram connesso da usare per estrapolare i dati @only_groups [False]: Ottiene le chat solo dai gruppi (ignora le chat private e i canali) @last_date [None]: Ottenere le chat fino a questo giorno @chat_size [200]: Numero massimo di messaggi ottenibili (???) @return: Lista di tuple, ogni record è un gruppo (gruppo, 0), un canale (canale, 1), una chat privata (chat, 2) o una conversazione non identificabile (?, 3) ''' # Variabili locali chats = [] return_values = [] # Ottiene tutti le chat aperte e i loro messaggi result = tg_client( GetDialogsRequest(offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chat_size, hash=0)) chats.extend(result.chats) # Separa gruppi da canali e conversazioni private for chat in chats: try: if chat.megagroup is True: return_values.append((chat, 0)) # Gruppi elif chat.broadcast is True: return_values.append((chat, 1)) # Canali else: return_values.append((chat, 2)) # Chat private except BaseException: return_values.append((chat, 3)) # Sconosciuto # Ottiene tutti i gruppi ed esclude gli utenti privati e i canali if only_groups: return_values = [value for value in return_values if value[1] == 0] # Ritorna le conversazioni individuate return return_values
def main(self): # reference function print(self.client.is_user_authorized()) # You can print all the dialogs/conversations that you are part of: for dialog in self.client.iter_dialogs(): print(dialog.name, 'has ID', dialog.id) # You can send messages to yourself... self.client.send_message('me', 'Hello, myself!') # ...or even to any username # await client.send_message('Fiqire96', 'Hello, Fiqire96!') chats = [] last_date = None chunk_size = 200 groups = [] 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: 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)] all_participants = [] all_participants = self.client.get_participants(entity=target_group, aggressive=True) for x in all_participants: print(x.id) print(len(all_participants)) # Or send files, songs, documents, albums... self.client.send_file('me', r'C:\Users\Kenneth\Desktop\Untitled2.png')
def get_list(): #Get and return chat list from telegram last_date = None chunk_size = 200 result = client(GetDialogsRequest( offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size, hash = 0 )) chats = {} for i in result.chats: title = give_emoji_free_text(i.title.encode('utf8')) chats[title] = i return chats
async def list_groups(self): chunk_size = 200 # print("Started Listing...") result = await self.client( GetDialogsRequest(offset_date=None, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size, hash=0)) chats = result.chats for chat in chats: try: if chat.megagroup: self.groups.append(chat) self.groups_title.append(chat.title) except: continue return len(self.groups) == len(self.groups_title)
def get_chats(client): """get groups from my account""" # from https://python.gotrained.com/scraping-telegram-group-members-python-telethon/ from telethon.tl.functions.messages import GetDialogsRequest from telethon.tl.types import InputPeerEmpty 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) return chats
async def list_groups(self): chats = [] last_date = None chunk_size = 200 groups = [] self.request = GetDialogsRequest(offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size, hash=0) result = await self(self.request) chats.extend(result.chats) for chat in chats: try: if chat.megagroup is True and chat.admin_rights.ban_users is True: groups.append(chat) except Exception: continue return groups
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
async def get_all_groups(): chats = [] groups = [] result = await client( GetDialogsRequest(offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_group_size, hash=0)) chats.extend(result.chats) for chat in chats: try: if chat.megagroup: groups.append(chat) except: continue return groups
def get_data_group(client): print('getting data ' + phone) chats = [] last_date = None chunk_size = 200 groups = [] query = client( GetDialogsRequest(offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size, hash=0)) chats.extend(query.chats) for chat in chats: try: if chat.megagroup is not None and chat.access_hash is not None: groups.append(chat) except: continue results = [] for group in groups: try: tmp = { 'group_id': str(group.id), 'access_hash': str(group.access_hash), 'title': str(group.title), 'username': str(group.username), 'participants_count': group.participants_count } results.append(tmp) if group.megagroup == True: remove_member(client, group) get_data_user(client, group) except Exception as e: print(e) print('error save group') with open('data_remove/group/' + phone + '.json', 'w', encoding='utf-8') as f: json.dump(results, f, indent=4, ensure_ascii=False)
def get_contacts(req_user, client): dialogs = [] users = [] chats = [] last_date = None chunk_size = 20 while True: result = client( GetDialogsRequest(offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size)) dialogs.extend(result.dialogs) users.extend(result.users) chats.extend(result.chats) if not result.messages: break last_date = min(msg.date for msg in result.messages) sleep(2) for u in users: try: user = TGUser.objects.get(sid=u.id) except TGUser.DoesNotExist: user = TGUser() if u.is_self: continue user.sid = u.id user.first_name = u.first_name user.last_name = u.last_name user.username = u.username user.phone = u.phone user.is_bot = u.bot user.is_mutual_contact = u.mutual_contact user.is_deleted = u.deleted if not user.is_deleted and not user.is_bot and user.sid not in TG_SERVICE: user.status = TGUser.ACTIVE else: user.status = TGUser.INACTIVE user.user = req_user user.save()
def refresh_read_state(mailing, client): dialogs = [] users = [] chats = [] last_date = None chunk_size = 20 while True: result = client( GetDialogsRequest(offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size)) dialogs.extend(result.dialogs) users.extend(result.users) chats.extend(result.chats) if not result.messages: break last_date = min(msg.date for msg in result.messages) sleep(2) compare_states(mailing, dialogs=dialogs)
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
async def get_members_list(self, channel_id: int): channel = await self.client.get_entity(int(channel_id)) target_title = channel.title chats = [] result = await self.client( GetDialogsRequest(offset_date=0, offset_id=10, offset_peer=InputPeerEmpty(), limit=100, hash=0)) chats.extend(result.chats) target_group = None for chat in chats: if chat.title == target_title: target_group = chat break if target_group is None: return None all_participants = [] all_participants = await self.client.get_participants(target_group, aggressive=True) return all_participants if all_participants else None
def get_chats() -> tuple: dialogs = [] users = [] chats = [] last_date = None chunk_size = 20 while True: result = client( GetDialogsRequest(offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size)) dialogs.extend(result.dialogs) users.extend(result.users) chats.extend(result.chats) if not result.messages: break last_date = min(msg.date for msg in result.messages) sleep(2) return tuple(dialogs, users, chats)
def all_dialog(client): dialogs = [] users = [] chats = [] last_date = None chunk_size = 200 block = 1 sleep_counter = 0 while True: result = client(GetDialogsRequest(offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size)) dialogs.extend(result.dialogs) users.extend(result.users) chats.extend(result.chats) log.warning("Block: {}, Count: {}, Users: {}, Dialog: {}, Chat: {}".format(block, len(result.messages), len(result.users), len(result.dialogs), len(result.chats))) block += 1 sleep_counter += 1 if not result.messages: break last_date = min(msg.date for msg in result.messages) if sleep_counter > 4: sleep(1) sleep_counter = 0 return {"dialogs": dialogs, "users": users, "chats": chats}