fix: handle WhatsApp LID mentions (#692) (thanks @peschee)
This commit is contained in:
@@ -178,6 +178,12 @@ type MentionConfig = {
|
||||
allowFrom?: Array<string | number>;
|
||||
};
|
||||
|
||||
type MentionTargets = {
|
||||
normalizedMentions: string[];
|
||||
selfE164: string | null;
|
||||
selfJid: string | null;
|
||||
};
|
||||
|
||||
function buildMentionConfig(
|
||||
cfg: ReturnType<typeof loadConfig>,
|
||||
agentId?: string,
|
||||
@@ -186,26 +192,42 @@ function buildMentionConfig(
|
||||
return { mentionRegexes, allowFrom: cfg.whatsapp?.allowFrom };
|
||||
}
|
||||
|
||||
function isBotMentioned(
|
||||
function resolveMentionTargets(
|
||||
msg: WebInboundMsg,
|
||||
authDir?: string,
|
||||
): MentionTargets {
|
||||
const jidOptions = authDir ? { authDir } : undefined;
|
||||
const normalizedMentions = msg.mentionedJids?.length
|
||||
? msg.mentionedJids
|
||||
.map((jid) => jidToE164(jid, jidOptions) ?? jid)
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
const selfE164 =
|
||||
msg.selfE164 ?? (msg.selfJid ? jidToE164(msg.selfJid, jidOptions) : null);
|
||||
const selfJid = msg.selfJid ? msg.selfJid.replace(/:\\d+/, "") : null;
|
||||
return { normalizedMentions, selfE164, selfJid };
|
||||
}
|
||||
|
||||
function isBotMentionedFromTargets(
|
||||
msg: WebInboundMsg,
|
||||
mentionCfg: MentionConfig,
|
||||
authDir?: string,
|
||||
targets: MentionTargets,
|
||||
): boolean {
|
||||
const clean = (text: string) =>
|
||||
// Remove zero-width and directionality markers WhatsApp injects around display names
|
||||
normalizeMentionText(text);
|
||||
|
||||
const isSelfChat = isSelfChatMode(msg.selfE164, mentionCfg.allowFrom);
|
||||
const isSelfChat = isSelfChatMode(targets.selfE164, mentionCfg.allowFrom);
|
||||
|
||||
if (msg.mentionedJids?.length && !isSelfChat) {
|
||||
const normalizedMentions = msg.mentionedJids
|
||||
.map((jid) => jidToE164(jid, authDir ? { authDir } : undefined) ?? jid)
|
||||
.filter(Boolean);
|
||||
if (msg.selfE164 && normalizedMentions.includes(msg.selfE164)) return true;
|
||||
if (msg.selfJid && msg.selfE164) {
|
||||
if (
|
||||
targets.selfE164 &&
|
||||
targets.normalizedMentions.includes(targets.selfE164)
|
||||
)
|
||||
return true;
|
||||
if (targets.selfJid && targets.selfE164) {
|
||||
// Some mentions use the bare JID; match on E.164 to be safe.
|
||||
const bareSelf = msg.selfJid.replace(/:\\d+/, "");
|
||||
if (normalizedMentions.includes(bareSelf)) return true;
|
||||
if (targets.normalizedMentions.includes(targets.selfJid)) return true;
|
||||
}
|
||||
} else if (msg.mentionedJids?.length && isSelfChat) {
|
||||
// Self-chat mode: ignore WhatsApp @mention JIDs, otherwise @mentioning the owner in group chats triggers the bot.
|
||||
@@ -214,8 +236,8 @@ function isBotMentioned(
|
||||
if (mentionCfg.mentionRegexes.some((re) => re.test(bodyClean))) return true;
|
||||
|
||||
// Fallback: detect body containing our own number (with or without +, spacing)
|
||||
if (msg.selfE164) {
|
||||
const selfDigits = msg.selfE164.replace(/\D/g, "");
|
||||
if (targets.selfE164) {
|
||||
const selfDigits = targets.selfE164.replace(/\D/g, "");
|
||||
if (selfDigits) {
|
||||
const bodyDigits = bodyClean.replace(/[^\d]/g, "");
|
||||
if (bodyDigits.includes(selfDigits)) return true;
|
||||
@@ -233,14 +255,20 @@ function debugMention(
|
||||
mentionCfg: MentionConfig,
|
||||
authDir?: string,
|
||||
): { wasMentioned: boolean; details: Record<string, unknown> } {
|
||||
const result = isBotMentioned(msg, mentionCfg, authDir);
|
||||
const mentionTargets = resolveMentionTargets(msg, authDir);
|
||||
const result = isBotMentionedFromTargets(msg, mentionCfg, mentionTargets);
|
||||
const details = {
|
||||
from: msg.from,
|
||||
body: msg.body,
|
||||
bodyClean: normalizeMentionText(msg.body),
|
||||
mentionedJids: msg.mentionedJids ?? null,
|
||||
normalizedMentionedJids: mentionTargets.normalizedMentions.length
|
||||
? mentionTargets.normalizedMentions
|
||||
: null,
|
||||
selfJid: msg.selfJid ?? null,
|
||||
selfJidBare: mentionTargets.selfJid,
|
||||
selfE164: msg.selfE164 ?? null,
|
||||
resolvedSelfE164: mentionTargets.selfE164,
|
||||
};
|
||||
return { wasMentioned: result, details };
|
||||
}
|
||||
@@ -1586,7 +1614,11 @@ export async function monitorWebProvider(
|
||||
groupHistories.set(groupHistoryKey, history);
|
||||
}
|
||||
|
||||
const mentionDebug = debugMention(msg, mentionConfig, account.authDir);
|
||||
const mentionDebug = debugMention(
|
||||
msg,
|
||||
mentionConfig,
|
||||
account.authDir,
|
||||
);
|
||||
replyLogger.debug(
|
||||
{
|
||||
conversationId,
|
||||
|
||||
Reference in New Issue
Block a user