Multiple Instances

Multiple Telegram bots on a single AgentOS server

Code

multiple_instances.py
import os

from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIChat
from agno.os.app import AgentOS
from agno.os.interfaces.telegram import Telegram
from agno.tools.websearch import WebSearchTools

agent_db = SqliteDb(db_file="tmp/persistent_memory.db")

basic_agent = Agent(
    name="Basic Agent",
    model=OpenAIChat(id="gpt-5.2"),
    db=agent_db,
    add_history_to_context=True,
    num_history_runs=3,
    add_datetime_to_context=True,
    markdown=True,
)

web_research_agent = Agent(
    name="Web Research Agent",
    model=OpenAIChat(id="gpt-5.2"),
    db=agent_db,
    tools=[WebSearchTools()],
    add_history_to_context=True,
    num_history_runs=3,
    add_datetime_to_context=True,
)

# Telegram only supports one webhook per bot token, so each interface needs its own bot.
# Create two bots via @BotFather and pass each token explicitly.
agent_os = AgentOS(
    agents=[basic_agent, web_research_agent],
    interfaces=[
        Telegram(agent=basic_agent, prefix="/basic", token=os.getenv("TELEGRAM_TOKEN_BASIC")),
        Telegram(agent=web_research_agent, prefix="/web-research", token=os.getenv("TELEGRAM_TOKEN_RESEARCH")),
    ],
)
app = agent_os.get_app()

if __name__ == "__main__":
    agent_os.serve(app="multiple_instances:app", reload=True)

Usage

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Set Environment Variables

Use this secret as secret_token when registering the public webhook in the setup guide. Keep the same value in the server and registration terminals. APP_ENV=development bypasses webhook authentication, so clear it for this public callback flow.

export TELEGRAM_TOKEN_BASIC=bot-token-for-basic-agent
export TELEGRAM_TOKEN_RESEARCH=bot-token-for-research-agent
export OPENAI_API_KEY=your-openai-api-key
export TELEGRAM_WEBHOOK_SECRET_TOKEN=replace-with-a-long-random-secret
unset APP_ENV

Install dependencies

uv pip install -U "agno[os,telegram]" openai ddgs

Run Example

python multiple_instances.py

Start a Public Tunnel

In a separate terminal, start ngrok for the AgentOS port:

ngrok http 7777

Leave the server and tunnel running. In a third terminal, export the HTTPS forwarding URL and the same credentials used by the server:

export NGROK_URL=https://your-subdomain.ngrok-free.app
export TELEGRAM_TOKEN_BASIC=bot-token-for-basic-agent
export TELEGRAM_TOKEN_RESEARCH=bot-token-for-research-agent
export TELEGRAM_WEBHOOK_SECRET_TOKEN=replace-with-the-same-server-secret

Register Webhooks

Register a webhook for each bot token:

curl -X POST "https://api.telegram.org/bot${TELEGRAM_TOKEN_BASIC}/setWebhook" \
  --data-urlencode "url=${NGROK_URL}/basic/webhook" \
  --data-urlencode "secret_token=${TELEGRAM_WEBHOOK_SECRET_TOKEN}"
curl -X POST "https://api.telegram.org/bot${TELEGRAM_TOKEN_RESEARCH}/setWebhook" \
  --data-urlencode "url=${NGROK_URL}/web-research/webhook" \
  --data-urlencode "secret_token=${TELEGRAM_WEBHOOK_SECRET_TOKEN}"

See Telegram setup for BotFather and production webhook-secret configuration.

Key Features

  • Prefix-Based Routing: Each agent gets its own webhook path (/basic/webhook, /web-research/webhook)
  • Shared Server: Both agents run on a single AgentOS instance
  • Separate Bot Tokens: Each interface uses its own BotFather bot (Telegram allows only one webhook per token)
  • Shared Database: Both agents share the same SQLite database