Skip to content

Instantly share code, notes, and snippets.

@aequabit
Created January 31, 2026 22:29
Show Gist options
  • Select an option

  • Save aequabit/0380381696f1356ed168d26404c576bd to your computer and use it in GitHub Desktop.

Select an option

Save aequabit/0380381696f1356ed168d26404c576bd to your computer and use it in GitHub Desktop.
# hoeremover 0.0.1
# NOTE: I suggest not using your main Telegram account for creating bots
# DEPENDENCIES: Install `telethon` via pip
from telethon.sync import TelegramClient, events
from telethon.tl.functions.channels import GetMessagesRequest
from telethon.tl.functions.users import GetFullUserRequest
# Doesn't really matter
session_name = 'hoeremover'
# Get from https://my.telegram.org/auth
app_id = 123456
api_hash = 'abcdefg123456'
# Get from @BotFather on Telegram
bot_token = '123456789:bguisberniguhbrseiughers9p8'
with TelegramClient(session_name, app_id, api_hash).start(bot_token=bot_token) as client:
async def ban_user(client, chat_id, from_id):
msg = await client.edit_permissions(chat_id, from_id, view_messages=False)
if len(msg.updates) > 0 and msg.updates[0].id:
await client.delete_messages(chat_id, msg.updates[0].id)
@client.on(events.ChatAction)
async def handler(event):
if event.user_joined and event.action_message and event.action_message.from_id:
full_user = await event.client(GetFullUserRequest(id=event.action_message.from_id.user_id))
if full_user.full_user.personal_channel_message and full_user.chats and len(full_user.chats) > 0:
chat_title = full_user.chats[0].title
# Most bots are caught by this, no need for another request
if "Secret Place" in chat_title and "18+" in chat_title:
await ban_user(event.client, event.chat_id, event.action_message.from_id);
else: # Check first channel message for t.me links instead
messages = await event.client(
GetMessagesRequest(full_user.chats[0], [full_user.full_user.personal_channel_message])
)
if messages.count > 0 and messages.messages and messages.messages[0].message:
msg_content = messages.messages[0].message
if "t.me/" in msg_content:
await ban_user(event.client, event.chat_id, event.action_message.from_id);
client.run_until_disconnected()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment