Skip to content

[Bug]: Telegram Bot Routing Broken - accountId Ignored in Bindings #9351

@lazmo88

Description

@lazmo88

Telegram multi-account/bot to different agents routing is broken in OpenClaw 2026.2.1. Messages to secondary Telegram bot accounts always route to the default agent (agent:main), ignoring bindings configuration with accountId matching.

Related issues: #9110, #9058, #8692, #8773
Related PR (Feishu fix): #8975

Steps to reproduce

  1. Configure two Telegram bot accounts in channels.telegram.accounts:
    "channels": {
    "telegram": {
    "enabled": true,
    "accounts": {
    "bot1": { "botToken": "TOKEN1" },
    "bot2": { "botToken": "TOKEN2" }
    }
    }
    }

  2. Set up bindings mapping each account to different agents:
    "bindings": [
    { "agentId": "main", "match": { "channel": "telegram", "accountId": "bot1" } },
    { "agentId": "secondary", "match": { "channel": "telegram", "accountId": "bot2" } }
    ]

  3. Send message to @bot2 on Telegram

  4. Observe: Message routes to agent:main instead of secondary
    Expected behavior

Messages to @bot2 should route to secondary agent based on binding:

{ "agentId": "secondary", "match": { "channel": "telegram", "accountId": "bot2" } }

Actual behavior

All messages route to default agent (agent:main), ignoring accountId matching in bindings.

Root cause identified: bot-message-context.js does not pass accountId to routing layer. File: /usr/lib/node_modules/openclaw/dist/telegram/bot-message-context.js

Environment

• OpenClaw version: 2026.2.1
• OS: Linux (HAOS/Docker)
• Channel: Telegram (multi-account)
• Install method: Docker/HAOS addon
Attempted workarounds (all failed)

  1. accountId variations

• accountId: "bot2" - Standard format
• accountId: "bot2_bot" - Username format (matching Telegram bot handle)
2. Group mentions with mentionPatterns

Configured agents with mention patterns:

{
"agents": {
"list": [
{
"id": "main",
"groupChat": { "mentionPatterns": ["@BOT1"] }
},
{
"id": "secondary",
"groupChat": { "mentionPatterns": ["@bot2"] }
}
]
}
}

Result: Created Telegram group, added both bots, mentioned @bot2 - still routed to agent:main

  1. Separate instances

Blocked: Requires multiple ports (HAOS addon limitation - single port mapping)

Conclusion: No viable workaround. Routing is fundamentally broken at code level.

Proposed fix

Solution: Map Bot Tokens to Agents via accountId

Core change: Pass accountId from the bot account that received the message through to the routing layer.

File: src/telegram/bot-message-context.ts

function buildBotMessageContext(update, account) {
return {
channel: "telegram",
peer: extractPeer(update),
text: update.message?.text",
accountId: account.id, // ADD: Pass account identifier
};
}

File: src/channels/channel-router.ts

function resolveAgentRoute(context) {
// Match binding by channel + accountId
const binding = bindings.find(b =>
b.match.channel === context.channel &&
b.match.accountId === context.accountId // ADD: Check account match
);
return binding?.agentId || defaultAgentId;
}

Result: Each bot account routes to its bound agent based on accountId matching in bindings configuration.

Alternative: Bot Token Mapping

If accountId matching is problematic, could map by bot token:

// Map bot token to agent via lookup table
const tokenToAgent = {
"TOKEN1": "main",
"TOKEN2": "secondary"
};

Reference: PR #8975 implements similar fix for Feishu channel (see pattern there).

Logs or screenshots

Gateway status shows multiple Telegram accounts configured but routing ignores accountId:

agents.list: 2 entries
bindings: 2 entries
telegram.accounts: ['bot1', 'bot2']

Session logs show all messages routing to agent:main:telegram:... regardless of which bot received them.

Impact: Blocks multi-bot Telegram workflows, multiple telegram bot/agent deployments and dev/prod bot separation.

Priority: High

Labels: bug, telegram

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions