
With reports, logs, profanity filter, anti-spam AI, NSFW detection, reputation system and more :3
- Anti-Profanity: Automatically detects and removes messages containing profanity (Russian/English)
- Anti-Spam: ML-based spam detection for new users
- NSFW Detection: Profile photo analysis for NSFW accounts
- Reputation System: Users gain reputation through positive participation
- Report System: Users can report messages to admins
- Scheduled Announcements: Periodic automated messages
samurai/
βββ bot.py # Main entry point
βββ config/
β βββ __init__.py
β βββ settings.py # Pydantic configuration
β βββ config.toml # Configuration file
βββ core/
β βββ __init__.py
β βββ i18n.py # Fluent internationalization
βββ db/
β βββ __init__.py
β βββ database.py # Database setup
β βββ models/
β βββ member.py # Member model
β βββ spam.py # Spam record model
βββ filters/
β βββ is_owner.py
β βββ is_admin.py
β βββ throttle.py
β βββ .. other useful filters
βββ handlers/
β βββ admin_actions.py # Ban/unban commands
β βββ callbacks.py # Inline button handlers
β βββ exceptions.py # Error handler
β βββ group_events.py # Main message processing
β βββ personal_actions.py# Ping, profanity check
β βββ user_actions.py # Report command
βββ locales/
β βββ en/
β β βββ strings.ftl # English translations
β β βββ announcements.ftl
β βββ ru/
β βββ strings.ftl # Russian translations
β βββ announcements.ftl
βββ middlewares/
β βββ __init__.py
β βββ throttling.py # Middleware for rate limiting
β βββ i18n.py # I18n middleware
βββ services/
β βββ announcements.py # Scheduled announcements
β βββ cache.py # LRU caching
β βββ gender.py # Gender detection
β βββ nsfw.py # NSFW detection
β βββ profanity.py # Profanity detection
β βββ healthcheck.py # Healthcheck server for containers orchestration
β βββ ml_manager.py # Unloads unused ML models from memory after some time
β βββ spam.py # Spam detection
βββ utils/
β βββ helpers.py # Utility functions
β βββ enums.py # Some useful enums to keep the codebase consistent
β βββ localization.py # Localization exports
βββ libs/ # External libraries (censure, gender_extractor)
βββ ruspam_model/ # ML model for spam detection
βββ requirements.txt
βββ Dockerfile
βββ config.py # Configuration of the bot
βββ db_init.py # Use this to initialize your database tables
βββ .env.example
The bot uses Project Fluent for translations.
# Method 1: Import _ function directly
from core.i18n import _
async def handler(message: Message) -> None:
text = _("error-no-reply")
await message.reply(text)
# Method 2: Use i18n from middleware (user's locale)
async def handler(message: Message, i18n: Callable) -> None:
text = i18n("error-no-reply")
await message.reply(text)
# With variables
text = _("report-message", date="2024-01-01", chat_id="123", msg_id="456")- Create/edit
.ftlfiles inlocales/{lang}/ - Use hyphenated keys:
error-no-reply - Variables use
{ $var }syntax
Example locales/ru/strings.ftl:
error-no-reply = ΠΡΠ° ΠΊΠΎΠΌΠ°Π½Π΄Π° Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΡΡ ΠΎΡΠ²Π΅ΡΠΎΠΌ Π½Π° ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅!
report-message = π ΠΡΠΏΡΠ°Π²Π»Π΅Π½ΠΎ { $date }
<a href="https://t.me/c/{ $chat_id }/{ $msg_id }">ΠΠ΅ΡΠ΅ΠΉΡΠΈ</a>- Python 3.11+ is required
- Bot token from @BotFather
-
Clone the repository
-
Install dependencies:
pip install -r requirements.txt
-
Copy
.env.exampleto.envand fill in your values:cp .env.example .env
-
Configure
config.tomlwith your group IDs and other settings how you like -
Run the bot:
python bot.py
-
Enjoy!
For production deployments, you can also set environment variables directly instead of using .env file:
# Export variables directly
export BOT_TOKEN="your_bot_token"
export BOT_OWNER="your_user_id"
export GROUPS_MAIN="-1001234567890"
export DB_URL="sqlite:///./samurai.db"
# Or pass them inline
BOT_TOKEN="..." BOT_OWNER="..." python bot.pyFor systemd services, add them to the unit file:
[Service]
Environment="BOT_TOKEN=your_token"
Environment="BOT_OWNER=123456789"For Docker, use -e flags or --env-file:
docker run -e BOT_TOKEN="..." -e BOT_OWNER="..." samurai-bot
# or
docker run --env-file .env samurai-botThe db_init.py script can be used to create or recreate database tables.
Make sure to backup first if running on an existing database.
# 1. Open db_init.py and comment out or delete this line:
# exit("COMMENT THIS LINE IN ORDER TO RE-INIT DATABASE TABLES")
# 2. Run the script
python db_init.py
# 3. Uncomment the exit() line again to prevent accidental runs or just delete this file after usageUse this script ONLY when:
- Setting up the bot for the first time
- Migrating to a new database
- Resetting all data (development only)
docker build -t samurai-bot .
docker run -d --name samurai-bot -v $(pwd)/config.toml:/app/config.toml samurai-botCurrently bot uses ~800mb of RAM for ML models and for data caching.
Probably we could reduce ML models RAM usage by implementing ONNX runtime models, but that's plans for future updates.
That ain't worked, the only viable solution would be to quantize the models :3
For now, if your server doesn't handle and the process being killed with Out of memory (dmesg | grep -i "killed process"),
simple solution is to add swap:
# Create 2GB swap file
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
# Make permanent
echo '/swapfile none swap sw 0 0' >> /etc/fstab| Variable | Description |
|---|---|
BOT_TOKEN |
Telegram bot token |
BOT_OWNER |
Owner's Telegram user ID |
GROUPS_MAIN |
Main group chat ID (can be a comma separated list) |
GROUPS_REPORTS |
Reports group chat ID |
GROUPS_LOGS |
Logs group chat ID |
LINKED_CHANNEL |
Linked channel ID (can be a comma separated list) |
DB_URL |
Database URL |
| Command | Description |
|---|---|
!rules / /rules |
Request the chat rules |
!report / /report |
Report a message (reply) |
!me / !info |
Show user info |
!Π±Ρ |
Fun command (bot pretends to be scared lol) |
@admin |
Call admin attention |
| Command | Description |
|---|---|
!ban |
Ban user (reply) |
!unban |
Unban user (reply) |
!ping |
Check bot status |
!prof <text> |
Check text for profanity |
| Command | Description |
|---|---|
!spam |
Mark message as spam (reply) |
!reward <points> |
Add reputation points |
!punish <points> |
Remove reputation points |
!setlvl <level> |
Set user level |
!rreset |
Reset user reputation |
!msg <text> |
Send message from bot |
!chatid |
Get current chat ID |
!reload |
Reload announcements from localization files |
!log <text> |
Write test log |
The bot uses two external libraries in the libs/ folder:
- censure: Russian/English profanity detection
- gender_extractor: Gender detection from names
https://github.com/masteroncluster/py-censure - Profanity filter we used as a base
https://github.com/MasterGroosha/telegram-report-bot - Reports system we used as a base
https://huggingface.co/RUSpam/spam_deberta_v4 - Anti-Spam AI model we used as a base
https://github.com/wwydmanski/gender-extractor - Gender detection we used as a base
https://huggingface.co/prithivMLmods/siglip2-x256-explicit-content - Our current NSFW detection model
(C) 2026 Abraham Tugalov