Last active
October 10, 2024 04:10
-
-
Save ronik56/67ff4e5eee46c501686e436060649e6a to your computer and use it in GitHub Desktop.
script to leave all telegram groups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = 111111 | |
api_hash = '2o23o13k1o3131' | |
phone = '+123456789' | |
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: ')) # Enter the login code sent to your telegram | |
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) | |
""" | |
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'] | |
for chat in chats: | |
try: | |
if chat.megagroup== True and chat.title not in groups_to_exclude: | |
client.delete_dialog(chat) | |
except: | |
continue |
Thanks for sharing, It didn't work well with me, I developed a new one https://gist.github.com/eramax/2a651152cbbb4c73d9b9e63e869abea9
Best,
Thank you , added some features and made a new one https://gist.github.com/zodwick/46c7fd398e4616ded6dc5e0bc38da9fd , Hope it helps.
updated verison
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
from telethon.errors import UserNotParticipantError, ChatAdminRequiredError, ChannelPrivateError
import telethon
api_id = '**************'
api_hash = '********************************'
phone_number = '+************'
client = TelegramClient(phone_number, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone_number)
try:
client.sign_in(phone_number, input('Enter the code: ')) # Enter the login code sent to your telegram
except telethon.errors.SessionPasswordNeededError:
password = input("Enter password: ")
client.sign_in(password=password)
chats = []
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.extend(result.chats)
for chat in chats:
try:
# Check if the chat is a group or megagroup
if hasattr(chat, 'megagroup') and chat.megagroup or hasattr(chat, 'broadcast') and chat.broadcast:
client.delete_dialog(chat)
print(f"Deleted group: {chat.title}")
except UserNotParticipantError:
print(f"Failed to delete {chat.title}: The target user is not a member of the specified megagroup or channel.")
except ChatAdminRequiredError:
print(f"Failed to delete {chat.title}: The target user is not an admin of the specified megagroup or channel.")
except ChannelPrivateError:
print(f"Failed to delete {chat.title}: The channel specified is private and you lack permission to access it. Another reason may be that you were banned from it.")
except Exception as e:
print(f"Failed to delete {chat.title}: {e}")
client.disconnect()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import sys
from telethon import TelegramClient
import time
import datetime
from datetime import date
import os, os.path
import asyncio
from telethon import errors
######## API CONFIG #######
#nick1
#api_id=21578781 # Insert api id (int)
#api_hash = '79bac46bfaf8fcff8ae4fa3526743e62' # Insert api hash (string)
#nick 2
#api_id=2 # Insert api id (int)
#api_hash = '2' # Insert api hash (string)
#nick n
#api_id=n # Insert api id (int)
#api_hash = 'n' # Insert api hash (string)
users = [ [21578781, '79bac46bfaf8fcff8ae4fa3526743e62'], # Stands for [api_id, api_hash, phone]
[24721605, 'ac85efbca1b235a39efb6874271b9bf9'] ]
for user in users:
api_id = user[0]
api_hash = user[1]
####### SET GROUPS TO MESSAGE #######
groups = ['botqc321'] # Insert the group usernames as shown above. You must have previously joined them.
gset=set(groups)
groups=list(gset) # eliminate duplicates
####### END GET SHILL GROUPS #####
async def work():
async with TelegramClient('anon',api_id,api_hash) as client:
while(True):
failcount=0
####### Asyncio and loop handling #######
try:
loop = asyncio.get_running_loop()
except RuntimeError: # 'RuntimeError: There is no current event loop...'
loop = None
if loop and loop.is_running():
print('Async event loop already running. Adding coroutine to the event loop.')
tsk = loop.create_task(work())
# ^-- https://docs.python.org/3/library/asyncio-task.html#task-object
# Optionally, a callback function can be executed when the coroutine completes
tsk.add_done_callback(
lambda t: print(f'Task done with result={t.result()} << return val of main()'))
else:
print('Starting new event loop')
asyncio.run(work())
hi pro
Can you fix this program?
The program i am writing only takes first account and first line in chat.txt file , it does not work with 2nd account ,3 ....n and lines 2,3,...n in file chat.txt .
I need multiple accounts to send messages to botqc321 group and these accounts get message data from file chat.txt , each account will read each line in chat.txt file to send to botqc321 group and will repeat at end of line in file chat .txt
Thanks pro