Repository navigation
Description
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 botall— 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 intelegram_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:
- Passes all messages to agent (like
all) - Includes message history — build context from recent group messages so agent has conversational awareness
- Adds system prompt guidance: "Group observation mode. Respond with
[NO_REPLY]to stay silent." - Router checks for
[NO_REPLY]marker and skipssend_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
observeto validtrigger_modeenum - Consider
group_chat_messagestable for history persistence (or reusepublic_chat_sessionspattern)
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_enabledflag)
Backend endpoints:
GET /api/agents/{name}/channel-groups— list groups across channelsPOST /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
- Agent sees
[Group: X] [From: @user (Name)]prefix in group messages -
observetrigger mode allows agent to see all messages without forced reply - Agent can return
[NO_REPLY]to skip sending a response - Observation mode includes recent message history for context
- MCP tool
list_channel_groupsreturns agent's Telegram groups - MCP tool
send_group_messagedelivers to Telegram groups - Rate limiting prevents group spam
- Works with existing auth/access control (feat: Unified channel access control — verified-email identity, per-agent allow-list, access requests (web / Telegram / Slack) #311)
Related
- feat: proactive agent messaging to channel users (Telegram / Slack / web) #321 — proactive DM messaging (this issue is about groups)
- feat: Unified channel access control — verified-email identity, per-agent allow-list, access requests (web / Telegram / Slack) #311 — unified channel access control
Activity
feat(telegram): group chat enhancements - sender identity, observe mo…
docs: update requirements and architecture for #349 Telegram group en…
feat(telegram): group chat enhancements - sender identity, observe mo…
feat(slack): channel identity, proactive messaging, and per-agent bot…
feat(channels): make proactive message rate limits admin-configurable (…
feat(channels): make proactive message rate limits admin-configurable (…
fix(channels): persist proactive messages to channel session history (#…
fix(channels): persist proactive group messages to channel session hi…