Overview
The Telegram Bot Interface provides a conversational interface for the Lead Intelligence Engine. Users can analyze business URLs, check system status, and view model information through Telegram commands.Configuration
The bot requires a Telegram Bot Token from @BotFather:Rate Limiting
The bot implements in-memory rate limiting to prevent abuse:Configuration Constants
Time window for rate limiting in minutes
Maximum analysis requests allowed per user within the time window
Rate Limit Storage
is_rate_limited()
Checks if a user has exceeded the rate limit.
Telegram user ID to check
Returns
True if user has exceeded rate limit, False otherwiseImplementation
Rate limits are per-user and reset after the time window expires. The system only counts successful analysis requests.
Bot Commands
/start
Sends a welcome message with available commands.
Handler: start()
Response:
~/workspace/source/telegram_bot.py:40
/analyze <url>
Analyzes a business website and stores results in CRM.
Handler: analyze_command()
Business website or social media URL to analyze. Must be provided as command argument.
Response Flow
-
Validation: Checks if URL argument is provided
-
Rate Limit Check: If user exceeded limits
-
Processing Message:
-
Success Response:
-
Duplicate Detection:
-
Error Response:
Facebook URLs receive a special error message: “Facebook public metadata unavailable. Please provide website link if available.”
~/workspace/source/telegram_bot.py:52
/model
Displays the current LLM model being used by the AI Evaluator.
Handler: model_command()
Response:
~/workspace/source/telegram_bot.py:113
/status
Shows comprehensive AI system status including quota, token usage, and activity.
Handler: status_command()
Response:
Status Indicators
[ONLINE]- System operational, quota available[OFFLINE/LIMITED]- Rate limit reached or out of tokens
WITHIN LIMITS- Normal operationRATE LIMIT REACHED / OUT OF TOKENS- Service degraded
~/workspace/source/telegram_bot.py:124
Plain Text URLs
The bot also accepts URLs sent as plain text messages (without/analyze command).
Handler: handle_text_url()
Behavior:
- URLs starting with
http://orhttps://are automatically processed - Non-URL text receives response:
"I only analyze URLs. Use /analyze <url> or just send the link."
~/workspace/source/telegram_bot.py:63
Core Processing Function
process_lead_analysis()
Core logic for processing lead analysis requests. Used by both /analyze command and plain text URL handler.
Telegram Update object containing message context
Business URL to analyze
Processing Steps
- Rate Limit Check: Validates user hasn’t exceeded request quota
- Status Message: Sends “Analyzing…” message to user
- Lead Engine Execution: Creates
LeadEngine()instance and callsprocess_url() - Result Handling:
- Checks for duplicate URLs (
_status == "skipped") - Formats success response with business insights
- Handles errors with user-friendly messages
- Checks for duplicate URLs (
- Message Update: Edits status message with final result
~/workspace/source/telegram_bot.py:73
Error Handling
Global Error Handler
- Logs error with full traceback to logger
- Sends generic error message to user
~/workspace/source/telegram_bot.py:160
Command-Specific Error Handling
Model Command:- Facebook-specific: “Facebook public metadata unavailable. Please provide website link if available.”
- Generic:
f"Error processing {url}:\n\n{error_msg}"
Message Formatting
The bot uses Telegram’s Markdown formatting:- Bold:
**text** - Code:
`text` - Italic:
_text_
parse_mode='Markdown'.
Logging Configuration
Minimal logging setup (WARNING level only):- Analysis errors (from exception handlers)
- Status command errors
- Unhandled exceptions (via error_handler)
Application Lifecycle
The bot runs in polling mode:Handlers are registered in order. Command handlers take precedence over the text message handler due to the
~filters.COMMAND filter.Integration with Lead Engine
The bot creates a newLeadEngine() instance for each request:
- Fresh state per analysis
- No cross-contamination between user requests
- Latest configuration and credentials loaded