Have you ever wanted to automatically moderate a Telegram group — block spam, remove offensive language, and keep things clean?
In this guide, I’ll walk you through building a simple but powerful moderation bot using Python and the python-telegram-bot library. By the end, you’ll have a working bot that monitors group messages and deletes those containing inappropriate content.
Before you start, make sure you have the following:
Python 3 installed
A Telegram account
A bot created via @BotFather
The python-telegram-bot library installed
Open Telegram and search for @BotFather.
Type /newbot and follow the instructions.
You’ll receive a bot token that looks like this:
⚠️ Keep this token private! It grants full access to your bot.
Open a terminal and install the python-telegram-bot library:
pip install python-telegram-bot
(Use pip3 if needed on macOS or Linux.)
Create a file on your desktop named bot.py, and paste the following code:
from telegram import Update
from telegram.ext import ApplicationBuilder, MessageHandler, ContextTypes, filters
TOKEN = “YOUR_BOT_TOKEN_HERE”
# Offensive and spam keyword lists
BAD_WORDS = [“fuck”, “fuuck”, “fck”]
SPAM_KEYWORDS = [“bet”, “casino”, “free bonus”, “giveaway”, “telegram.me”]
async def message_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
msg = update.message.text.lower()
if any(word in msg for word in BAD_WORDS + SPAM_KEYWORDS):
await update.message.delete()
await update.message.reply_text(“Inappropriate message removed.”)
app = ApplicationBuilder().token(TOKEN).build()
app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), message_filter))
app.run_polling()
Navigate to your Desktop (or wherever the file is) and run:
cd ~/Desktop
python3 bot.py
If there are no errors, the bot will start and wait for messages.
Open your Telegram group.
Tap on group settings → Add Members.
Search for your bot by its username (@your_bot_username) and add it.
Once added, promote it to admin.
Important: While promoting, make sure to enable the “Delete Messages” permission.
Send a message like:
fuck test
If everything is set up correctly, the bot should delete the message and reply with a warning.
Right now, your bot stops when you close the terminal or shut down your PC.
To run it continuously, deploy it to:
In this guide, you learned how to create a simple Telegram moderation bot with Python. It deletes offensive and spammy messages and helps keep your community clean and safe.
With a few tweaks, you can turn this into a fully-featured moderation tool for your Telegram groups.
Railway.app or Render.com (free tier available)
Heroku (with workaround)
DigitalOcean or any VPS
Raspberry Pi at home