diff --git a/CHANGELOG.md b/CHANGELOG.md index 269655c18..a584998c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,7 @@ - Fix: use local auth for gateway security probe unless remote mode has a URL. (#1011) — thanks @ivanrvpereira. - Discord: truncate skill command descriptions for slash command limits. (#1018) — thanks @evalexpr. - macOS: resolve gateway token/password using config mode/remote URL, and warn when `launchctl setenv` overrides config. (#1022, #1021) — thanks @kkarimi. +- Telegram: allow reply-chain messages to bypass mention gating in groups. (#1038) — thanks @adityashaw2. ## 2026.1.14-1 diff --git a/src/telegram/bot-message-context.ts b/src/telegram/bot-message-context.ts index ab9f21d58..11c109b8d 100644 --- a/src/telegram/bot-message-context.ts +++ b/src/telegram/bot-message-context.ts @@ -219,6 +219,10 @@ export const buildTelegramMessageContext = async ({ groupConfig?.requireMention, baseRequireMention, ); + // Reply-chain detection: replying to a bot message acts like an implicit mention. + const botId = primaryCtx.me?.id; + const replyFromId = msg.reply_to_message?.from?.id; + const isReplyToBot = botId != null && replyFromId === botId; const shouldBypassMention = isGroup && requireMention && @@ -226,10 +230,11 @@ export const buildTelegramMessageContext = async ({ !hasAnyMention && commandAuthorized && hasControlCommand(msg.text ?? msg.caption ?? "", cfg, { botUsername }); - const effectiveWasMentioned = wasMentioned || shouldBypassMention; + const shouldBypassForReplyChain = isGroup && requireMention && isReplyToBot; + const effectiveWasMentioned = wasMentioned || shouldBypassMention || shouldBypassForReplyChain; const canDetectMention = Boolean(botUsername) || mentionRegexes.length > 0; if (isGroup && requireMention && canDetectMention) { - if (!wasMentioned && !shouldBypassMention) { + if (!wasMentioned && !shouldBypassMention && !shouldBypassForReplyChain) { logger.info({ chatId, reason: "no-mention" }, "skipping group message"); return null; } @@ -247,7 +252,7 @@ export const buildTelegramMessageContext = async ({ if (!isGroup) return false; if (!requireMention) return false; if (!canDetectMention) return false; - return wasMentioned || shouldBypassMention; + return wasMentioned || shouldBypassMention || shouldBypassForReplyChain; } return false; }; diff --git a/src/telegram/bot.test.ts b/src/telegram/bot.test.ts index bed540797..7966856f8 100644 --- a/src/telegram/bot.test.ts +++ b/src/telegram/bot.test.ts @@ -952,6 +952,39 @@ describe("createTelegramBot", () => { expect(replySpy).not.toHaveBeenCalled(); }); + it("accepts group replies to the bot without explicit mention when requireMention is enabled", async () => { + onSpy.mockReset(); + const replySpy = replyModule.__replySpy as unknown as ReturnType; + replySpy.mockReset(); + loadConfig.mockReturnValue({ + channels: { + telegram: { groups: { "*": { requireMention: true } } }, + }, + }); + + createTelegramBot({ token: "tok" }); + const handler = getOnHandler("message") as (ctx: Record) => Promise; + + await handler({ + message: { + chat: { id: 456, type: "group", title: "Ops Chat" }, + text: "following up", + date: 1736380800, + reply_to_message: { + message_id: 42, + text: "original reply", + from: { id: 999, first_name: "Clawdbot" }, + }, + }, + me: { id: 999, username: "clawdbot_bot" }, + getFile: async () => ({ download: async () => new Uint8Array() }), + }); + + expect(replySpy).toHaveBeenCalledTimes(1); + const payload = replySpy.mock.calls[0][0]; + expect(payload.WasMentioned).toBe(true); + }); + it("honors routed group activation from session store", async () => { onSpy.mockReset(); const replySpy = replyModule.__replySpy as unknown as ReturnType; diff --git a/src/telegram/bot/types.ts b/src/telegram/bot/types.ts index 00fcc0518..0d3aec455 100644 --- a/src/telegram/bot/types.ts +++ b/src/telegram/bot/types.ts @@ -6,7 +6,7 @@ export type TelegramStreamMode = "off" | "partial" | "block"; export type TelegramContext = { message: TelegramMessage; - me?: { username?: string }; + me?: { id?: number; username?: string }; getFile: () => Promise<{ file_path?: string; }>;