Skip to content

fix(telegram): group chat user identity, observation mode, and proactive messaging #349

Closed

Description

@vybe

Problem

Telegram group chat behavior has three related issues:

1. User Identity Bug (Critical)

Agent cannot see who sent a message in group chats. All users appear as generic "user" because sender identity is not passed to the agent.

Root cause in message_router.py:310-311:

if is_group:
    context_prompt = message.text  # ← Just the raw text!

The adapter correctly extracts username and first_name into metadata, but the router never formats it into the prompt.

Current agent view:

Hello, what do you think about this?

Expected agent view:

[Group: AI Builders Chat]
[From: @johndoe (John)]

Hello, what do you think about this?

2. Forced Response Behavior

When trigger_mode="all", every message triggers a full execution cycle ending with a mandatory response. The agent has no way to "observe without responding."

Current trigger modes (telegram_group_configs.trigger_mode):

  • mention (default) — only @mentions or replies to bot
  • all — every message triggers execution + response

Agents need the ability to see group conversation context and decide whether to engage.

3. No Proactive Group Messaging

Agent can wake up on schedule but cannot send messages to Telegram groups. Issue #321 covers proactive DMs to users; this is about group broadcasts.

Current state:

  • Group chat_ids are stored in telegram_group_configs (created when bot joins)
  • No MCP tool exists for outbound group messages
  • Agent cannot discover which groups it's in

Proposed Solution

Phase 1: Fix User Identity

Add sender formatting in message_router.py for group messages:

def _format_group_sender(message: NormalizedMessage) -> str:
    """Format sender identity for group chat context."""
    parts = []
    if chat_title := message.metadata.get("chat_title"):
        parts.append(f"[Group: {chat_title}]")
    
    raw_msg = message.metadata.get("raw_message", {})
    from_user = raw_msg.get("from", {})
    username = message.metadata.get("username")
    first_name = from_user.get("first_name")
    
    if username and first_name:
        parts.append(f"[From: @{username} ({first_name})]")
    elif username:
        parts.append(f"[From: @{username}]")
    elif first_name:
        parts.append(f"[From: {first_name}]")
    else:
        parts.append(f"[From: User #{message.sender_id}]")
    
    return "\n".join(parts)

Update router:

if is_group:
    sender_context = _format_group_sender(message)
    context_prompt = f"{sender_context}\n\n{message.text}"

Phase 2: Observation Mode with History

Add new trigger mode observe that:

  1. Passes all messages to agent (like all)
  2. Includes message history — build context from recent group messages so agent has conversational awareness
  3. Adds system prompt guidance: "Group observation mode. Respond with [NO_REPLY] to stay silent."
  4. Router checks for [NO_REPLY] marker and skips send_response()

This keeps the decision with the agent — it can follow CLAUDE.md instructions to decide when to engage based on:

  • Topic relevance
  • Direct questions to the agent
  • Gaps in conversation it can fill
  • Periodic check-ins

Database changes:

  • Add observe to valid trigger_mode enum
  • Consider group_chat_messages table for history persistence (or reuse public_chat_sessions pattern)

Phase 3: Proactive Group Messaging

Add MCP tools for group discovery and messaging:

// Discover groups agent is in
list_channel_groups(channel_type?: "telegram" | "slack") 
  → Array<{channel_type, chat_id, chat_title, chat_type, member_count?}>

// Send to a group
send_group_message(
  channel_type: "telegram" | "slack",
  chat_id: string,
  message: string
) → {sent: boolean, message_id?: string}

Security:

  • Verify agent owns the binding for that chat_id
  • Rate limit proactive group messages (e.g., 5/hour/group)
  • Respect group config settings (e.g., proactive_enabled flag)

Backend endpoints:

  • GET /api/agents/{name}/channel-groups — list groups across channels
  • POST /api/agents/{name}/channel-groups/{chat_id}/messages — send to group

Files to Modify

File Changes
src/backend/adapters/message_router.py Add _format_group_sender(), update group context building, check for [NO_REPLY]
src/backend/adapters/telegram_adapter.py Expose first_name in metadata
src/backend/db/telegram_channels.py Add observe trigger mode support
src/backend/services/platform_prompt_service.py Add observation mode guidance to execution context
src/mcp-server/src/tools/channels.ts New tool module for list_channel_groups, send_group_message
src/backend/routers/telegram.py Add group messaging endpoint

Acceptance Criteria

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority-p1type-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions