openclaw - 💡(How to fix) Fix [Bug]: Telegram bot's own message triggers pairing request in DM [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#54337Fetched 2026-04-08 01:28:47
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Participants
Timeline (top)
closed ×1locked ×1

When a Telegram bot sends a message in a DM (direct message) conversation, the bot's own message is processed by the Gateway and triggers a pairing request because the bot's user ID is not in the allowFrom list. This creates an unnecessary pairing challenge from the bot to itself.

Root Cause

In extensions/telegram/src/bot-message-context.ts, the buildTelegramMessageContext function unconditionally calls enforceTelegramDmAccess for all messages, including messages sent by the bot itself:

if (
  !(await enforceTelegramDmAccess({
    isGroup,
    dmPolicy: effectiveDmPolicy,
    msg,
    chatId,
    effectiveDmAllow,
    accountId: account.accountId,
    bot,
    logger,
    upsertPairingRequest,
  }))
) {
  return null;
}

The enforceTelegramDmAccess function (in dm-access.ts) checks if the sender is in the allowFrom list. Since the bot's own ID is typically not in this list (only human users are), it triggers a pairing request:

if (dmPolicy === "pairing") {
  // ... creates pairing challenge
}

Code Example

if (
  !(await enforceTelegramDmAccess({
    isGroup,
    dmPolicy: effectiveDmPolicy,
    msg,
    chatId,
    effectiveDmAllow,
    accountId: account.accountId,
    bot,
    logger,
    upsertPairingRequest,
  }))
) {
  return null;
}

---

if (dmPolicy === "pairing") {
  // ... creates pairing challenge
}

---

{
  "id": "8492386395",
  "code": "PK2753K5",
  "createdAt": "2026-03-24T23:43:30.448Z",
  "lastSeenAt": "2026-03-24T23:45:18.496Z",
  "meta": {
    "username": "lancelot_clawdbot",
    "firstName": "Lancelot",
    "accountId": "default"
  }
}

---

// In bot-handlers.runtime.ts, around line 1726
bot.on("message", async (ctx) => {
  const msg = ctx.message;
  if (!msg) return;
  
  // Add bot message filtering
  if (msg.from?.id === ctx.me?.id) {
    return; // Ignore bot's own messages
  }
  
  // ... rest of handler
});

---

{
  "telegram": {
    "enabled": true,
    "dmPolicy": "pairing",
    "allowFrom": ["426518660"]
  }
}

---

// In extensions/telegram/src/bot-handlers.runtime.ts
bot.on("message", async (ctx) => {
  const msg = ctx.message;
  if (!msg) return;
  
  // Filter out bot's own messages
  if (msg.from?.is_bot && msg.from.id === ctx.me?.id) {
    return;
  }
  
  // ... existing logic
});
RAW_BUFFERClick to expand / collapse

name: submit-github-issue description: Submit a GitHub issue to the openclaw/openclaw repository

GitHub Issue Report: Telegram Bot Own Message Triggers Pairing

Summary

When a Telegram bot sends a message in a DM (direct message) conversation, the bot's own message is processed by the Gateway and triggers a pairing request because the bot's user ID is not in the allowFrom list. This creates an unnecessary pairing challenge from the bot to itself.

Bug Details

Root Cause

In extensions/telegram/src/bot-message-context.ts, the buildTelegramMessageContext function unconditionally calls enforceTelegramDmAccess for all messages, including messages sent by the bot itself:

if (
  !(await enforceTelegramDmAccess({
    isGroup,
    dmPolicy: effectiveDmPolicy,
    msg,
    chatId,
    effectiveDmAllow,
    accountId: account.accountId,
    bot,
    logger,
    upsertPairingRequest,
  }))
) {
  return null;
}

The enforceTelegramDmAccess function (in dm-access.ts) checks if the sender is in the allowFrom list. Since the bot's own ID is typically not in this list (only human users are), it triggers a pairing request:

if (dmPolicy === "pairing") {
  // ... creates pairing challenge
}

Evidence

  1. Pairing store record showing bot's own ID triggered pairing:
{
  "id": "8492386395",
  "code": "PK2753K5",
  "createdAt": "2026-03-24T23:43:30.448Z",
  "lastSeenAt": "2026-03-24T23:45:18.496Z",
  "meta": {
    "username": "lancelot_clawdbot",
    "firstName": "Lancelot",
    "accountId": "default"
  }
}
  1. Code analysis shows:
    • Reaction handler has bot filtering: if (user?.is_bot) return; (line 790 in bot-handlers.runtime.ts)
    • Regular message handler does NOT have bot filtering
    • buildTelegramMessageContext unconditionally calls enforceTelegramDmAccess

Similar Issues

Related to #32256 (forum topic system messages) and #46567 (pairing persistence), but this is a distinct issue specifically about bot's own messages triggering pairing.

Expected Behavior

Bot's own messages should be filtered out and not processed for pairing checks, similar to how reaction events are filtered:

// In bot-handlers.runtime.ts, around line 1726
bot.on("message", async (ctx) => {
  const msg = ctx.message;
  if (!msg) return;
  
  // Add bot message filtering
  if (msg.from?.id === ctx.me?.id) {
    return; // Ignore bot's own messages
  }
  
  // ... rest of handler
});

Actual Behavior

Bot's own messages are processed through the full pipeline, triggering unnecessary pairing requests when dmPolicy is set to "pairing".

Environment

  • OpenClaw version: 2026.3.23-2
  • OS: Linux 5.15.102-1-pve
  • Node: v22.22.0
  • Telegram Bot: @lancelot_clawdbot

Configuration

{
  "telegram": {
    "enabled": true,
    "dmPolicy": "pairing",
    "allowFrom": ["426518660"]
  }
}

Impact

  • Low severity: pairing request is created but doesn't cause infinite loop due to existingIdx check in pairing store
  • Creates unnecessary pairing entries
  • May confuse users who see pairing messages from the bot

Proposed Fix

Add bot message filtering in the message handler:

// In extensions/telegram/src/bot-handlers.runtime.ts
bot.on("message", async (ctx) => {
  const msg = ctx.message;
  if (!msg) return;
  
  // Filter out bot's own messages
  if (msg.from?.is_bot && msg.from.id === ctx.me?.id) {
    return;
  }
  
  // ... existing logic
});

Alternatively, add the check in buildTelegramMessageContext before calling enforceTelegramDmAccess.

Additional Context

  • The issue only occurs in DM conversations (not groups)
  • The issue only occurs when dmPolicy is set to "pairing"
  • The pairing store's existingIdx mechanism prevents infinite loops, but the first occurrence still creates a pairing request

extent analysis

Fix Plan

To resolve the issue, we need to add a check to filter out the bot's own messages before processing them for pairing checks. This can be achieved in two ways:

  • Method 1: Filter in the message handler
    1. Open extensions/telegram/src/bot-handlers.runtime.ts.
    2. Locate the bot.on("message", async (ctx) => { block.
    3. Add the following check at the beginning of the block:

if (msg.from?.is_bot && msg.from.id === ctx.me?.id) { return; // Ignore bot's own messages }

* **Method 2: Filter in `buildTelegramMessageContext`**
  1. Open `extensions/telegram/src/bot-message-context.ts`.
  2. Locate the `buildTelegramMessageContext` function.
  3. Add the following check before calling `enforceTelegramDmAccess`:
     ```typescript
if (msg.from?.is_bot && msg.from.id === bot.id) {
  return null; // Ignore bot's own messages
}

Verification

To verify that the fix worked:

  1. Send a message from the bot in a DM conversation.
  2. Check the pairing store for new entries.
  3. If no new pairing entry is created, the fix is successful.

Extra Tips

  • Make sure to test the fix in a development environment before deploying it to production.
  • Consider adding logging to track when the bot's own messages are filtered out for debugging purposes.
  • Review similar issues (e.g., #32256 and #46567) to ensure that the fix does not introduce any regressions.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix [Bug]: Telegram bot's own message triggers pairing request in DM [1 participants]