Basic Telegram Agent
Gemini agent with session persistence on Telegram
Part of the Telegram interface examples. Follow the setup guide.
Code
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.google import Gemini
from agno.os.app import AgentOS
from agno.os.interfaces.telegram import Telegram
agent_db = SqliteDb(session_table="telegram_sessions", db_file="tmp/telegram_basic.db")
telegram_agent = Agent(
name="Telegram Bot",
model=Gemini(id="gemini-2.5-pro"),
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,
)
],
)
app = agent_os.get_app()
if __name__ == "__main__":
agent_os.serve(app="basic:app", reload=True)Usage
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateSet 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 GOOGLE_API_KEY=your-google-api-key
export TELEGRAM_WEBHOOK_SECRET_TOKEN=replace-with-a-long-random-secret
unset APP_ENVInstall dependencies
uv pip install -U "agno[os,telegram]" google-genaiRun Example
python basic.pyThe bot needs a public webhook URL to receive messages. See Telegram setup.
Key Features
- Telegram Integration: Responds to direct messages, group @mentions, and replies to the bot
- Conversation History: Maintains context with last 3 interactions
- Persistent Memory: SQLite database for session storage
- Group Chat Support: Responds to group mentions and replies to the bot; commands are handled separately
- DateTime Context: Time-aware responses