Automated user filtering bot for Telegram groups that monitors usernames and display names against configurable patterns.
- Group-specific pattern management - Each group maintains separate filter lists
- Real-time monitoring - Checks users on join, name changes (30s window), and messages
- Pattern types - Text, wildcards (
*,?), and regex patterns - Flexible actions - Ban (permanent) or kick (temporary) per group
- Hit tracking - Statistics on pattern matches
- Admin interface - DM menu system for pattern management
- Pattern sharing - Browse and copy patterns between groups
git clone <repository-url>
cd telegram-ban-bot
yarn install-
Environment file - Copy and configure:
cp example.env .env
-
Bot token - Add your Telegram bot token to
.env:BOT_TOKEN=your_bot_token_here
-
Configuration file - Copy and configure:
cp config.example.js config.js
-
User/Group IDs - Edit
config.js:export const WHITELISTED_USER_IDS = [123456789]; // Global admins export const WHITELISTED_GROUP_IDS = [-1001234567890]; // Monitored groups
-
Create directories:
mkdir -p config data/banned_patterns
yarn start/start- Initialize bot and show welcome/menu- Interactive configuration interface/addFilter <pattern>- Add pattern to selected group/removeFilter <pattern>- Remove pattern from selected group/listFilters- Show all patterns for selected group/setaction <ban|kick>- Set action for selected group/testpattern <pattern> <text>- Test pattern matching/hits [pattern]- Show hit statistics/help- Command reference
/chatinfo- Display chat ID and configuration status
| Type | Format | Example | Matches |
|---|---|---|---|
| Text | pattern |
spam |
"SPAM", "spammer", "123spam" |
| Wildcard | *pattern* |
*bot* |
"testbot", "bot_user", "mybot123" |
| Wildcard | pattern* |
evil* |
"evil", "eviluser", "evil123" |
| Wildcard | test? |
test? |
"test1", "testa", "tests" |
| Regex | /pattern/flags |
/^bad.*$/i |
Lines starting with "bad" (case-insensitive) |
-
Global Admins - Users in
WHITELISTED_USER_IDS- Manage all whitelisted groups
- Full configuration access
-
Group Admins - Telegram group administrators
- Manage only their own groups
- Group must be in
WHITELISTED_GROUP_IDS
.
├── bot.js # Main bot logic
├── security.js # Pattern validation and matching
├── config.js # User/group configuration and paths
├── config/
│ ├── settings.json # Runtime settings (auto-generated)
│ └── hit_counters.json # Statistics (auto-generated)
├── data/
│ └── banned_patterns/ # Pattern storage (auto-generated)
│ └── patterns_<groupId>.toml
└── tests/ # Test suite- Pattern validation with length limits (500 chars)
- Regex timeout protection (100ms)
- Control character filtering
- Dangerous regex detection
- Safe compilation with error handling
The bot checks users when they:
- Join a group (immediate check)
- Change username/display name (monitored for 30 seconds)
- Send messages (ongoing check)
Access via /menu in private chat:
- Select target group
- Add/remove patterns
- Toggle ban/kick actions
- Browse patterns from other groups
- Copy patterns between groups
Use specific commands for scripting or quick changes:
/addFilter *scam*
/setaction kick
/listFiltersyarn testOptional environment variable overrides:
BOT_TOKEN- Telegram bot token (required)BANNED_PATTERNS_DIR- Pattern storage directory (default:./data/banned_patterns)SETTINGS_FILE- Settings file path (default:./config/settings.json)
Example service file:
[Unit]
Description=Telegram Ban Bot
After=network.target
[Service]
Type=simple
User=telegram
WorkingDirectory=/path/to/bot
ExecStart=/usr/bin/node bot.js
Restart=always
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target- Group ID verification - Use
/chatinfoto confirm group IDs - Supergroup IDs - Must include
-100prefix in config - Bot permissions - Requires admin privileges with ban permissions
- Pattern testing - Use
/testpatternto verify regex/wildcard behavior - Logs - Console output shows detailed operation information
Use the included update script for production deployments:
./update.shScript performs:
- Git pull from main branch
- Dependency updates
- Service restart
- Preserves local configuration