Telegram: Add reply-chain detection to bypass mention requirement (#1038)

* Telegram: add reply-chain detection to bypass mention requirement

* fix: allow telegram reply-chain mention bypass (#1038) (thanks @adityashaw2)

---------

Co-authored-by: Aditya Shaw <aditya@adityashaw.dev>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
adityashaw2
2026-01-17 02:54:45 +05:30
committed by GitHub
parent 9072e35f08
commit e9d6869290
4 changed files with 43 additions and 4 deletions

View File

@@ -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

View File

@@ -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;
};

View File

@@ -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<typeof vi.fn>;
replySpy.mockReset();
loadConfig.mockReturnValue({
channels: {
telegram: { groups: { "*": { requireMention: true } } },
},
});
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
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<typeof vi.fn>;

View File

@@ -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;
}>;