Overview
The handlers module contains all Telegram message routing and processing logic. Handlers respond to user commands, messages, callbacks, and inline queries, coordinating between the Telegram API and TT-Bot’s core functionality.Handler Routers
TT-Bot uses aiogram’s Router system to organize handlers by functionality:link_router (link_dispatcher.py)
Routes Instagram and TikTok links to appropriate handlers.
Key Components:
IsInstagramLink Filter
Custom filter that detects Instagram URLs in messages.
handle_instagram_message()
Processes Instagram links with queue management and error handling.
Location: handlers/link_dispatcher.py:32
- Per-user queue limits
- Automatic user registration
- Message reactions or status messages (fallback)
- Group chat detection (limits image count to 10)
user_router (user.py)
Handles user commands and settings.
/start Command
Location: handlers/user.py:14
/mode Command
Location: handlers/user.py:30
Toggles file mode (send as file vs. media).
video_router (get_video.py)
Processes TikTok video downloads and unsupported content.
send_tiktok_video()
Location: handlers/get_video.py:88
Main handler for TikTok link processing.
- Validates TikTok URL with regex
- Checks per-user queue limits
- Sends reaction (👀) or status message
- Acquires queue slot via
QueueManager - Fetches video info using TikTok API
- Determines if slideshow or video
- Sends appropriate media type
- Shows advertisement (private chats only)
- Records download to database
["👀", "🤔", "🙏"] - shown during retry attempts
Unsupported Content Handlers
Location:handlers/get_video.py:42-84
Retry Button Callback
Location:handlers/get_video.py:322
music_router (get_music.py)
Extracts and sends TikTok audio/music.
send_tiktok_sound()
Location: handlers/get_music.py:20
- Parses video ID from callback data (
id/{video_id}) - Removes music button from message
- Sets processing reaction
- Fetches music info using 2-part retry strategy
- Sends audio with metadata (title, artist, cover)
- Logs to database
inline_router (get_inline.py)
Handles inline queries and chosen results.
handle_inline_query()
Location: handlers/get_inline.py:38
- TikTok download option (if TikTok link detected)
- Instagram download option (if Instagram link detected)
- “Wrong link” message (if invalid)
- “Start bot” button (if user not registered)
handle_chosen_inline_result()
Location: handlers/get_inline.py:102
- Bypasses per-user queue limits for inline downloads
- Uploads to storage channel (required for inline message edits)
- Supports both videos and slideshows
- Updates inline message with media
lang_router (lang.py)
Manages language selection.
/lang Command
Location: handlers/lang.py:26
Language Selection Callback
Location:handlers/lang.py:38
admin_router (admin.py)
Admin-only commands for bot management.
/msg Command
Location: handlers/admin.py:15
/export Command
Location: handlers/admin.py:32
advert_router (advert.py)
Broadcast message system for announcements.
Location: handlers/advert.py
Commands:
/admin- Opens admin menu/hide- Hides keyboard/stop,/cancel,/back- Returns to main menu
- Message preview
- Message editing
- Bulk send to all users
- Tracks delivery stats (sent, blocked, errors)
- Rate limiting (0.04s delay between messages)
Routing Logic
The dispatcher routes messages based on content type:Queue Management Integration
All download handlers integrate withQueueManager for concurrency control:
Error Handling
Handlers use centralized error handling:TikTokDeletedError→ “Video deleted”TikTokPrivateError→ “Private video”TikTokNetworkError→ “Network error”TikTokRateLimitError→ “Rate limit exceeded”InstagramError→ Instagram-specific messages
Best Practices
- Always acquire queue slots before API calls
- Use reactions for UX feedback (fallback to status messages)
- Clean up resources in
finallyblocks - Check group chat status to limit behavior
- Require admin permissions for group settings changes
- Log all downloads to database for analytics
- Close video_info resources after slideshow processing

