Skip to content

Minimal, production-ready Telegram anti-spam bot with trust-based moderation for groups and supergroups.

License

Notifications You must be signed in to change notification settings

Johnny-Taake/TGAntiSpamBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

44 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ TG AntiSpam Bot

A production-ready minimal Telegram bot that protects groups and supergroups from spam using a trust-based moderation system.

The bot automatically removes suspicious messages from new users while allowing legitimate members to communicate freely - without captchas, delays, or manual moderation.


πŸŽ₯ Demo

Short demos are worth more than long explanations

Spam message deletion and Admin (group management): Admin demo

Notification of AI service failures to admin: AI error notification

Bot rights via BotFather: Bot rights demo

Admin rights in a group for bot to delete messages: Admin rights for bot demo


⭐ Key Features

  • Trust-based Anti-Spam

    • New users are monitored more strictly
    • Trusted users are never interrupted
  • Automatic Spam Deletion

    • Links, mentions, and suspicious entities, messages with many emojis are removed
    • Links white-listing
  • Admin Panel

    • Enable or disable protection per group
    • Configure protection parameters per group
    • See all chats the bot is present in
  • Async Queue Processing

    • Handles high message volume safely
  • Polling & Webhook modes

  • Persistent Storage

    • SQLite + migrations
    • Filters, middleware, services, registry cache

🧠 AI-Powered Contextual Spam Detection (Optional)

The bot can optionally use an AI-based contextual analyzer to detect suspicious messages that bypass classic heuristics.

This feature is disabled by default and works as an additional signal on top of the trust-based system - not a replacement.

What the AI detects

The AI model analyzes message intent and context, not just keywords. It helps catch messages like:

  • Funnel / solicitation phrasing
    • "write me in DM"
    • "details in PM"
    • "contact privately"
  • Cross-language bait (RU / EN mixed messages)
  • Indirect advertising without links
  • Rephrased spam that avoids obvious patterns

How it works

  1. Message passes basic filters (chat type, trust level)
  2. If enabled, the message is sent to the AI analyzer
  3. The analyzer runs a Prompt Pack (multiple prompts) sequentially
  4. Each prompt returns a risk score (0.0–1.0)
  5. If any prompt score reaches the configured threshold - the message is flagged
  6. The decision is applied by the AntiSpamService (AI affects deletion only)

The AI never auto-bans users - it only influences message deletion.

Prompt Pack (multiple prompts)

The bot supports running any number of prompts stored as plain text files in prompts/.

Ordering rule (by filename suffix): Prompts are loaded from prompts/*.txt Files are executed in ascending numeric order based on the trailing _N suffix Examples (any name instead of moderation_policy will work):

  1. moderation_policy.txt β†’ treated as index 0
  2. moderation_policy_1.txt
  3. moderation_policy_2.txt
  4. moderation_policy_3.txt
  5. …and so on

This lets you keep prompts small and focused (e.g., illegal activity / funnel solicitation / formatting tricks), and modify the size and amount of prompts depending on the LLM used (bigger models require less prompts and more token budget).

Design principles

  • Fail-safe - if AI is unavailable, the bot works normally
  • Low latency - async queue, non-blocking
  • Deterministic prompts - prompt order is controlled by filenames
  • Explainable thresholds - per-message: β€œhit on prompt #N with score X”
  • Privacy-aware - messages are not stored by the AI layer

Configuration

Enable AI moderation via environment variables:

APP_AI_ENABLED=true
APP_AI_MODEL=your_model_name
APP_AI_BASE_URL=your_provider_url | local_ollama_url
APP_AI_API_KEY=your_api_key

If configuration is incomplete, the AI service is automatically skipped.

πŸ“„ Detailed configuration: docs/AI.md πŸ“„ Local Ollama API: docs/OLLAMA.md

Supported modes

  • Local models via Ollama
  • OpenAI-compatible APIs

❓ How It Works

The bot uses a trust model instead of hard rules:

  1. New users

    • Messages with links or mentions may be deleted
  2. Trust building

    • Time spent in chat
    • Number of clean messages sent
  3. Trusted users

    • No moderation
    • No delays
    • No false positives

This approach keeps chats clean without annoying real people.


πŸͺ› Configuration

Configuration is done via environment variables.

πŸ“„ Full explanation: docs/ENV.md πŸ“„ Example file: .env.example

Minimal required variables:

APP_BOT_TOKEN=your_bot_token_here
APP_MAIN_ADMIN_ID=your_telegram_user_id

Everything else has safe defaults.


πŸ“¦ Installation

🐳 Docker (Recommended)

πŸ“„ Detailed guide: docs/DOCKER.md

git clone <repository-url>
cd TGAntiSpamBot

cp .env.example .env
# edit .env

make run

The database and logs (depending on the env) are persisted automatically.

Make is the preferred tool for local development. Otherwise, you can use the docker-compose.yml file directly.


Production / Development Setup

πŸ“„ make usage: docs/MAKE.md πŸ“„ docker usage: docs/DOCKER.md πŸ“„ uv usage: docs/UV.md


πŸ€– Bot Commands

Private chats only

  • /start - Welcome message
  • /about - Bot description

For admin only:

  • /chats - Admin panel
  • /metrics - Runtime metrics
  • /test_ai - Test AI service

Only if fun is enabled via .env:

  • /dice - Roll a dice 🎲
  • /slot - Slot machine 🎰

πŸ› οΈ Admin

Accessible via /chats (private chat, admin only).

Allows you to:

  • View all groups (after adding there bot, giving admin rights and sending any message in the group after bot join by any user)
  • Activate / deactivate anti-spam per group
  • Navigate chats with pagination
  • Safely manage large numbers of groups

🧩 Architecture Overview

  • Filters - chat type, admin-only, private-only

  • Middleware

    • DB session lifecycle
    • Chat registry cache
  • Services

    • AntiSpamService (queue + workers)
    • ChatRegistry (in-memory TTL cache)
  • Database

    • SQLAlchemy ORM
    • Alembic migrations
  • Bot Runtime

    • aiogram 3.x

πŸ“ Project Structure

ai_client/                # AI client (providers/adapters, requests, utils)
β”œβ”€β”€ adapters/             # Provider adapters (Ollama, OpenAI)
β”œβ”€β”€ models/               # Request parts, errors
└── service.py            # Unified AI service
alembic/                  # DB migrations
app/
β”œβ”€β”€ antispam/             # Anti-spam core
β”‚   β”œβ”€β”€ ai/               # AI moderator + notifier
β”‚   β”œβ”€β”€ detectors/        # Mentions/links/text normalization
β”‚   β”œβ”€β”€ processors/       # Message processing pipeline
β”‚   β”œβ”€β”€ scoring/          # AI scoring + parsing
β”‚   β”œβ”€β”€ dto.py            # MessageTask and DTOs
β”‚   └── service.py        # AntiSpamService
β”œβ”€β”€ bot/
β”‚   β”œβ”€β”€ filters/          # Chat/admin filters
β”‚   β”œβ”€β”€ handlers/         # Message handlers
β”‚   β”‚   β”œβ”€β”€ admin/        # Admin UI: callbacks, keyboards, renderers, services
β”‚   β”‚   β”œβ”€β”€ fun/          # Dice/slot etc.
β”‚   β”‚   └── test/         # Test commands (AI test handler)
β”‚   β”œβ”€β”€ middleware/       # DB session, registry, antispam, security
β”‚   β”œβ”€β”€ utils/            # Bot helpers (message actions etc.)
β”‚   β”œβ”€β”€ bootstrap.py      # Bot bootstrap
β”‚   β”œβ”€β”€ factory.py        # Bot factory (DI wiring)
β”‚   β”œβ”€β”€ run_polling.py    # Polling entry
β”‚   └── run_webhook.py    # Webhook entry
β”œβ”€β”€ db/                   # Database layer
β”‚   β”œβ”€β”€ models/           # SQLAlchemy models
β”‚   β”œβ”€β”€ base.py           # Base model / metadata
β”‚   └── helper.py         # DB helpers
β”œβ”€β”€ services/             # Business services (chat, user, registry, cache)
β”œβ”€β”€ container.py          # App container (DI)
β”œβ”€β”€ monitoring.py         # Metrics / monitoring
└── security.py           # Security helpers
config/                   # Settings (Pydantic)
β”œβ”€β”€ settings.py
β”œβ”€β”€ bot.py
β”œβ”€β”€ ai_client.py
β”œβ”€β”€ database.py
└── logging.py
docs/                     # Documentation
β”œβ”€β”€ AI.md
β”œβ”€β”€ DOCKER.md
β”œβ”€β”€ ENV.md
β”œβ”€β”€ MAKE.md
β”œβ”€β”€ OLLAMA.md
β”œβ”€β”€ TEST.md
β”œβ”€β”€ UV.md
β”œβ”€β”€ gifs/                 # Demo GIFs
└── images/
scripts/                  # Utility scripts (ollama pull)
tests/                    # Tests
utils/                    # Shared utilities
main.py                   # App entrypoint
README.md                 # This file

Deployment Modes

Polling (default)

  • No public URL required
  • Best for local or simple hosting

Webhook

  • Requires HTTPS and public URL
  • Recommended for production

πŸ“„ Webhook details: docs/DOCKER.md

APP_BOT_MODE=webhook
APP_WEBHOOK_URL=https://your-domain.com

Testing

πŸ“„ Testing guide: docs/TEST.md


🀝 Contributing

  1. Fork the repo
  2. Create a feature branch
  3. Write clean, typed code
  4. Add tests where reasonable
  5. Open a Pull Request

Support

If you:

  • found a bug
  • want a feature
  • need help with setup

➑️ write to the issues section.

About

Minimal, production-ready Telegram anti-spam bot with trust-based moderation for groups and supergroups.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages