Telegram Agent with User Memory
MemoryManager for cross-session user recall on Telegram
Part of the Telegram interface examples. Follow the setup guide.
Code
from textwrap import dedent
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.memory.manager import MemoryManager
from agno.models.google import Gemini
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")
memory_manager = MemoryManager(
memory_capture_instructions="""\
Collect User's name,
Collect Information about user's passion and hobbies,
Collect Information about the users likes and dislikes,
Collect information about what the user is doing with their life right now
""",
model=Gemini(id="gemini-3.6-flash"),
)
personal_agent = Agent(
name="Basic Agent",
model=Gemini(id="gemini-3.6-flash"),
tools=[WebSearchTools()],
add_history_to_context=True,
num_history_runs=3,
add_datetime_to_context=True,
markdown=True,
db=agent_db,
memory_manager=memory_manager,
enable_agentic_memory=True,
instructions=dedent("""
You are a personal AI friend of the user, your purpose is to chat with the user about things and make them feel good.
First introduce yourself and ask for their name then, ask about themselves, their hobbies, what they like to do and what they like to talk about.
Use web search to find latest information about things in the conversations
"""),
)
agent_os = AgentOS(
agents=[personal_agent],
interfaces=[Telegram(agent=personal_agent)],
)
app = agent_os.get_app()
if __name__ == "__main__":
agent_os.serve(app="agent_with_user_memory: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-genai ddgsRun Example
python agent_with_user_memory.pyThe bot needs a public webhook URL to receive messages. See Telegram setup.
Key Features
- Agentic Memory: MemoryManager captures user preferences, hobbies, and personal details
- Cross-Session Recall: Remembers user information across conversations
- Web Search: Uses WebSearchTools to find up-to-date information during conversations
- Persistent Storage: SQLite database for both sessions and memory