From 93594a144039431f6bedc8a41e151de3ae6fedb1 Mon Sep 17 00:00:00 2001 From: khhjoe Date: Wed, 25 Mar 2026 01:13:16 +0800 Subject: [PATCH] fix(whatsapp): compare selfLid for reply-to-bot implicit mention in groups --- .../whatsapp/src/auto-reply/monitor/group-gating.ts | 9 +++++++-- extensions/whatsapp/src/inbound/monitor.ts | 4 ++++ extensions/whatsapp/src/inbound/types.ts | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/extensions/whatsapp/src/auto-reply/monitor/group-gating.ts b/extensions/whatsapp/src/auto-reply/monitor/group-gating.ts index d639e9e182a..e137768a277 100644 --- a/extensions/whatsapp/src/auto-reply/monitor/group-gating.ts +++ b/extensions/whatsapp/src/auto-reply/monitor/group-gating.ts @@ -127,14 +127,19 @@ export function applyGroupGating(params: ApplyGroupGatingParams) { conversationId: params.conversationId, }); const requireMention = activation !== "always"; - const selfJid = params.msg.selfJid?.replace(/:\\d+/, ""); - const replySenderJid = params.msg.replyToSenderJid?.replace(/:\\d+/, ""); + const selfJid = params.msg.selfJid?.replace(/:\d+/, ""); + const selfLid = params.msg.selfLid?.replace(/:\d+/, ""); + const replySenderJid = params.msg.replyToSenderJid?.replace(/:\d+/, ""); const selfE164 = params.msg.selfE164 ? normalizeE164(params.msg.selfE164) : null; const replySenderE164 = params.msg.replyToSenderE164 ? normalizeE164(params.msg.replyToSenderE164) : null; + // Detect reply-to-bot: compare JIDs, LIDs, and E.164 numbers. + // WhatsApp may report the quoted message sender as either a phone JID + // (xxxxx@s.whatsapp.net) or a LID (xxxxx@lid), so we compare both. const implicitMention = Boolean( (selfJid && replySenderJid && selfJid === replySenderJid) || + (selfLid && replySenderJid && selfLid === replySenderJid) || (selfE164 && replySenderE164 && selfE164 === replySenderE164), ); const mentionGate = resolveMentionGating({ diff --git a/extensions/whatsapp/src/inbound/monitor.ts b/extensions/whatsapp/src/inbound/monitor.ts index 2542c09d33e..8e88759bab4 100644 --- a/extensions/whatsapp/src/inbound/monitor.ts +++ b/extensions/whatsapp/src/inbound/monitor.ts @@ -77,6 +77,9 @@ export async function monitorWebInbox(options: { const selfJid = sock.user?.id; const selfE164 = selfJid ? jidToE164(selfJid) : null; + // Bot's own LID (Linked Identity) — needed for reply-to-bot detection + // when contextInfo.participant returns a LID instead of a phone JID. + const selfLid = (sock.user as { lid?: string } | undefined)?.lid ?? undefined; const debouncer = createInboundDebouncer({ debounceMs: options.debounceMs ?? 0, buildKey: (msg) => { @@ -416,6 +419,7 @@ export async function monitorWebInbox(options: { groupParticipants: inbound.groupParticipants, mentionedJids: mentionedJids ?? undefined, selfJid, + selfLid, selfE164, fromMe: Boolean(msg.key?.fromMe), location: enriched.location ?? undefined, diff --git a/extensions/whatsapp/src/inbound/types.ts b/extensions/whatsapp/src/inbound/types.ts index 731dcd2c8cc..d3af7a0dec6 100644 --- a/extensions/whatsapp/src/inbound/types.ts +++ b/extensions/whatsapp/src/inbound/types.ts @@ -30,6 +30,7 @@ export type WebInboundMessage = { groupParticipants?: string[]; mentionedJids?: string[]; selfJid?: string | null; + selfLid?: string | null; selfE164?: string | null; fromMe?: boolean; location?: NormalizedLocation;