Telegram Bot Agent

t-251·WorkTask·
·
·
Created2 months ago·Updated2 months ago

Dependencies

Description

Edit

Create Telegram Bot agent - the first concrete agent that validates the infrastructure.

Context

This is the first agent built on top of the shared infrastructure (Provider, Memory, Registry). It serves as a family assistant accessible via Telegram, demonstrating cross-agent memory sharing.

Prerequisites

  • t-247 (Provider Abstraction) - for LLM calls
  • t-248 (Memory System) - for storing/recalling user knowledge
  • t-249 (Tool Registry) - for composing tool sets

Telegram Bot API

  • Create bot via @BotFather, get token
  • Use getUpdates polling (simpler than webhooks for start)
  • API endpoint: https://api.telegram.org/bot<TOKEN>/getUpdates
  • Send messages: https://api.telegram.org/bot<TOKEN>/sendMessage

Requirements

1. Create Telegram API client:

-- Omni/Agent/Telegram.hs

data TelegramConfig = TelegramConfig
  { tgBotToken :: Text
  , tgPollingTimeout :: Int  -- seconds, default 30
  }

data TelegramMessage = TelegramMessage
  { tmUpdateId :: Int
  , tmChatId :: Int64
  , tmUserId :: Int64
  , tmUserName :: Maybe Text
  , tmText :: Text
  }

-- Poll for new messages
getUpdates :: TelegramConfig -> Int -> IO [TelegramMessage]

-- Send reply
sendMessage :: TelegramConfig -> Int64 -> Text -> IO ()

2. User identification flow:

  • Extract Telegram user ID from message
  • Call Memory.getOrCreateUserByTelegramId to get/create User
  • Use user's display name from Telegram if creating new user

3. Agent loop:

runTelegramBot :: TelegramConfig -> EngineConfig -> IO ()
runTelegramBot tgConfig engineConfig = do
  -- Poll loop
  forever do
    messages <- getUpdates tgConfig lastUpdateId
    for_ messages handleMessage
    
handleMessage :: TelegramMessage -> IO ()
handleMessage msg = do
  -- Get or create user
  user <- Memory.getOrCreateUserByTelegramId (tmUserId msg) (fromMaybe "Unknown" (tmUserName msg))
  
  -- Recall relevant memories
  memories <- Memory.recallMemories (userId user) (tmText msg) 10
  
  -- Build agent config with memory context
  let systemPrompt = telegramSystemPrompt <> formatMemories memories
      agentConfig = AgentConfig
        { agentTools = Registry.telegramTools  -- [remember, recall, web_search]
        , agentSystemPrompt = systemPrompt
        , agentMaxIterations = 5
        , ...
        }
  
  -- Run agent
  result <- Engine.runAgent engineConfig agentConfig (tmText msg)
  
  -- Send response
  sendMessage tgConfig (tmChatId msg) (resultFinalMessage result)

4. System prompt for Telegram bot:

You are a helpful family assistant on Telegram. You help with questions, 
remember important information about family members, and provide friendly assistance.

When you learn something important about the user (preferences, facts about them,
their interests), use the 'remember' tool to store it for future reference.

Be concise in responses - Telegram is a chat interface, not a document.
Be friendly and helpful. This is a family bot, keep content appropriate.

5. Tools for Telegram agent:

  • remember (from Memory module) - store facts about user
  • recall (from Memory module) - explicitly query memories
  • web_search (from Registry) - answer questions requiring web lookup

6. CLI command:

jr telegram [--token=TOKEN]    Start Telegram bot (uses TELEGRAM_BOT_TOKEN env if not provided)

7. Environment variables:

  • TELEGRAM_BOT_TOKEN - bot token from @BotFather
  • Can also pass via --token flag

Files to Create

  • Omni/Agent/Telegram.hs - Telegram API client and bot loop

Files to Modify

  • Omni/Jr.hs - add 'jr telegram' command

Deployment

  • For now, run manually: jr telegram
  • Later: systemd service or similar for persistent running

Testing

  • bild --test Omni/Agent/Telegram.hs
  • Create test bot via @BotFather
  • Send test messages, verify responses
  • Verify memories are stored and recalled across messages

Example Interaction

User: Hi, I'm Ben and I work as an AI engineer Bot: Nice to meet you, Ben! I've noted that you're an AI engineer. How can I help you today? [remember tool called: 'Ben works as an AI engineer']

User: What papers should I read? Bot: Since you're an AI engineer, you might be interested in... [recall tool found: 'Ben works as an AI engineer']

Reference

  • Telegram Bot API: https://core.telegram.org/bots/api
  • See Omni/Agent/PLAN.md section 3.4 and Phase 6
  • See Omni/Agent/Engine.hs for runAgent usage
  • See Omni/Agent/Memory.hs for memory operations

Timeline (2)

🔄[human]Open → InProgress2 months ago
🔄[human]InProgress → Done2 months ago