Streaming Telegram Agent

OpenAI agent with token-by-token streaming via live message edits

Part of the Telegram interface examples. Follow the setup guide.

Code

streaming.py
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

agent_db = SqliteDb(
    session_table="telegram_sessions", db_file="tmp/telegram_streaming.db"
)

telegram_agent = Agent(
    name="Telegram Streaming Bot",
    model=OpenAIChat(id="gpt-4o-mini"),
    db=agent_db,
    instructions=[
        "You are a helpful assistant on Telegram.",
        "Keep responses concise and friendly.",
        "In groups, respond when mentioned with @ or when someone replies to your message.",
    ],
    add_history_to_context=True,
    num_history_runs=3,
    add_datetime_to_context=True,
    markdown=True,
)

agent_os = AgentOS(
    agents=[telegram_agent],
    interfaces=[
        Telegram(
            agent=telegram_agent,
            reply_to_mentions_only=True,
            streaming=True,
        )
    ],
)
app = agent_os.get_app()

if __name__ == "__main__":
    agent_os.serve(app="streaming: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=your-bot-token-from-botfather
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

Run Example

python streaming.py

The bot needs a public webhook URL to receive messages. See Telegram setup.

Key Features

  • Token-by-Token Streaming: Responses appear incrementally as they are generated
  • Live Message Edits: The bot edits its message in real time, throttled to stay within Telegram rate limits
  • Conversation History: Maintains context with last 3 interactions
  • Persistent Memory: SQLite database for session storage