Deploy a Multi-Channel Agent
Instructions
In this lab, you will configure and deploy a multi-channel AI agent that can respond to users across Telegram and Discord simultaneously. You will set up the agent configuration, build bot handlers for each platform, create a systemd service for 24/7 operation, and implement a monitoring script to keep everything running smoothly.
This is a real-world deployment pattern used by production AI agents. By the end of this lab, you will have a complete deployment blueprint that you can adapt to your own projects.
Architecture Overview
+-------------------+ +-------------------+
| Telegram Bot | | Discord Bot |
| (telegram-bot.py)| | (discord-bot.py) |
+--------+----------+ +--------+----------+
| |
v v
+-------------------------------------------+
| Agent Core (config.yaml) |
| Model selection, API keys, settings |
+-------------------------------------------+
|
v
+-------------------------------------------+
| systemd Service (systemd-service.conf)|
| Auto-restart, logging, process mgmt |
+-------------------------------------------+
|
v
+-------------------------------------------+
| Monitoring (monitoring.py) |
| Health checks, log parsing, alerts |
+-------------------------------------------+
Step 1: Agent Configuration (config.yaml)
Create a central configuration file that both bots will read from:
Model Configuration:
- Select a primary LLM model for generating responses
- Select a fallback model in case the primary is unavailable
- Set temperature, max tokens, and other generation parameters
API Keys (Placeholders):
- Telegram bot token placeholder
- Discord bot token placeholder
- LLM API key placeholder
- Use environment variable references (e.g.,
${TELEGRAM_BOT_TOKEN}) rather than hardcoded values
Deployment Settings:
- Deployment mode (development, staging, production)
- Log level (debug, info, warning, error)
- Rate limiting settings (max requests per user per minute)
- Response timeout in seconds
Channel Configuration:
- For each channel (Telegram, Discord): enabled/disabled flag, allowed chat IDs or channel IDs, command prefix
Step 2: Telegram Bot Handler (telegram-bot.py)
Build a Telegram bot using the python-telegram-bot library pattern:
Required Handlers:
/startcommand: Send a welcome message introducing the agent and its capabilities/helpcommand: List all available commands and usage instructions- Message handler: Process incoming text messages, send them to the LLM, and return the response
- Error handler: Catch and log errors gracefully without crashing the bot
Implementation Details:
- Load configuration from
config.yamlusing PyYAML - Implement rate limiting per user (check config for max requests per minute)
- Add logging with timestamps for every incoming message and outgoing response
- Include a
process_with_llm(message: str) -> strfunction that sends the user message to the configured LLM - Handle long responses by splitting them into multiple messages (Telegram has a 4096-character limit)
Bot Setup:
- Use
ApplicationBuilderfrompython-telegram-botto create the bot application - Register all handlers with appropriate filters
- Include a
main()function that starts polling
Step 3: Discord Bot Handler (discord-bot.py)
Build a Discord bot using the discord.py library pattern:
Required Events:
on_ready: Log that the bot is connected and list the servers it has joinedon_message: Process incoming messages, ignore bot's own messages, send to LLM, and reply- Channel routing: Only respond in configured channels (check config for allowed channel IDs)
Implementation Details:
- Load configuration from
config.yamlusing PyYAML - Implement rate limiting per user (check config for max requests per minute)
- Add logging with timestamps for every incoming message and outgoing response
- Include a
process_with_llm(message: str) -> strfunction (same pattern as Telegram bot) - Handle long responses by splitting into multiple messages (Discord has a 2000-character limit)
- Use
commands.Botwith a configurable command prefix from config
Commands:
!help(or configured prefix + help): List available commands!status: Show bot uptime and number of messages processed- Regular messages in allowed channels: Process with LLM and reply
Step 4: systemd Service File (systemd-service.conf)
Create a systemd service unit file for running the agent 24/7 on a Linux server:
Service Configuration:
[Unit]section: Description, documentation URL, dependency on network[Service]section:- Working directory for the agent code
- Python executable path and entry point script
- Environment file for loading API keys (e.g.,
/etc/agent/.env) - Restart policy:
on-failurewith a 10-second delay - Maximum restart attempts (5 restarts within 300 seconds)
- Logging to stdout/stderr (captured by journald)
- Resource limits: memory limit, CPU quota
- Security hardening:
NoNewPrivileges=true,ProtectSystem=strict
[Install]section: Enable on multi-user target
Step 5: Monitoring Script (monitoring.py)
Build a health check and monitoring script:
Health Checks:
- Check if the Telegram bot process is running (by PID file or process name)
- Check if the Discord bot process is running
- Check if the LLM API is reachable (HTTP health check with timeout)
- Report overall system health status
Log Parsing:
- Read the last N lines from the agent log file
- Count errors and warnings in the recent logs
- Extract the timestamp of the last successful response
- Calculate error rate (errors per hour)
Alerting:
- Define alert thresholds (e.g., error rate > 10/hour, process down, API unreachable)
- Print alert messages to stdout with severity levels
- Include a
send_alert(message: str, severity: str)function that can be extended to send notifications (email, Slack, etc.)
Report Generation:
- Generate a health report with: uptime, messages processed, error count, API latency, memory usage
- Include a
main()function that runs all checks and prints the report
What to Submit
The editor has 5 file sections with TODO comments. Replace each TODO with your code. The AI grader will evaluate each section against the rubric.
Hints
- For
config.yaml, use a clean hierarchical structure. Group related settings under top-level keys likemodel,telegram,discord,deployment - For the Telegram bot, the
python-telegram-botlibrary usesApplication.builder().token(TOKEN).build()pattern for v20+ - For the Discord bot,
discord.pyusescommands.Bot(command_prefix="!", intents=intents)pattern - For systemd, remember that environment variables from
.envfiles useEnvironmentFile=directive - For monitoring,
psutilis a common library for process checks, but you can also usesubprocesswithpgrep