Multi-Agent Telegram Team

Researcher + Writer team coordinating on Telegram

Code

1from kern.agent import Agent
2from kern.db.sqlite import SqliteDb
3from kern.models.openai import OpenAIChat
4from kern.os.app import AgentOS
5from kern.os.interfaces.telegram import Telegram
6from kern.team import Team
7
8agent_db = SqliteDb(
9 session_table="telegram_team_sessions", db_file="tmp/telegram_team.db"
10)
11
12researcher = Agent(
13 name="Researcher",
14 model=OpenAIChat(id="gpt-4o-mini"),
15 role="Researches topics and provides detailed factual information.",
16 instructions=["Provide well-researched, factual information on the given topic."],
17)
18
19writer = Agent(
20 name="Writer",
21 model=OpenAIChat(id="gpt-4o-mini"),
22 role="Takes research and writes clear, engaging summaries.",
23 instructions=["Write concise, engaging summaries based on the research provided."],
24)
25
26telegram_team = Team(
27 name="Telegram Research Team",
28 model=OpenAIChat(id="gpt-4o-mini"),
29 members=[researcher, writer],
30 db=agent_db,
31 instructions=[
32 "You coordinate a research team on Telegram.",
33 "Use the Researcher to gather facts, then the Writer to create a response.",
34 "Keep responses concise for Telegram.",
35 ],
36 add_history_to_context=True,
37 num_history_runs=3,
38 add_datetime_to_context=True,
39 markdown=True,
40)
41
42agent_os = AgentOS(
43 teams=[telegram_team],
44 interfaces=[
45 Telegram(
46 team=telegram_team,
47 reply_to_mentions_only=True,
48 )
49 ],
50)
51app = agent_os.get_app()
52
53if __name__ == "__main__":
54 agent_os.serve(app="team:app", reload=True)

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set Environment Variables

1export TELEGRAM_TOKEN=your-bot-token-from-botfather
2export OPENAI_API_KEY=your-openai-api-key
3export APP_ENV=development

Install dependencies

1uv pip install -U "kern-ai[telegram]"

Run Example

1python team.py

Key Features

  • Multi-Agent Coordination: Team leader delegates to Researcher and Writer agents
  • Specialized Roles: Each agent has a focused responsibility
  • Team on Telegram: The Team is passed directly to the Telegram interface
  • Persistent Memory: SQLite database for session storage
  • Group Chat Support: Only responds when mentioned in groups