Back to Course|AI Agent Orchestration Mastery: Build, Deploy & Sell Autonomous Systems
Lab

Deploy a Multi-Channel Agent

35 min
Advanced
Unlimited free attempts

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:

  • /start command: Send a welcome message introducing the agent and its capabilities
  • /help command: 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.yaml using 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) -> str function 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 ApplicationBuilder from python-telegram-bot to 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 joined
  • on_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.yaml using 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) -> str function (same pattern as Telegram bot)
  • Handle long responses by splitting into multiple messages (Discord has a 2000-character limit)
  • Use commands.Bot with 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-failure with 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 like model, telegram, discord, deployment
  • For the Telegram bot, the python-telegram-bot library uses Application.builder().token(TOKEN).build() pattern for v20+
  • For the Discord bot, discord.py uses commands.Bot(command_prefix="!", intents=intents) pattern
  • For systemd, remember that environment variables from .env files use EnvironmentFile= directive
  • For monitoring, psutil is a common library for process checks, but you can also use subprocess with pgrep

Grading Rubric

config.yaml defines a complete agent configuration with primary and fallback model settings (name, provider, temperature, max_tokens), API key placeholders using environment variable references, deployment settings (mode, log level, rate limiting, timeout), and per-channel configuration for both Telegram and Discord (enabled flag, allowed IDs, command prefix)15 points
telegram-bot.py implements /start and /help command handlers with informative messages, a message handler that checks rate limits and processes text through the LLM, an error handler that logs errors gracefully, a split_message function for long responses (4096 char limit), and a main() function that builds the Application and starts polling25 points
discord-bot.py implements on_ready event logging connected servers, on_message event with channel routing based on config and rate limiting, help and status commands, a split_message function for long responses (2000 char limit), process_with_llm integration, message statistics tracking, and a main() function that runs the bot25 points
systemd-service.conf defines a valid systemd unit file with [Unit] section (description, network dependency), [Service] section (working directory, ExecStart, EnvironmentFile, Restart=on-failure, RestartSec=10, start limit burst/interval, stdout/stderr to journal, MemoryMax, CPUQuota, NoNewPrivileges=true, ProtectSystem=strict), and [Install] section (WantedBy=multi-user.target)15 points
monitoring.py implements process health checks using subprocess/pgrep, API health check using urllib with timeout, log file parsing that counts errors and warnings and finds last success timestamp, error rate calculation per hour, alert evaluation against configurable thresholds with send_alert function, and a main() function that generates and prints a complete health report20 points

Checklist

0/5

Your Solution

Unlimited free attempts
FREE WEEKLY NEWSLETTER

Stay on the Nerd Track

One email per week — courses, deep dives, tools, and AI experiments.

No spam. Unsubscribe anytime.