Advanced AI Integration
Relevant source files
The following files were used as context for generating this wiki page:
- README.md
- bot.py
- requirements.txt
- .env.example (referenced via documentation)
- Dockerfile (referenced via documentation)
Advanced AI Integration provides automated text summarization and intelligent content insights for Telegram channels and groups. The system processes chat history or direct user input to generate concise summaries using local extractive algorithms or external AI models like Gemini and ChatGPT.
Sources: README.md:12-16, README.md:139-142
System Architecture
The bot operates as a long-running worker that interacts with the Telegram Bot API via long-polling. It maintains a temporary in-memory buffer of recent messages for each chat to enable context-aware summarization without persistent database requirements in its minimal configuration.
The diagram shows the data flow from a user message to the summarization engine and back to the Telegram interface.
Sources: bot.py:99-112, bot.py:115-127
Summarization Engine
The summarization logic resides in the summarize_text function. It implements a dependency-light, extractive approach by selecting the first few sentences of a given text block.
Local Extractive Logic
When a user requests a summary, the bot performs the following actions:
- Cleans the input text by removing redundant whitespace.
- Splits the text into individual sentences using delimiters (
.,!,?). - Selects a specific number of sentences defined by the
SUMMARY_SENTENCESenvironment variable. - Joins the selected sentences to form the final summary.
Sources: bot.py:68-83
AI Model Support
The platform supports integration with high-level AI providers for advanced insights:
- Gemini & ChatGPT: Used for chat summarization, Q&A from history, and content insights.
- Smart Analytics: Provides member growth and engagement charts alongside AI-generated trend analysis.
Sources: README.md:139-142, README.md:131-133
Configuration and Parameters
The AI behavior and bot operations are controlled via environment variables.
| Variable | Type | Default | Description |
|---|---|---|---|
SUMMARY_SENTENCES | Integer | 5 | Max sentences returned by the local summarizer. |
MAX_STORED_MESSAGES | Integer | 100 | Messages retained per chat for history-based summaries. |
TELEGRAM_BOT_TOKEN | String | None | Authentication token for the Telegram API. |
POLL_TIMEOUT | Integer | 30 | Long-poll timeout duration in seconds. |
Sources: README.md:38-44, bot.py:22-25
Data Management
The bot manages message data using a specialized data structure to ensure memory efficiency.
Message Buffering
The chat_messages object is a dictionary where keys are chat IDs and values are collections.deque objects. This ensures that only the most recent messages (up to MAX_STORED_MESSAGES) are kept, automatically discarding older messages as new ones arrive.
chat_messages: dict[int, deque[str]] = defaultdict(lambda: deque(maxlen=MAX_STORED_MESSAGES))
Sources: bot.py:33
Data Processing Flow
The sequence diagram illustrates how the bot differentiates between commands and general messages for storage.
Sources: bot.py:99-113
Integration with TelePilot MiniApp
While the core bot performs local processing, it is part of a larger ecosystem that includes a MiniApp control panel. This integration provides:
- Per-channel configurations: Specific settings for different groups or channels.
- Audience Segmentation: AI-driven categorization of users (VIP, Active, New, etc.).
- Dynamic Placeholders: Support for
{name},{username}, and{channel}in AI-generated or automated responses.
Sources: README.md:91-93, README.md:104-108, README.md:118-121
Advanced AI Integration allows the Telegram Summary Bot to transform raw chat data into actionable summaries and insights through a combination of local extractive logic and external AI models.