Skip to content

Instantly share code, notes, and snippets.

@rokibhasansagar
Created March 4, 2020 20:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rokibhasansagar/d727fb30ef5a274cf536bea73260887c to your computer and use it in GitHub Desktop.
Save rokibhasansagar/d727fb30ef5a274cf536bea73260887c to your computer and use it in GitHub Desktop.
Kick all the deleted accounts from a telegram chat using telethon.
from telethon import TelegramClient
from telethon.tl.functions.channels import EditBannedRequest
from telethon.tl.types import ChatBannedRights
import asyncio
import datetime
api_id = 1234 # Your API_ID
api_hash = "1a2b3c456" # Your APP_ID
async def clear_chat(client):
group = input("Enter the group username where the script should search for deleted accounts: ")
deleted_accounts = 0
async for user in client.iter_participants(group):
if user.deleted:
try:
deleted_accounts += 1
await client(EditBannedRequest(group, user, ChatBannedRights(
until_date=datetime.timedelta(minutes=1),
view_messages=True
)))
except Exception as exc:
print(f"Failed to kick one deleted account because: {str(exc)}")
if deleted_accounts:
print(f"Kicked {deleted_accounts} Deleted Accounts")
else:
print(f"No deleted accounts found in {group}")
with TelegramClient("deleteacc", api_id, api_hash) as client:
asyncio.get_event_loop().run_until_complete(clear_chat(client))
@ramack19
Copy link

ramack19 commented Nov 5, 2022

Hi,
Thanks for writing this script, works perfectly! I had been trying to run is within Spyder but with errors. However, when running from the CLI, calling directly from Python, it worked. Awesome, thanks again!

@rokibhasansagar
Copy link
Author

Hi, Thanks for writing this script, works perfectly! I had been trying to run is within Spyder but with errors. However, when running from the CLI, calling directly from Python, it worked. Awesome, thanks again!

Glad to know it still works.
Well, I didn't write this code, but got it from somewhere on the net.
Although I forgot to mention the source here and eventually it is left as is.

@naut20161990
Copy link

hi ,
Currently this code says so, can you fix it?

DeprecationWarning: There is no current event loop
asyncio.get_event_loop().run_until_complete(clear_chat(client))

@Hashboii1
Copy link

I'm getting errors here:

with TelegramClient ()"deleteacc"*, api_key, "api_hash) as client:
(asyncio.get_event_loop).run_until_complete(clear_chat(client))

Please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment