MAJOR FIXES: ✅ Fixed UserBot container startup by making TELEGRAM_BOT_TOKEN optional ✅ Broke circular import chain between app modules ✅ Made Config.validate() conditional for UserBot-only mode ✅ Removed unused celery import from userbot_service.py INTEGRATION: ✅ UserBot menu now accessible from main bot /start command ✅ Added 🤖 UserBot button to main keyboard ✅ Integrated userbot_manager.py handlers: - userbot_menu: Main UserBot interface - userbot_settings: Configuration - userbot_collect_groups: Gather all user groups - userbot_collect_members: Parse group members ✅ UserBot handlers properly registered in ConversationHandler CONTAINERS: ✅ tg_autoposter_bot: Running and handling /start commands ✅ tg_autoposter_userbot: Running as standalone microservice ✅ All dependent services (Redis, PostgreSQL, Celery workers) operational STATUS: Bot is fully operational and ready for testing
4.8 KiB
4.8 KiB
🎯 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
- Send
/start - Click "🤖 UserBot"
- Click "⚙️ Настройки" (Settings)
- Click "🔄 Инициализировать" (Initialize)
Step 2: Collect Groups
- In UserBot menu, click "📥 Собрать группы" (Collect Groups)
- Wait for completion
- See list of your groups and member counts
- Data saved to DB automatically
Step 3: Collect Members
- In UserBot menu, click "👥 Собрать участников" (Collect Members)
- Select a group from the list
- Wait for member parsing
- See statistics (total, bots, admins, owners)
- Data saved to DB automatically
File Changes
New Files Created
app/handlers/userbot_manager.py- UserBot control handlersUSERBOT_INTEGRATION_GUIDE.md- Full documentation
Files Modified
app/__init__.py- Added UserBot handlers and ConversationHandler importapp/handlers/__init__.py- Added userbot handler exportsapp/utils/keyboards.py- Added MANAGE_USERBOT callback type, updated main keyboardapp/database/repository.py- Added get_active_groups() and get_group_by_id() methodsapp/userbot/parser.py- Added parse_groups_user_in() method
Key Functions
In app/handlers/userbot_manager.py:
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:
async def parse_groups_user_in() # Get all user's groups
In app/database/repository.py:
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 informationgroup_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:
-
Initialize:
/start → 🤖 UserBot → ⚙️ Settings → 🔄 Initialize -
Collect Groups:
🤖 UserBot → 📥 Collect Groups → ✅ See list -
Collect Members:
🤖 UserBot → 👥 Collect Members → Select group → ✅ See stats -
Verify Data:
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):
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
- ✅ Test the new UserBot menu
- ✅ Verify group collection works
- ✅ Verify member collection works
- ✅ Check database for saved data
- ✅ Deploy to production
Full Documentation
For detailed information, see:
USERBOT_INTEGRATION_GUIDE.md- Complete user manualdocs/USERBOT_MICROSERVICE.md- Technical detailsUSERBOT_README.md- Overview in Russian
Status: ✅ Ready for Testing
Date: 2024
Components: 1 new file + 5 files modified