# 🎯 UserBot Integration - Quick Start ## What's New? UserBot management is now integrated into the main Telegram bot. You can: - βœ… Manage UserBot settings - βœ… Collect all groups you're a member of - βœ… Parse members from selected groups - βœ… Save everything to PostgreSQL ## Menu Structure ``` /start β”‚ └─ Main Menu πŸ€– β”œβ”€ πŸ“¨ Messages (existing) β”œβ”€ πŸ‘₯ Groups (existing) └─ πŸ€– UserBot ← NEW! β”œβ”€ βš™οΈ Settings β”‚ └─ Check status / Initialize β”œβ”€ πŸ“₯ Collect Groups β”‚ └─ Get all groups you're in └─ πŸ‘₯ Collect Members └─ Select group β†’ Parse members ``` ## Three Simple Steps ### Step 1: Initialize UserBot 1. Send `/start` 2. Click "πŸ€– UserBot" 3. Click "βš™οΈ Настройки" (Settings) 4. Click "πŸ”„ Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ" (Initialize) ### Step 2: Collect Groups 1. In UserBot menu, click "πŸ“₯ Π‘ΠΎΠ±Ρ€Π°Ρ‚ΡŒ Π³Ρ€ΡƒΠΏΠΏΡ‹" (Collect Groups) 2. Wait for completion 3. See list of your groups and member counts 4. Data saved to DB automatically ### Step 3: Collect Members 1. In UserBot menu, click "πŸ‘₯ Π‘ΠΎΠ±Ρ€Π°Ρ‚ΡŒ участников" (Collect Members) 2. Select a group from the list 3. Wait for member parsing 4. See statistics (total, bots, admins, owners) 5. Data saved to DB automatically ## File Changes ### New Files Created - `app/handlers/userbot_manager.py` - UserBot control handlers - `USERBOT_INTEGRATION_GUIDE.md` - Full documentation ### Files Modified - `app/__init__.py` - Added UserBot handlers and ConversationHandler import - `app/handlers/__init__.py` - Added userbot handler exports - `app/utils/keyboards.py` - Added MANAGE_USERBOT callback type, updated main keyboard - `app/database/repository.py` - Added get_active_groups() and get_group_by_id() methods - `app/userbot/parser.py` - Added parse_groups_user_in() method ## Key Functions ### In `app/handlers/userbot_manager.py`: ```python async def userbot_menu() # Main UserBot menu async def userbot_settings() # Settings dialog async def userbot_init() # Initialize UserBot async def userbot_collect_groups() # Collect groups async def userbot_collect_members() # Select group for parsing async def userbot_parse_members() # Parse members of group async def cancel_userbot() # Cancel operation ``` ### In `app/userbot/parser.py`: ```python async def parse_groups_user_in() # Get all user's groups ``` ### In `app/database/repository.py`: ```python async def get_active_groups() # Get active groups async def get_group_by_id() # Get group by ID ``` ## Database ### New/Modified Tables - `groups` - Stores group information - `group_members` - Stores member information ### Data Saved **Groups:** - chat_id, title, description - members_count, is_active - created_at, updated_at **Members:** - user_id, username, first_name, last_name - is_bot, is_admin, is_owner - group_id (reference to group) ## Testing Before deploying, test: 1. **Initialize:** ``` /start β†’ πŸ€– UserBot β†’ βš™οΈ Settings β†’ πŸ”„ Initialize ``` 2. **Collect Groups:** ``` πŸ€– UserBot β†’ πŸ“₯ Collect Groups β†’ βœ… See list ``` 3. **Collect Members:** ``` πŸ€– UserBot β†’ πŸ‘₯ Collect Members β†’ Select group β†’ βœ… See stats ``` 4. **Verify Data:** ```bash docker-compose exec postgres psql -U admin -d tg_autoposter SELECT COUNT(*) FROM groups; SELECT COUNT(*) FROM group_members; \q ``` ## Workflow States The UserBot manager uses these states (in `userbot_manager.py`): ```python USERBOT_MENU = 1 USERBOT_SETTINGS = 2 USERBOT_COLLECTING_GROUPS = 3 USERBOT_SELECT_GROUP = 4 USERBOT_COLLECTING_MEMBERS = 5 ``` ## Error Handling The integration includes error handling for: - ❌ UserBot not initialized β†’ Prompt to initialize - ❌ FloodWait errors β†’ Graceful retry - ❌ Permission errors β†’ Inform user - ❌ No groups found β†’ Clear message - ❌ Database errors β†’ Error reporting ## Callbacks New callbacks added: ``` userbot_menu - Main menu userbot_settings - Settings dialog userbot_init - Initialize UserBot userbot_collect_groups - Collect groups userbot_collect_members - Select group userbot_members_\d+ - Parse specific group ``` ## Next Steps 1. βœ… Test the new UserBot menu 2. βœ… Verify group collection works 3. βœ… Verify member collection works 4. βœ… Check database for saved data 5. βœ… Deploy to production ## Full Documentation For detailed information, see: - `USERBOT_INTEGRATION_GUIDE.md` - Complete user manual - `docs/USERBOT_MICROSERVICE.md` - Technical details - `USERBOT_README.md` - Overview in Russian --- **Status:** βœ… Ready for Testing **Date:** 2024 **Components:** 1 new file + 5 files modified