AI Anti-Spam Bot for Telegram
April 13, 2026 ยท View on GitHub
๐ค AI-powered anti-spam bot for Telegram groups
๐ฏ Demo Bot โข ๐ข Channel โข ๐ฌ Group โข ็ฎไฝไธญๆ
โ ๏ธ Usage Notice
If you use this project, please:
- โ
Keep the developer information in the Bot (
/startcommand) - โ Credit the source in your project
- โ Do not remove copyright notices from the code
This is the most basic respect for open source authors ๐
๐ก Official Version: This is the official repository maintained by the original author. Forks may be outdated.
โจ Features
- ๐ก๏ธ Smart Detection: AI-powered spam detection for text, images, and stickers
- ๐ Visible-Content Checks: Image captions and quoted snippets are included in moderation
- ๐ Reply & Forward Coverage: Reply content and forwarded visible content are included in extraction
- ๐ฏ Multi-Model Support: Choose from OpenAI, Qwen, or DeepSeek
- ๐ Flexible Strategy: Configurable detection days, message count, verification times
- ๐ User-Friendly Management: One-click unban, admin panel
- ๐ข Ad Buttons: Custom advertisement buttons on ban notifications
- โก High Performance: Async processing, non-blocking
- ๐ Verification System: Verified users skip future checks, saving API calls
- ๐ Auto Retry: AI API calls auto-retry on failure (3 times, exponential backoff)
- ๐ Persistent Logs: Runtime logs saved to
data/bot.log - ๐งน Auto Cleanup: Ban notices and welcome messages can auto-delete after a configurable delay
- ๐ Statistics:
/statscommand to view detection statistics - ๐ Multi-Language: Support for Chinese/English
๐ Quick Start
1. Install Dependencies
pip install -r requirements.txt
2. Configuration
cp config.example.yml config.yml
# Edit config.yml with your settings
Required:
telegram.token- Your bot token (from @BotFather)telegram.owners- Super admin ID (your Telegram ID, get it from @userinfobot)- AI model config (choose one):
openai.api_key- OpenAI API Keyqwen.api_key- Qwen API Keydeepseek.api_key- DeepSeek API Key
3. Run
python bot.py
4. Add to Group
- Add the bot to your group
- Set as admin (requires delete messages & ban users permissions)
- Send
/startto test
๐ Usage Guide
User Commands
/start- View bot info/admin- View admin panel (admins only)
Admin Commands
/unban <user_id>- Unban user/unban(reply to message) - Unban the replied user
Super Admin Commands
/add_ad title|link|expiry|weight- Add ad button/all_ad- View all ads/del_ad <ID>- Delete ad/stats- View runtime statistics (detection count, ban rate, etc.)
Ad Button Example:
/add_ad Official Channel|https://t.me/mychannel|2099-12-31 23:59:59|100
โ๏ธ Configuration
Basic Config
telegram:
token: "your-bot-token"
owners: ["your-telegram-id"] # Super admins
allow_any_group: true # Allow any group
groups: [] # Whitelist groups (when allow_any_group=false)
# Language setting
language: "zh" # Options: zh / en
AI Model Selection
ai_model: "deepseek" # Options: openai / qwen / deepseek
Model Comparison:
| Model | Advantage | Price | Image Support |
|---|---|---|---|
| OpenAI | High accuracy | Expensive | โ gpt-4o-mini |
| Qwen | Good for Chinese | Medium | โ qwen-vl-max |
| DeepSeek | Cheap | Lowest | โ |
Detection Strategy
strategy:
joined_days: 3 # Days since joining threshold
min_messages: 3 # Minimum message count
spam_score: 80 # Spam score threshold (0-100)
verification_times: 1 # Verification pass limit
check_message_count: true # Check message count
Message Cleanup
message:
ban_notice_template: |
\#BanAlert
[{masked_name}]({user_link}) Warning: Your username or message violates the rules
โ ๏ธ Identified as high-risk user by AI, permanently banned
Risk score: {score}
๐ Reason:
```
{reason}
```
๐ค AI roast:
```
{mock}
```
delete_ban_notice_after_seconds: 30
delete_welcome_message_after_seconds: 30
Set either value to 0 to disable auto deletion.
Supported ban_notice_template variables:
{masked_name}{user_link}{score}{reason}{mock}{user_id}{chat_id}{channel_url}{group_url}
Unknown placeholders will fall back to the default template instead of breaking moderation.
๐งช Testing
pip install -r requirements-dev.txt
pytest
The first test baseline covers ban-notice template rendering, MarkdownV2 safety, and recent moderation regressions.
Current visible-content extraction covers:
- Text messages
- Photo/media captions
- Quoted snippets
- Replied message text/caption
- Forwarded visible text/caption
Strategy Explanation:
- joined_days: Users who joined more than N days ago skip detection
- min_messages: Users with more than N messages skip detection (requires
check_message_count: true) - verification_times: Users who passed N verifications skip detection (0=unlimited)
- spam_score: Only ban when AI score exceeds this value
Common Scenarios:
Scenario 1: Strict Mode (Check all new users)
strategy:
joined_days: 999999
verification_times: 0
check_message_count: false
Scenario 2: Relaxed Mode (Quick pass)
strategy:
joined_days: 3
min_messages: 3
verification_times: 1
check_message_count: true
Scenario 3: Balanced Mode (Recommended)
strategy:
joined_days: 7
min_messages: 5
verification_times: 2
check_message_count: true
๐จ Ad Button Feature
Ban notifications display custom buttons:
๐ซ Spam Detected
...
โ ๏ธ User has been permanently banned
[๐ Unban] โ Admins can click
[๐ข Official Channel] โ Your ad
[๐ VIP Group] โ Your ad
Manage Ads:
# Add
/add_ad Official Channel|https://t.me/channel|2099-12-31 23:59:59|100
# View
/all_ad
# Delete
/del_ad 1
๐ณ Docker Deployment
# 1. Edit config
cp config.example.yml config.yml
vim config.yml
# 2. Start
docker compose up -d
# 3. View logs
docker compose logs -f
๐ How It Works
User sends text / photo / sticker
โ
Check if admin โ Yes โ Allow
โ No
Check if needs detection โ No โ Allow
โ Yes
Extract visible content
โ
Photo message with caption / quote โ Text pre-check first
โ
AI analyzes content
โ
Score < threshold โ Allow + verification count +1
โ
Score โฅ threshold โ Ban + delete message + send notification
๐ง Advanced Features
Custom Telegram API
If you have a self-hosted Telegram API server:
export TELEGRAM_API_URL="https://your-api-server.com"
python bot.py
Database Location
Default: data/bot.db
Modify:
database:
path: "custom/path/bot.db"
๐ฎ Contact
- ๐จโ๐ป Developer: ็ผๅฅ (@luoyanglang)
- ๐ฆ GitHub: @luoyanglang
- ๐ Official Project: github.com/luoyanglang/AI-Anti-Spam-Bot
- ๐ข Official Channel: @langgefabu
- ๐ฌ Discussion Group: @langgepython
- ๐ฏ Demo Bot: @xiaolangzaibot - Add to your group to test!
๐ฐ Support
If this project helps you, consider supporting the developer:
- โญ Give this project a Star on GitHub
- ๐ข Share with others who might need it
- ๐ Report bugs and suggest features
- โ Buy me a coffee
Your support keeps this project alive
๐ค Contributing
Issues and Pull Requests are welcome!
๐ License
MIT License
๐ Acknowledgments
Thanks to all contributors and users for your support!
Important Notice:
- This project is open source under the MIT License
- Commercial use is allowed, but please keep the developer information
- If you modify or distribute this project, please credit the original author
- This is respect for open source spirit and motivation for continuous development
โญ If this project helps you, please give it a Star!
๐ฐ Support the developer to keep this project alive!