Skip to content

Instantly share code, notes, and snippets.

@itskenny0
Created December 29, 2020 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itskenny0/0c41f52641c3d2deeb4d59bbf135b9dd to your computer and use it in GitHub Desktop.
Save itskenny0/0c41f52641c3d2deeb4d59bbf135b9dd to your computer and use it in GitHub Desktop.
VERY crude and hastily written Python script to clean up a Telegram group using Telethon based on the user having been online within a month and a picture and username in their profile
from telethon import TelegramClient,sync
from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.functions.channels import GetAdminLogRequest
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.functions.channels import EditBannedRequest
from telethon.tl.types import ChannelParticipantsSearch
from telethon.tl.types import InputChannel
from telethon.tl.types import ChannelAdminLogEventsFilter
from telethon.tl.types import InputUserSelf
from telethon.tl.types import InputUser
from telethon.tl.types import ChatBannedRights
import readchar
api_id = 12345 # configure this
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # configure this
client = TelegramClient('purge', api_id, api_hash)
client.start()
channel = "yourchannelhere" # configure this
offset = 0
limit = 10000
all_participants = []
def kick_user(channel, user):
client(EditBannedRequest(channel, user, ChatBannedRights(
until_date=None,
view_messages=True
)))
while True:
participants = client(GetParticipantsRequest(
channel, ChannelParticipantsSearch(''), offset, limit,
hash=0
))
if not participants.users:
break
all_participants.extend(participants.users)
offset += len(participants.users)
for user in all_participants:
print(user.restriction_reason)
s = type(user.status).__name__
if str(user.username).lower().endswith('bot'):
continue
if s == "UserStatusRecently":
continue
elif s == "UserStatusLastMonth":
print('Removed:', user.id, user.username)
kick_user(channel, user.id)
elif s == "UserStatusLastWeek":
continue
elif s == "UserStatusOffline":
continue
elif s == "UserStatusOnline":
continue
else:
print('Removed (offline long time, deleted or otherwise weird):', user.first_name, user.last_name, user.id, user.username, user.status, user.is_self, user.restriction_reason)
kick_user(channel, user.id)
continue
### police existance of profile picture
for user in all_participants:
s = type(user.photo).__name__
#print(user.photo)
if s == "UserProfilePhoto":
continue
else:
print('No PFP, kick?', user.first_name, user.last_name, "@" + str(user.username))
resp = readchar.readkey()
if resp == "y":
kick_user(channel, user.id)
print('Kicked:', "ID:", user.id, user.username, user.status, user.restriction_reason)
else:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment