def get_chat_list(phone: str): client = TelegramClient(os.path.join('data', phone), api_id, api_hash, proxy=proxy) client.connect() if not client.is_user_authorized(): client.disconnect() return False res = [] for dialog in client.iter_dialogs(): if dialog.is_channel: res.append(dialog) client.disconnect() return res
class TelegramDialogSource(DialogSource): def __init__(self, api_id: int, api_hash: str): self._client = TelegramClient(session=None, api_id=api_id, api_hash=api_hash).start() def __enter__(self): return self def __exit__(self, ecx_type, ecx_value, traceback): self._client.log_out() self._client.disconnect() def getDialogs(self) -> Iterable[Dialog_]: for dialog in self._client.iter_dialogs(): yield TelegramDialog(dialog)
from telethon.sync import TelegramClient # api_id and api_hash you must take from https://my.telegram.org/. Create your app (any) and get it. api_id = # Your api_id in int format (for example, 1234567) api_hash = # Your api_hash in str format (for example, 'deadbeef1337600613') username = # Session name in str format (for example, 'Anon') counter_deleted_message = 0 client = TelegramClient(username, api_id, api_hash) client.start() dialog_array = {} for dialog in client.iter_dialogs(): dialog_array[dialog.id] = dialog.name print("You are currently in the following chats:") for dialog in client.iter_dialogs(): print('"{}" with ID {}'.format(dialog.name, dialog.id)) while True: print() chat = int(input('Enter chat ID to remove your chat messages or "0" (without quotes) for exit: ')) print() if chat == 0: print('Bye bye!') break else: for message in client.iter_messages(chat, from_user='******'): if message.raw_text is not None: counter_deleted_message += 1 client.delete_messages(chat, message) print('Deleted {} post(s) from chat "{}"'.format(counter_deleted_message, dialog_array[chat]))
from telethon.tl.types import MessageMediaPhoto from telethon.utils import get_display_name from telethon.sync import TelegramClient import time from Telebots import Thief_bot print("Hello world") api_id = 1005783 api_hash = '09cb354a26d71b92d9c12e06a7760732' phone_01 = '+380635362036' phone = str new_data = str time_01 = 0 client = TelegramClient(phone_01, api_id, api_hash).start() groups = [] for dialog in client.iter_dialogs(): #выводит список диалогов if len(str(dialog.id)) <= 14: groups.append(dialog) i = 0 for g in groups: print(str(i) + '- ' + g.title + str(g.id)) i += 1
me = client.get_me() ''' 增量爬取 比对incremental文件中每个群组id对应的最后一个消息id的值 如果当前群组的最后一个消息id大于存储值则新增至数据库,同时更新incremental文件中对应的消息id ''' df=pd.read_csv('incremental.csv',encoding='utf-8') f1 = open('incremental.csv', 'a+', encoding='utf-8') csv_writer = csv.writer(f1) responses = client.iter_dialogs() if responses is not None: for response in responses: #if isinstance(response.entity, Channel): # 过滤群组 if hasattr(response.message.to_id,'channel_id'): temp_id=response.message.to_id.channel_id if int(temp_id) not in list(df['group_id']): csv_writer.writerow([str(temp_id),-1]) f1.close() idn=df['group_id'] last_message_id=df['last_message_id'] for name in idn: group_id=int(name) try: