Skip to main content
This example demonstrates how to build a Telegram-controlled DevOps agent using Upsonic’s AutonomousAgent. The agent treats a workspace as its home: it reads AGENTS.md for behavior, SOUL.md for identity, USER.md for context, and uses daily plus long-term memory. It can check disk usage, analyze logs, create backups, and run read-only or controlled shell commands β€” all from Telegram, with optional locking to your user ID.

Overview

Upsonic’s AutonomousAgent is wired to Telegram via TelegramInterface in CHAT mode so it keeps conversation context. The stack is minimal:
  1. AutonomousAgent β€” LLM-driven agent with filesystem and shell tools, sandboxed to a workspace directory
  2. Workspace β€” Agent’s home: AGENTS.md (playbook), SOUL.md (identity), USER.md (who you are), MEMORY.md (long-term), memory/YYYY-MM-DD.md (daily logs)
  3. Telegram β€” Chat UI; webhook receives updates, FastAPI serves the webhook
  4. ngrok β€” Exposes localhost so Telegram can reach your bot
The agent follows a DevOps playbook defined in AGENTS.md: log analysis (tail, grep, summarize), system checks (df, free, ps), backups (tar to backups/ with a fixed naming pattern), incident response (severity, evidence, log to memory). It prefers trash over rm, never pipes unknown input to bash, and asks before editing app code or restarting services.

Project Structure

Environment Variables

Configure the bot and LLM via environment variables (e.g. in .env):

Installation

Option A: Setup script
Option B: Manual
Create .env from .env.example and set TELEGRAM_BOT_TOKEN, TELEGRAM_WEBHOOK_URL, and OPENAI_API_KEY.

Usage

1. Create the Telegram bot and get your user ID

  1. In Telegram, open @BotFather β†’ /newbot β†’ choose name (e.g. β€œDevOps Agent”) and username.
  2. Copy the bot token.
  3. Open @userinfobot β†’ send any message β†’ copy your user ID (optional, for TELEGRAM_USER_ID).

2. Expose localhost with ngrok

Copy the https://xxxx.ngrok-free.app URL into .env as TELEGRAM_WEBHOOK_URL.

3. Run the bot

The server listens on 0.0.0.0:8000. Telegram will send updates to your webhook URL.

Demo commands (send in Telegram)

How It Works

Example flow

  1. User sends β€œCheck disk usage” in Telegram.
  2. Telegram hits your webhook β†’ FastAPI β†’ TelegramInterface.
  3. Interface passes the message to AutonomousAgent.
  4. Agent (with workspace context) uses its tools to run the appropriate command (e.g. shell or read), then replies.
  5. Reply is sent back to Telegram.

Complete Implementation

bot.py

Only the entry point is in the repo root; all agent behavior and memory live inside workspace/ (AGENTS.md, SOUL.md, USER.md, MEMORY.md, memory/, logs/, app/, backups/).

Workspace and Agent Behavior

  • SOUL.md β€” Defines the agent as β€œDevOps Agent”, a senior systems engineer: fast, direct, technical; treats the workspace as the server.
  • USER.md β€” Describes the human as a developer/DevOps who wants quick answers from Telegram without SSH.
  • AGENTS.md β€” Full playbook: read SOUL, USER, and memory each session; use memory/YYYY-MM-DD.md and MEMORY.md for continuity; safety rules (no exfiltration, no destructive commands without asking, trash over rm); DevOps procedures for logs, system checks, backups, incidents; when to speak in group chats and how to react.
  • MEMORY.md β€” Long-term: server baseline, known issues (e.g. Redis instability, DB performance), patterns to watch, useful commands, app structure. Loaded in main session only (not in shared/group contexts).
  • memory/YYYY-MM-DD.md β€” Daily log: session start, health checks, issues found, actions taken. Agent creates/updates these.
The demo workspace/app/ and workspace/logs/ provide sample app code and logs so the agent can demonstrate backups, config reading, and log analysis without touching real production.

Key Features

Sandboxing

  • The agent is restricted to the workspace directory. File and shell operations outside it are blocked.
  • Use TELEGRAM_USER_ID so only your account can talk to the bot.

CHAT mode and memory

  • InterfaceMode.CHAT keeps conversation context so the agent can do multi-turn tasks.
  • /reset (configurable) clears conversation state.
  • Memory is file-based: daily logs in memory/ and long-term in MEMORY.md, so behavior is reproducible and auditable.

DevOps playbook (AGENTS.md)

  • Log analysis: Check size first, use tail/grep/wc, count by type, summarize and recommend.
  • System checks: Run df/free/ps, flag anomalies (e.g. disk >80%, low memory).
  • Backups: tar -czf to backups/, name like {what}-backup-{YYYY-MM-DD-HHMM}.tar.gz, log to daily memory.
  • Incident response: One-line summary, severity (πŸ”΄/🟑/🟒), evidence, suggested fix, log to daily memory.

Platform-aware output

  • Telegram: short messages, monospace for commands/output.
  • Group chats: respond when relevant (e.g. mentioned or troubleshooting); avoid dominating or over-reacting.

Security Notes

  • Agent is sandboxed to workspace/; no access to the rest of the host by default.
  • Set TELEGRAM_USER_ID to restrict the bot to your Telegram account.
  • AGENTS.md instructs the agent not to exfiltrate private data, not to run destructive commands without asking, and to avoid piping unknown input to bash or eval.

Repository

View the full example: DevOps Telegram Bot