Skip to content

Commit 377d474

Browse files
hendisantikaclaude
andcommitted
📝 docs: update README for multi-group support and current test coverage
Add multi-group feature section, groups.json setup guide, updated project structure with new files (group_config.py, check.py), and refresh test stats from 404 to 440 tests at 99% coverage (1,381 statements). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ea81972 commit 377d474

1 file changed

Lines changed: 74 additions & 12 deletions

File tree

README.md

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ A comprehensive Telegram bot for managing group members with profile verificatio
2323
- **New user probation**: New members restricted from sending links/forwarded messages for 3 days (configurable)
2424
- **Anti-spam enforcement**: Tracks violations and restricts spammers after threshold
2525

26+
### Multi-Group Support
27+
- **Single bot instance** manages multiple Telegram groups simultaneously
28+
- **Per-group configuration** via `groups.json` with independent settings per group
29+
- **Fallback to `.env`** when `groups.json` is not present (single-group mode)
30+
- **Independent settings**: Each group has its own warning topic, thresholds, captcha, and probation config
31+
2632
### Admin Tools
2733
- **/verify command**: Whitelist users with hidden profile pictures (DM only)
2834
- **/unverify command**: Remove users from verification whitelist (DM only)
35+
- **/check command**: Check user profiles via DM
2936
- **Inline verification**: Forward messages to bot for quick verify/unverify buttons
3037
- **Automatic clearance**: Sends notification when verified users' warnings are cleared
3138

@@ -85,6 +92,8 @@ A comprehensive Telegram bot for managing group members with profile verificatio
8592

8693
### 5. Configure Environment
8794

95+
#### Single-Group Mode
96+
8897
```bash
8998
cp .env.example .env
9099
```
@@ -101,6 +110,47 @@ WARNING_TIME_THRESHOLD_MINUTES=180
101110
RULES_LINK=https://t.me/yourgroup/rules
102111
```
103112

113+
#### Multi-Group Mode
114+
115+
To manage multiple groups with a single bot instance, create a `groups.json` file:
116+
117+
```bash
118+
cp groups.json.example groups.json
119+
```
120+
121+
Edit `groups.json` with your groups:
122+
123+
```json
124+
[
125+
{
126+
"group_id": -1001234567890,
127+
"warning_topic_id": 123,
128+
"restrict_failed_users": false,
129+
"warning_threshold": 3,
130+
"warning_time_threshold_minutes": 180,
131+
"captcha_enabled": false,
132+
"captcha_timeout_seconds": 120,
133+
"new_user_probation_hours": 72,
134+
"new_user_violation_threshold": 3,
135+
"rules_link": "https://t.me/yourgroup/rules"
136+
},
137+
{
138+
"group_id": -1009876543210,
139+
"warning_topic_id": 456,
140+
"restrict_failed_users": true,
141+
"warning_threshold": 5,
142+
"warning_time_threshold_minutes": 60,
143+
"captcha_enabled": true,
144+
"captcha_timeout_seconds": 180,
145+
"new_user_probation_hours": 168,
146+
"new_user_violation_threshold": 2,
147+
"rules_link": "https://t.me/mygroup/rules"
148+
}
149+
]
150+
```
151+
152+
Each group has independent settings. When `groups.json` exists, the per-group `GROUP_ID`/`WARNING_TOPIC_ID`/etc. fields in `.env` are ignored. You still need `TELEGRAM_BOT_TOKEN`, `DATABASE_PATH`, and logging settings in `.env`.
153+
104154
## Installation
105155

106156
```bash
@@ -150,14 +200,14 @@ uv run pytest -v
150200
### Test Coverage
151201

152202
The project maintains comprehensive test coverage:
153-
- **Coverage**: 99% (1,216 statements)
154-
- **Tests**: 404 total
155-
- **Pass Rate**: 100% (404/404 passed)
156-
- **All modules**: 100% coverage including JobQueue scheduler integration, captcha verification, and anti-spam enforcement
203+
- **Coverage**: 99% (1,381 statements)
204+
- **Tests**: 440 total
205+
- **Pass Rate**: 100% (440/440 passed)
206+
- **All modules**: 100% coverage including JobQueue scheduler integration, captcha verification, anti-spam enforcement, and multi-group configuration
157207
- Services: `bot_info.py` (100%), `scheduler.py` (100%), `user_checker.py` (100%), `telegram_utils.py` (100%), `captcha_recovery.py` (100%)
158208
- Handlers: `anti_spam.py` (100%), `captcha.py` (100%), `check.py` (100%), `dm.py` (100%), `message.py` (100%), `topic_guard.py` (100%), `verify.py` (100%)
159-
- Database: `service.py` (100%), `models.py` (100%)
160-
- Config: `config.py` (100%)
209+
- Database: `service.py` (97%), `models.py` (100%)
210+
- Config: `config.py` (100%), `group_config.py` (100%)
161211
- Constants: `constants.py` (100%)
162212

163213
All modules are fully unit tested with:
@@ -167,6 +217,7 @@ All modules are fully unit tested with:
167217
- Background job testing (JobQueue integration, job configuration, auto-restriction logic)
168218
- Captcha verification flow (new member handling, callback verification, timeout handling)
169219
- Anti-spam protection (forwarded messages, URL whitelisting, external replies)
220+
- Multi-group configuration (registry, per-group settings, fallback to `.env`)
170221

171222
## Project Structure
172223

@@ -175,6 +226,8 @@ PythonID/
175226
├── pyproject.toml
176227
├── .env # Your configuration (not committed)
177228
├── .env.example # Example configuration
229+
├── groups.json # Multi-group config (optional, not committed)
230+
├── groups.json.example # Multi-group config template
178231
├── README.md
179232
├── data/
180233
│ └── bot.db # SQLite database (auto-created)
@@ -183,25 +236,30 @@ PythonID/
183236
│ ├── test_bot_info.py
184237
│ ├── test_captcha.py
185238
│ ├── test_captcha_recovery.py
239+
│ ├── test_check.py
186240
│ ├── test_config.py
187241
│ ├── test_constants.py
188242
│ ├── test_database.py
189243
│ ├── test_dm_handler.py
244+
│ ├── test_group_config.py # Multi-group config tests
190245
│ ├── test_message_handler.py
191246
│ ├── test_photo_verification.py
192-
│ ├── test_scheduler.py # JobQueue scheduler tests
247+
│ ├── test_scheduler.py # JobQueue scheduler tests
193248
│ ├── test_telegram_utils.py
194249
│ ├── test_topic_guard.py
195250
│ ├── test_user_checker.py
196-
│ └── test_verify_handler.py
251+
│ ├── test_verify_handler.py
252+
│ └── test_whitelist.py
197253
└── src/
198254
└── bot/
199255
├── main.py # Entry point with JobQueue integration
200256
├── config.py # Pydantic settings
257+
├── group_config.py # Multi-group configuration & registry
201258
├── constants.py # Shared constants
202259
├── handlers/
203260
│ ├── anti_spam.py # Anti-spam handler for probation users
204261
│ ├── captcha.py # Captcha verification handler
262+
│ ├── check.py # /check command & forwarded message handler
205263
│ ├── dm.py # DM unrestriction handler
206264
│ ├── message.py # Group message handler
207265
│ ├── topic_guard.py # Warning topic protection
@@ -224,7 +282,8 @@ The following diagram illustrates the complete bot workflow including captcha ve
224282
```mermaid
225283
flowchart TD
226284
Start([Bot Starts]) --> Init[Initialize Database & Config]
227-
Init --> FetchAdmins[Fetch Group Admin IDs]
285+
Init --> LoadGroups[Load Group Registry<br/>from groups.json or .env]
286+
LoadGroups --> FetchAdmins[Fetch Admin IDs<br/>for All Groups]
228287
FetchAdmins --> RecoverCaptcha{Captcha<br/>Enabled?}
229288
RecoverCaptcha -->|Yes| RecoverPending[Recover Pending Captchas]
230289
RecoverCaptcha -->|No| StartJobs
@@ -380,7 +439,7 @@ flowchart TD
380439
classDef endNode fill:#e94560,stroke:#16213e,color:#eee
381440
classDef startNode fill:#1a5f7a,stroke:#16213e,color:#eee
382441
383-
class Init,FetchAdmins,RecoverPending,StartJobs,Poll,CheckProfile,CheckDMProfile,RestrictAndChallenge,StorePending,ScheduleTimeout,WaitCaptcha,StartProbation,StartProbationAfter processNode
442+
class Init,LoadGroups,FetchAdmins,RecoverPending,StartJobs,Poll,CheckProfile,CheckDMProfile,RestrictAndChallenge,StorePending,ScheduleTimeout,WaitCaptcha,StartProbation,StartProbationAfter processNode
384443
class UpdateType,RecoverCaptcha,TopicGuard,IsAdmin,CheckBot,CheckWhitelist,ProfileComplete,CheckMode,CheckCount,CheckInGroup,CheckPendingCaptcha,DMProfileComplete,CheckBotRestricted,CheckCurrentStatus,HasExpired,CheckKicked,NextUser,CheckAdminVerify,CheckAdminUnverify,CaptchaAnswer,CheckCaptchaEnabled,CheckProbation,CheckExpired,CheckViolation,CheckWhitelisted,ViolationCount,CheckWarningsExist,CheckAdminForward,ExtractUser decisionNode
385444
class IncrementDB,SilentIncrement,MarkRestricted,ClearRecord,ClearRecord2,QueryDB,ClearKicked,MarkTimeRestricted,AddWhitelist,RemoveWhitelist,IncrementViolation,ClearProbation,DeleteWarnings dataNode
386445
class DeleteMsg,SendWarning,SendFirstWarning,RestrictUser,SendRestrictionMsg,SendNotInGroup,SendCaptchaRedirect,SendMissing,SendNoRestriction,SendAlreadyUnrestricted,UnrestrictUser,SendSuccess,ApplyTimeRestriction,SendTimeNotice,SchedulerJob,SendVerifySuccess,SendUnverifySuccess,DenyVerify,DenyUnverify,UnrestrictMember,KickMember,UpdateMessage,CancelTimeout,ShowError,DeleteSpam,SendSpamWarning,RestrictSpammer,SendSpamRestriction,UnrestrictVerified,SendClearance,DenyForward,SendButtons,SendExtractError,ProcessVerify,ProcessUnverify actionNode
@@ -395,13 +454,17 @@ flowchart TD
395454
The bot is organized into clear modules for maintainability:
396455

397456
- **main.py**: Entry point with python-telegram-bot's JobQueue integration and graceful shutdown
457+
- **config.py**: Environment configuration using Pydantic
458+
- **group_config.py**: Multi-group configuration with `GroupConfig` and `GroupRegistry` (singleton pattern for O(1) group lookup)
459+
- **constants.py**: Centralized message templates and utilities for consistent formatting across handlers
398460
- **handlers/**: Message processing logic
399461
- `message.py`: Monitors group messages and sends warnings/restrictions
400462
- `dm.py`: Handles DM unrestriction flow
401463
- `topic_guard.py`: Protects warning topic from unauthorized messages
402464
- `captcha.py`: Captcha verification for new members
403465
- `anti_spam.py`: Anti-spam enforcement for users on probation
404466
- `verify.py`: /verify and /unverify command handlers
467+
- `check.py`: /check command and forwarded message handler
405468
- **services/**: Business logic and utilities
406469
- `scheduler.py`: JobQueue background job that runs every 5 minutes for time-based auto-restrictions
407470
- `user_checker.py`: Profile validation (photo + username check)
@@ -411,8 +474,6 @@ The bot is organized into clear modules for maintainability:
411474
- **database/**: Data persistence
412475
- `service.py`: Database operations with SQLite
413476
- `models.py`: Data models using SQLModel (UserWarning, PhotoVerificationWhitelist, PendingCaptchaValidation, NewUserProbation)
414-
- **config.py**: Environment configuration using Pydantic
415-
- **constants.py**: Centralized message templates and utilities for consistent formatting across handlers
416477

417478
### Group Message Monitoring
418479
1. Bot listens to all text messages in the configured group
@@ -481,6 +542,7 @@ When a restricted user DMs the bot (or sends `/start`):
481542
| `NEW_USER_VIOLATION_THRESHOLD` | Spam violations before restriction | `3` |
482543
| `DATABASE_PATH` | SQLite database path | `data/bot.db` |
483544
| `RULES_LINK` | Link to group rules message | `https://t.me/pythonID/290029/321799` |
545+
| `GROUPS_CONFIG_PATH` | Path to `groups.json` for multi-group support | `groups.json` |
484546
| `LOGFIRE_ENABLED` | Enable Logfire logging integration | `true` |
485547
| `LOGFIRE_TOKEN` | Logfire API token (optional) | None |
486548
| `LOG_LEVEL` | Logging level (DEBUG/INFO/WARNING/ERROR) | `INFO` |

0 commit comments

Comments
 (0)