How to Build a Telegram Moderation Bot with Python

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.

Prerequisites

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

 

Step 1: Create Your Bot via BotFather

  1. Open Telegram and search for @BotFather.

  2. Type /newbot and follow the instructions.

  3. You’ll receive a bot token that looks like this:

⚠️ Keep this token private! It grants full access to your bot.

 

Step 2: Install Required Library

Open a terminal and install the python-telegram-bot library:

pip install python-telegram-bot
(Use pip3 if needed on macOS or Linux.)

 

Step 3: Write the Bot Code

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()

 

Step 4: Run the Bot

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.

 

Step 5: Add the Bot to a Telegram Group

  1. Open your Telegram group.

  2. Tap on group settings → Add Members.

  3. Search for your bot by its username (@your_bot_username) and add it.

  4. Once added, promote it to admin.

  5. Important: While promoting, make sure to enable the “Delete Messages” permission.

 

Step 6: Test It

Send a message like:

fuck test
If everything is set up correctly, the bot should delete the message and reply with a warning.

 

Step 7: Keep Your Bot Running 24/7

Right now, your bot stops when you close the terminal or shut down your PC.

To run it continuously, deploy it to:

 

Conclusion

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