VPS Deployment (systemd)
Relevant source files
The following files were used as context for generating this wiki page:
VPS Deployment using systemd configures the Telegram Summary Bot as a persistent background service on a Linux server. This method ensures the bot starts automatically on boot and restarts if the process crashes.
The deployment relies on bot.py as the entry point and a .env file for configuration. The systemd service manages the Python environment and execution lifecycle, providing a stable alternative to manual execution or Docker-based setups.
Sources: README.md:85-110, bot.py:1-5
Service Architecture
The systemd deployment integrates the Linux service manager with the Python application environment. The service executes the bot script using a dedicated virtual environment to isolate dependencies.
The diagram shows the hierarchy from the operating system down to the active bot process.
Sources: README.md:100-110, bot.py:130-142
Deployment Steps
To deploy the bot on a VPS using systemd, follow these procedures:
- Clone the repository to the
/opt/telegram-summary-botdirectory. - Create a virtual environment and install dependencies listed in
requirements.txt. - Create a
.envfile containing theTELEGRAM_BOT_TOKEN. - Create the service definition file at
/etc/systemd/system/telegram-summary-bot.service. - Enable and start the service using
systemctl.
Sources: README.md:85-98, requirements.txt:1-6
Service Configuration
The systemd service file defines how the bot process is managed. Key parameters include the working directory, the path to the Python executable within the virtual environment, and restart policies.
| Parameter | Value / Path | Description |
|---|---|---|
WorkingDirectory | /opt/telegram-summary-bot | The root directory of the bot source code. |
EnvironmentFile | /opt/telegram-summary-bot/.env | Path to the file containing secrets and config. |
ExecStart | /opt/telegram-summary-bot/.venv/bin/python | Path to the venv Python interpreter. |
Restart | always | Policy to restart the bot on failure. |
RestartSec | 5 | Delay between restart attempts. |
Sources: README.md:100-110
Unit Definition Example
[Unit]
Description=Telegram Summary Bot
After=network.target
[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
RestartSec=5
[Install]
WantedBy=multi-user.target
Sources: README.md:100-110
Runtime Configuration
The bot process reads configuration from environment variables loaded by the systemd service. These variables control the behavior of the polling mechanism and the summarization engine.
| Variable | Default | Logic |
|---|---|---|
TELEGRAM_BOT_TOKEN | None | Authenticates the bot with Telegram API. |
POLL_TIMEOUT | 30 | Sets the long-polling duration for getUpdates. |
MAX_STORED_MESSAGES | 100 | Defines the deque maxlen for chat history. |
SUMMARY_SENTENCES | 5 | Limits the number of sentences in a summary. |
Sources: bot.py:19-24, README.md:54-62
Process Lifecycle and Signals
The bot implements graceful shutdown handling to ensure the process exits cleanly when stopped by systemd.
The sequence diagram illustrates how handle_shutdown catches system signals to stop the polling loop.
Sources: bot.py:38-41, bot.py:121-128, bot.py:138-142
Summary
VPS deployment via systemd provides a robust production environment for the Telegram Summary Bot. By utilizing an EnvironmentFile and Restart logic, the bot maintains high availability and secure configuration management. This setup allows the bot to handle message polling and text summarization continuously without manual intervention.
Sources: README.md:85-115, bot.py:140-142