Getting Started (Python Bot)
Relevant source files
The following files were used as context for generating this wiki page:
The Telegram Summary Bot provides a local, dependency-light solution for summarizing Telegram chat messages. This Python-based bot runs as a long-running worker that monitors chat history and generates extractive summaries without requiring external AI API keys.
Sources: README.md:1-12, bot.py:1-10
System Architecture
The bot operates using a long-polling mechanism to retrieve updates from the Telegram Bot API. It maintains an in-memory message buffer for each chat to generate summaries on demand.
Data Flow and Update Loop
The following diagram illustrates how the bot polls for updates and processes incoming messages.
The bot utilizes urllib3 to communicate with the Telegram API and collections.deque to store a limited number of recent messages per chat.
Sources: bot.py:27-28, bot.py:126-140, requirements.txt:2
Installation and Configuration
To run the bot, you must configure a Telegram Bot Token and set up a Python environment.
Environment Variables
The bot relies on specific environment variables for its operation.
| Variable | Required | Default | Description |
|---|---|---|---|
TELEGRAM_BOT_TOKEN | Yes | None | Token obtained from @BotFather. |
LOG_LEVEL | No | INFO | Python logging level (e.g., DEBUG, INFO). |
POLL_TIMEOUT | No | 30 | Long-poll timeout in seconds for API requests. |
MAX_STORED_MESSAGES | No | 100 | Maximum recent messages retained per chat. |
SUMMARY_SENTENCES | No | 5 | Sentence count returned by the summarizer. |
Sources: README.md:31-41, bot.py:20-24, .env.example
Local Setup Steps
- Create a Python virtual environment:
python3 -m venv .venv. - Activate the environment:
source .venv/bin/activate. - Install dependencies:
pip install -r requirements.txt. - Configure environment:
cp .env.example .envand insert yourTELEGRAM_BOT_TOKEN. - Execute the bot:
python bot.py.
Sources: README.md:19-25, requirements.txt:1-6
Core Logic and Commands
Summarization Logic
The summarize_text function implements a local extractive summarizer. It cleans the input text, splits it into sentences based on punctuation, and joins the first $N$ sentences, where $N$ is defined by SUMMARY_SENTENCES.
def summarize_text(text: str) -> str:
cleaned = " ".join(text.split())
if not cleaned:
return "No text found to summarize."
sentences = [
sentence.strip()
for sentence in cleaned.replace("!", ".").replace("?", ".").split(".")
if sentence.strip()
]
if not sentences:
return cleaned[:1000]
summary = ". ".join(sentences[:SUMMARY_SENTENCES])
return f"{summary}."
Sources: bot.py:68-84
Bot Commands
The bot responds to specific slash commands to interact with users.
| Command | Action |
|---|---|
/start | Returns a health check message confirming the bot is active. |
/help | Displays a list of available commands and usage tips. |
/summary <text> | Directly summarizes the text provided immediately after the command. |
/summary | Summarizes the cached history of the current chat. |
Sources: bot.py:101-118, README.md:7-11
Deployment Options
Docker Deployment
You can containerize the bot for consistent deployment across environments.
docker build -t telegram-summary-bot .
docker run --env-file .env --restart unless-stopped telegram-summary-bot
Sources: README.md:43-49, Dockerfile
Service Configuration (systemd)
For VPS deployment, the bot can run as a systemd service to ensure high availability and automatic restarts.
[Service]
WorkingDirectory=/opt/telegram-summary-bot
EnvironmentFile=/opt/telegram-summary-bot/.env
ExecStart=/opt/telegram-summary-bot/.venv/bin/python /opt/telegram-summary-bot/bot.py
Restart=always
Sources: README.md:58-71
The Telegram Summary Bot provides a streamlined way to manage chat information through local summarization. By following these setup and deployment steps, you can integrate the bot into various environments ranging from local development to production servers.