A Telegram bot that bridges messages to Agent Zero's HTTP API, enabling you to chat with your AI agent directly from Telegram.
This bot bridges Telegram to Agent Zero's HTTP API running inside the Docker container. Here are the key technical details:
API Endpoint: The bot sends messages to POST /api_message on port 80 (the Agent Zero web UI server, powered by Flask + Socket.IO via uvicorn). This is the dedicated external API endpoint — it does not use the web UI's WebSocket protocol or the tunnel API on port 55520.
Authentication: The /api_message endpoint requires an API key via the X-API-KEY header. The key (mcp_server_token) is deterministically generated at runtime from sha256(runtime_id:username:password), truncated to 16 characters. The bot auto-discovers this key by importing Agent Zero's settings module at startup — no need to hardcode or manually update it.
Conversation Continuity: Agent Zero uses context_id to track conversations. The bot maps each Telegram chat to a unique context_id, so conversations persist across messages within the same chat. Use /reset to start a fresh context.
Response Format: The API returns {"context_id": "...", "response": "..."} synchronously — the request blocks until the agent finishes thinking (up to 5 minutes timeout). The bot shows a typing indicator during this wait.
Runtime Environment: The bot must run with /opt/venv/bin/python3 (Agent Zero's virtual environment) so it can import the settings module for API key discovery. The script lives at /a0/usr/workdir/telegram_bridge.py.
Reverse Proxy Compatibility: The bot includes X-Forwarded-For and X-Real-IP headers on every API request. This is required when a reverse proxy (such as a Cloudflare tunnel) is active in the container — without them, SearXNG's bot-detection middleware intercepts the request and returns a 404. The headers are harmless in setups without a reverse proxy.
- Open Telegram and search for @BotFather
- Send
/newbotand follow the prompts to create your bot - Copy the bot token that BotFather gives you
You can utilize the respective section of this tutorial.
Copy the bot token and add TELEGRAM_BOT_TOKEN=your_token_here to /a0/usr/.env (no quotes around the token).
You can do this via the Files browser in the Agent Zero GUI.
Download telegram_bridge.py and upload it into /a0/usr/workdir.
Again, you can utilize the Files browser in Agent Zero.
docker exec agent-zero /opt/venv/bin/pip install aiohttp python-telegram-bot python-dotenvNote: Packages that are already pre-installed will simply report as 'already satisfied' and be safely skipped.
I'd recommend launching with -it first to confirm everything starts cleanly:
docker exec -it agent-zero /opt/venv/bin/python3 /a0/usr/workdir/telegram_bridge.pyYou should see:
============================================================
Agent Zero <-> Telegram Bridge
============================================================
API URL: http://127.0.0.1:80/api_message
API Key: _tyX****
Timeout: 300s
============================================================
Starting Telegram bot...
Open your Telegram bot and send /start, then send a message. The agent should reply!
Once you've confirmed the bot responds, kill the -it instance (closing the terminal doesn't always kill it):
docker exec agent-zero pkill -f telegram_bridge.pyIf you ever need an instant kill, -9 sends SIGKILL which can't be caught — the process dies immediately:
docker exec agent-zero pkill -9 -f telegram_bridge.pySkip this step if you plan to use auto-start / supervisord (step 8) instead.
Switch to the -d approach to run it in background (since -d detaches immediately, you won't see the startup banner or any errors):
docker exec -d agent-zero /opt/venv/bin/python3 /a0/usr/workdir/telegram_bridge.pyIf you have a running bot instance (from step 7 or otherwise), kill it first:
docker exec agent-zero pkill -9 -f telegram_bridge.pyTo make it auto-start with the container, add it to the container's supervisord config (which Agent Zero already uses to manage its processes). Pick one of the two options below — not both.
Copy and paste this single command into your terminal — it appends the config block automatically:
docker exec agent-zero bash -c 'printf "\n[program:telegram_bridge]\ncommand=/opt/venv/bin/python3 /a0/usr/workdir/telegram_bridge.py\nenvironment=\nuser=root\ndirectory=/a0\nstopwaitsecs=10\nstdout_logfile=/dev/stdout\nstdout_logfile_maxbytes=0\nstderr_logfile=/dev/stderr\nstderr_logfile_maxbytes=0\nautorestart=true\nstartretries=3\nstopasgroup=true\nkillasgroup=true\n" >> /etc/supervisor/conf.d/supervisord.conf'Open /etc/supervisor/conf.d/supervisord.conf via the Files browser in the Agent Zero GUI and add this block at the end of the file:
[program:telegram_bridge]
command=/opt/venv/bin/python3 /a0/usr/workdir/telegram_bridge.py
environment=
user=root
directory=/a0
stopwaitsecs=10
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=true
startretries=3
stopasgroup=true
killasgroup=true
⚠️ Important: Whichever option you chose, make sure the block only appears once in the file. A duplicate[program:telegram_bridge]block will cause supervisor to error. You can verify the file looks correct with:docker exec agent-zero cat /etc/supervisor/conf.d/supervisord.conf
docker exec agent-zero supervisorctl reread && docker exec agent-zero supervisorctl updatedocker exec agent-zero supervisorctl status telegram_bridgeFrom now on the bot will auto-start with the container.
# Stop the bot
docker exec agent-zero supervisorctl stop telegram_bridge
# Start the bot
docker exec agent-zero supervisorctl start telegram_bridge
# Restart the bot
docker exec agent-zero supervisorctl restart telegram_bridge
# Status of all services
docker exec agent-zero supervisorctl status
# Kill the bot (any running instance)
docker exec agent-zero pkill -f telegram_bridge.py
# Instant kill
docker exec agent-zero pkill -9 -f telegram_bridge.py
# Verify it's gone
docker exec agent-zero pgrep -f telegram_bridge.py
# Check if the process is alive
docker exec agent-zero ps aux | grep telegram_bridge
# View live logs (supervisord-managed)
docker logs agent-zero --tail 50You can limit the bot to specific Telegram users so that only you (or a set of people) can interact with it.
1. Get your user ID — message @userinfobot on Telegram. It will reply with your numeric user ID.
2. Add it to .env — open /a0/usr/.env and add:
TELEGRAM_USER_IDS=123456789
For multiple users, separate with commas: TELEGRAM_USER_IDS=123456789,987654321
3. Restart the bot:
docker exec agent-zero supervisorctl restart telegram_bridgeWhen the bot starts, the banner will show the allowed user IDs. If TELEGRAM_USER_IDS is not set or empty, all users are allowed (the default). Unauthorized users are silently ignored — they receive no response.
This filter works alongside the existing
TELEGRAM_CHAT_IDSfilter. Both can be used at the same time.
| Command | Description |
|---|---|
/start |
Welcome message and usage info |
/reset |
Start a new conversation (clears context) |
/status |
Show connection status |
/help |
Show available commands |
The idle bot is effectively costless:
- No LLM credits — the bot only calls Agent Zero's
/api_messagewhen a Telegram message arrives. No messages = no API calls = no tokens used. - No cron jobs — the bot script has no scheduled tasks or periodic pings. It uses long polling to wait for Telegram updates.
- Negligible resources — the idle process uses ~30–50MB of RAM and essentially 0% CPU.
You only "pay" (in LLM credits) when someone actually sends a message through Telegram.
To fully remove the bot from your Agent Zero container (no container restart needed):
# 1. Stop and remove the supervisord entry (if configured)
docker exec agent-zero supervisorctl stop telegram_bridge
# 2. Remove the supervisord config block
# Open the file and delete the [program:telegram_bridge] block:
docker exec agent-zero nano /etc/supervisor/conf.d/supervisord.conf
# Alternatively, you can do this via the Files browser in the Agent Zero GUI.
# Then reload:
docker exec agent-zero supervisorctl reread && docker exec agent-zero supervisorctl update
# 3. Kill any running instance
docker exec agent-zero pkill -9 -f telegram_bridge.py
# 4. Delete the bot script and log file
docker exec agent-zero rm -f /a0/usr/workdir/telegram_bridge.py /a0/usr/workdir/telegram_bridge.log
# 5. Remove the TELEGRAM_BOT_TOKEN line from /a0/usr/.env
docker exec agent-zero sed -i '/TELEGRAM_BOT_TOKEN/d' /a0/usr/.envOptionally, you can also delete the bot via @BotFather on Telegram (send /deletebot).
- Agent Zero Discord Bridge — Same concept for Discord
MIT