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:
@@ -86,6 +86,7 @@
|
|||||||
- Fix: use local auth for gateway security probe unless remote mode has a URL. (#1011) — thanks @ivanrvpereira.
|
- 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.
|
- 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.
|
- 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
|
## 2026.1.14-1
|
||||||
|
|
||||||
|
|||||||
@@ -219,6 +219,10 @@ export const buildTelegramMessageContext = async ({
|
|||||||
groupConfig?.requireMention,
|
groupConfig?.requireMention,
|
||||||
baseRequireMention,
|
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 =
|
const shouldBypassMention =
|
||||||
isGroup &&
|
isGroup &&
|
||||||
requireMention &&
|
requireMention &&
|
||||||
@@ -226,10 +230,11 @@ export const buildTelegramMessageContext = async ({
|
|||||||
!hasAnyMention &&
|
!hasAnyMention &&
|
||||||
commandAuthorized &&
|
commandAuthorized &&
|
||||||
hasControlCommand(msg.text ?? msg.caption ?? "", cfg, { botUsername });
|
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;
|
const canDetectMention = Boolean(botUsername) || mentionRegexes.length > 0;
|
||||||
if (isGroup && requireMention && canDetectMention) {
|
if (isGroup && requireMention && canDetectMention) {
|
||||||
if (!wasMentioned && !shouldBypassMention) {
|
if (!wasMentioned && !shouldBypassMention && !shouldBypassForReplyChain) {
|
||||||
logger.info({ chatId, reason: "no-mention" }, "skipping group message");
|
logger.info({ chatId, reason: "no-mention" }, "skipping group message");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -247,7 +252,7 @@ export const buildTelegramMessageContext = async ({
|
|||||||
if (!isGroup) return false;
|
if (!isGroup) return false;
|
||||||
if (!requireMention) return false;
|
if (!requireMention) return false;
|
||||||
if (!canDetectMention) return false;
|
if (!canDetectMention) return false;
|
||||||
return wasMentioned || shouldBypassMention;
|
return wasMentioned || shouldBypassMention || shouldBypassForReplyChain;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -952,6 +952,39 @@ describe("createTelegramBot", () => {
|
|||||||
expect(replySpy).not.toHaveBeenCalled();
|
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 () => {
|
it("honors routed group activation from session store", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export type TelegramStreamMode = "off" | "partial" | "block";
|
|||||||
|
|
||||||
export type TelegramContext = {
|
export type TelegramContext = {
|
||||||
message: TelegramMessage;
|
message: TelegramMessage;
|
||||||
me?: { username?: string };
|
me?: { id?: number; username?: string };
|
||||||
getFile: () => Promise<{
|
getFile: () => Promise<{
|
||||||
file_path?: string;
|
file_path?: string;
|
||||||
}>;
|
}>;
|
||||||
|
|||||||
Reference in New Issue
Block a user