Skip to content

Instantly share code, notes, and snippets.

@zodwick
Created January 4, 2024 14:18
Show Gist options
  • Save zodwick/46c7fd398e4616ded6dc5e0bc38da9fd to your computer and use it in GitHub Desktop.
Save zodwick/46c7fd398e4616ded6dc5e0bc38da9fd to your computer and use it in GitHub Desktop.
Leave unwanted/spam Telegram Groups - also delete joined Telegram messages chats
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
# Go to https://my.telegram.org/apps, sign in, go to API development tools, create an app, copy and paste below:
api_id = 123456
api_hash = '303xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
phone = '+xxxxxxxxxxxx'
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
# Enter the login code sent to your telegram
client.sign_in(phone, 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 = []
chats.extend(result.chats)
"""
Megagroups are groups of more than 200 people, if you want to leave
smaller groups as well delete this part. If you want to stay in a few
specific groups, add their titles to the groups_to_exclude list.
"""
groups_to_exclude = ['group title']
max_members = 10000
for chat in chats:
try:
if chat.megagroup== True and chat.title not in groups_to_exclude:
client.delete_dialog(chat)
print("Deleted {} as megagroup".format(chat.title))
if chat.scam == True:
client.delete_dialog(chat)
print("Deleted {} as scam".format(chat.title))
if chat.participants_count > max_members:
client.delete_dialog(chat)
print("Deleted {} as it has {} members".format(chat.title, chat.participants_count))
except:
continue
"""
----------------------------------
USE WITH CAUTION
----------------------------------
Use this part if you want to delete all your unread messages with one unread message in each chat.
Especially useful to delete xyz joined Telegram messages.
BUT BE CAREFUL, THIS WILL DELETE ALL YOUR UNREAD MESSAGES WITH ONE UNREAD MESSAGE IN EACH CHAT.
"""
dialogues = []
dialogues.extend(result.dialogs)
c = 0
for chat in dialogues:
if chat.unread_count == 1:
client.delete_dialog(chat)
print(c)
c += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment