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]))
class UI: def __init__(self): self.client = TelegramClient('token', api_id, api_hash) try: self.client.connect() except OSError: tk.messagebox.showerror('錯誤', '無法連線伺服器\n請檢查你的網路') if self.client.is_user_authorized(): self.logged_in_windows = tk.Tk() self.logged_in_windows.title("快速刪除Telegram訊息({}已登入)".format( self.client.get_me().first_name)) self.logged_in_windows.geometry('432x243') self.logged_in_windows.resizable(width=0, height=0) self.logged_in_windows.wm_attributes('-topmost', 1) self.chat_room = tk.Entry(self.logged_in_windows) self.chat_room.pack() tk.Label( self.logged_in_windows, text= '\n使用說明\n\n在上方輸入頻道、群組的share link或者私訊對方的username\n格式可以是 https://t.me/TGQNA @TGQNA 或者 TGQNA\n\n在下方選譯是僅刪除自己傳送的訊息還是刪除所有的訊息\n注意:刪除所有訊息時請確保你有對應的權限\n' ).pack() self.del_button = tk.Button(self.logged_in_windows, text='刪除自己的訊息', command=self.del_msg) self.del_button.pack() self.del_all_button = tk.Button(self.logged_in_windows, text='刪除全部的訊息', command=self.delall_msg) self.del_all_button.pack() self.logged_in_windows.mainloop() else: self.log_in_windows = tk.Tk() self.log_in_windows.title("快速刪除Telegram訊息(未登入)") self.log_in_windows.geometry('432x243') self.log_in_windows.resizable(width=0, height=0) self.log_in_windows.wm_attributes('-topmost', 1) tk.Label(master=self.log_in_windows, text='國碼+電話號碼').place(x=20, y=50, height=26, width=100) tk.Label(master=self.log_in_windows, text='驗證碼').place(x=66, y=100, height=26, width=60) tk.Label(master=self.log_in_windows, text='密碼').place(x=72, y=150, height=26, width=60) self.phone_number = tk.Entry(master=self.log_in_windows) self.phone_number.place(x=144, y=50, height=26, width=220) self.code = tk.Entry(master=self.log_in_windows) self.code.place(x=144, y=100, height=26, width=100) self.password = tk.Entry(master=self.log_in_windows, show='*') self.password.place(x=144, y=150, height=26, width=220) self.get_code_button = tk.Button(master=self.log_in_windows, text='驗證碼', width=50, height=30, command=self.get_code) self.get_code_button.place(x=304, y=100, height=26, width=60) self.help_button = tk.Button(master=self.log_in_windows, text='說明', width=40, height=30, command=self.help) self.help_button.place(x=94, y=200, height=26, width=60) self.login_button = tk.Button(master=self.log_in_windows, text='登入', width=40, height=30, command=self.login) self.login_button.place(x=194, y=200, height=26, width=60) self.exit_button = tk.Button(master=self.log_in_windows, text='退出', width=40, height=30, command=self.exit) self.exit_button.place(x=294, y=200, height=26, width=60) self.log_in_windows.mainloop() def exit(self): self.log_in_windows.quit() def get_code(self): if len(self.phone_number.get()) <= 8: tk.messagebox.showerror( '錯誤', '請先輸入正確的電話號碼\n格式:國際電話區號+電話號碼\n例如:+85223802850') return try: self.sent = self.client.send_code_request(self.phone_number.get()) self.hash = self.sent.phone_code_hash except: tk.messagebox.showerror('未知錯誤', '無法取得驗證碼,請兩分鐘後再試!') def help(self): tk.messagebox.showinfo( '使用說明', '電話號碼格式國際電話區號+電話號碼\n例如:+85223802850\n\n驗證碼是5位數字的\n密碼是雙步驟驗證(2FA)密碼,沒有就留空' ) def del_msg(self): self.me = self.client.get_me() for self.message in self.client.iter_messages(self.chat_room.get()): if self.message.from_id == self.me.id: self.client.delete_messages(self.chat_room.get(), self.message.id) print(self.message.id) tk.messagebox.showinfo('成功', '成功刪除 {} 裡自己傳送的訊息'.format(self.chat_room.get())) def delall_msg(self): for self.message in self.client.iter_messages(self.chat_room.get()): self.client.delete_messages(self.chat_room.get(), self.message.id) print(self.message.id) tk.messagebox.showinfo('成功', '成功刪除 {} 裡所有的訊息'.format(self.chat_room.get())) def login(self): if len(self.code.get()) != 5: tk.messagebox.showerror('錯誤', '請先輸入正確的驗證碼') return try: self.client.sign_in(phone=self.phone_number.get(), code=self.code.get(), password=self.password.get(), phone_code_hash=self.hash) except: tk.messagebox.showerror('未知錯誤', '無法登入!') return tk.messagebox.showinfo('登入成功', '請重新啟動這個應用程式!') self.log_in_windows.quit()
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: ')) chats = [] last_date = None chunk_size = 10 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: groups.append(chat) except: continue while True: try: msg = client.send_message(username1, msgtosend) print("Send Complete!!, Waiting for " + str(timer) + " seconds") msgdel = msg.id - 1 client.delete_messages(username1, msgdel) time.sleep(timer) except: continue
if input( 'Are you sure you want to delete your *every outgoing message* in *every group*? (y/N): ' ) != 'y': exit(0) message_ids = [] counter = 0 for dialog in client.get_dialogs(): if dialog.is_group: for message in client.iter_messages(dialog.entity, from_user=client.get_me()): message_ids.append(message.id) counter += 1 print('Message ID: %d, Chat ID: %d, Chat Title: %s' % (message.id, dialog.id, dialog.title)) if len(message_ids) > 99: client.delete_messages(client.get_entity(dialog.entity), message_ids) print('%d messages were deleted.' % len(message_ids)) message_ids = [] client.delete_messages(client.get_entity(dialog.entity), message_ids) print('Total %d messages were deleted.' % counter) print( 'Done! (Please run the script two to three times to clean every message. If you run once only, some messages may won\'t be deleted.)' ) print( 'Some service messages(user joined group and so on) can only be deleted by admins, so the counter will stay the same and it\'s normal.' )