Skip to content

Commit fb85fa3

Browse files
author
Peter Steinberger
committed
fix(whatsapp): normalize provider self-echo identities
1 parent 618f329 commit fb85fa3

3 files changed

Lines changed: 166 additions & 2 deletions

File tree

‎extensions/whatsapp/src/inbound/dedupe.ts‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
// Whatsapp plugin module implements dedupe behavior.
2+
import {
3+
isHostedLidUser,
4+
isHostedPnUser,
5+
isJidGroup,
6+
jidDecode,
7+
jidEncode,
8+
jidNormalizedUser,
9+
} from "baileys";
210
import { createDedupeCache } from "openclaw/plugin-sdk/dedupe-runtime";
311

412
const RECENT_OUTBOUND_MESSAGE_TTL_MS = 20 * 60_000;
@@ -14,7 +22,19 @@ function buildMessageKey(params: {
1422
messageId: string;
1523
}): string | null {
1624
const accountId = params.accountId.trim();
17-
const remoteJid = params.remoteJid.trim();
25+
const rawRemoteJid = params.remoteJid.trim();
26+
const hostedPhone = isHostedPnUser(rawRemoteJid);
27+
// Match Baileys cleanMessage: hosted domains collapse before device stripping.
28+
let remoteJid: string;
29+
if (hostedPhone || isHostedLidUser(rawRemoteJid)) {
30+
const decodedJid = jidDecode(rawRemoteJid);
31+
if (!decodedJid) {
32+
return null;
33+
}
34+
remoteJid = jidEncode(decodedJid.user, hostedPhone ? "s.whatsapp.net" : "lid");
35+
} else {
36+
remoteJid = jidNormalizedUser(rawRemoteJid);
37+
}
1838
const messageId = params.messageId.trim();
1939
if (!accountId || !remoteJid || !messageId || messageId === "unknown") {
2040
return null;
@@ -41,11 +61,23 @@ export function rememberRecentOutboundMessage(params: {
4161
export function isRecentOutboundMessage(params: {
4262
accountId: string;
4363
remoteJid: string;
64+
alternateRemoteJid?: string;
4465
messageId: string;
4566
}): boolean {
4667
const key = buildMessageKey(params);
4768
if (!key) {
4869
return false;
4970
}
50-
return recentOutboundMessages.peek(key);
71+
if (recentOutboundMessages.peek(key)) {
72+
return true;
73+
}
74+
// Baileys exposes phone/LID aliases for direct chats only; never cross groups.
75+
if (!params.alternateRemoteJid || isJidGroup(params.remoteJid)) {
76+
return false;
77+
}
78+
const alternateKey = buildMessageKey({
79+
...params,
80+
remoteJid: params.alternateRemoteJid,
81+
});
82+
return alternateKey !== null && recentOutboundMessages.peek(alternateKey);
5183
}

‎extensions/whatsapp/src/inbound/message-normalization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export function createWhatsAppInboundMessageNormalizer(options: {
4545
!isRecentOutboundMessage({
4646
accountId: options.accountId,
4747
remoteJid,
48+
alternateRemoteJid: msg.key?.remoteJidAlt,
4849
messageId: id,
4950
})
5051
) {

‎extensions/whatsapp/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Whatsapp plugin module implements monitor inbox.allows messages from senders allowfrom list support behavior.
22
import "./monitor-inbox.test-harness.js";
3+
import { cleanMessage } from "baileys";
34
import { describe, expect, it, vi } from "vitest";
5+
import { isRecentOutboundMessage } from "./inbound/dedupe.js";
46
import {
57
buildNotifyMessageUpsert,
68
expectPairingPromptSent,
@@ -415,6 +417,135 @@ describe("web monitor inbox", () => {
415417
await listener.close();
416418
});
417419

420+
it.each([
421+
{
422+
name: "device-qualified phone JIDs",
423+
outboundJid: "123:[email protected]",
424+
echoedJid: "[email protected]",
425+
},
426+
{
427+
name: "legacy c.us phone JIDs",
428+
outboundJid: "[email protected]",
429+
echoedJid: "[email protected]",
430+
},
431+
{
432+
name: "hosted phone JIDs",
433+
outboundJid: "123:7@hosted",
434+
echoedJid: "123:7@hosted",
435+
},
436+
{
437+
name: "hosted LID JIDs",
438+
outboundJid: "777:[email protected]",
439+
echoedJid: "777:[email protected]",
440+
},
441+
{
442+
name: "phone aliases carried alongside direct LID echoes",
443+
outboundJid: "[email protected]",
444+
echoedJid: "777@lid",
445+
echoedAlternateJid: "123:[email protected]",
446+
},
447+
{
448+
name: "LID aliases carried alongside direct phone echoes",
449+
outboundJid: "777@lid",
450+
echoedJid: "[email protected]",
451+
echoedAlternateJid: "777:3@lid",
452+
},
453+
])(
454+
"filters provider-normalized outbound self echoes for $name",
455+
async ({ outboundJid, echoedJid, echoedAlternateJid }) => {
456+
mockLoadConfig.mockReturnValue({
457+
channels: {
458+
whatsapp: {
459+
selfChatMode: true,
460+
allowFrom: ["+123"],
461+
},
462+
},
463+
messages: DEFAULT_MESSAGES_CFG,
464+
});
465+
466+
const onMessage = vi.fn();
467+
const { listener, sock } = await startInboxMonitor(onMessage);
468+
sock.signalRepository.lidMapping.getPNForLID.mockImplementation(async (jid: string) =>
469+
jid.startsWith("777@") ? "[email protected]" : null,
470+
);
471+
const messageId = `bot-provider-echo-${outboundJid}`;
472+
sock.sendMessage.mockResolvedValueOnce({ key: { id: messageId } });
473+
await listener.sendMessage(outboundJid, "gateway self echo");
474+
475+
const echoedMessage = {
476+
key: {
477+
id: messageId,
478+
fromMe: true,
479+
remoteJid: echoedJid,
480+
...(echoedAlternateJid ? { remoteJidAlt: echoedAlternateJid } : {}),
481+
},
482+
message: { conversation: "gateway self echo" },
483+
messageTimestamp: nowSeconds(),
484+
};
485+
cleanMessage(echoedMessage, "[email protected]", "777@lid");
486+
487+
sock.ev.emit("messages.upsert", {
488+
type: "notify",
489+
messages: [echoedMessage],
490+
});
491+
await settleInboundWork();
492+
493+
expect(onMessage).not.toHaveBeenCalled();
494+
expectOnlyOutboundChannelActivity();
495+
await listener.close();
496+
},
497+
);
498+
499+
it("keeps provider-normalized echo matches scoped to their account and message", async () => {
500+
mockLoadConfig.mockReturnValue({
501+
channels: {
502+
whatsapp: {
503+
selfChatMode: true,
504+
allowFrom: ["+123"],
505+
},
506+
},
507+
messages: DEFAULT_MESSAGES_CFG,
508+
});
509+
510+
const { listener, sock } = await startInboxMonitor(vi.fn());
511+
sock.sendMessage.mockResolvedValueOnce({ key: { id: "scoped-hosted-echo" } });
512+
await listener.sendMessage("123:7@hosted", "gateway self echo");
513+
514+
expect(
515+
isRecentOutboundMessage({
516+
accountId: "other-account",
517+
remoteJid: "[email protected]",
518+
messageId: "scoped-hosted-echo",
519+
}),
520+
).toBe(false);
521+
expect(
522+
isRecentOutboundMessage({
523+
accountId: "default",
524+
remoteJid: "[email protected]",
525+
messageId: "other-message",
526+
}),
527+
).toBe(false);
528+
529+
await listener.close();
530+
});
531+
532+
it("never treats another group as an alternate direct-chat echo", async () => {
533+
const { listener, sock } = await startInboxMonitor(vi.fn());
534+
sock.sendMessage.mockResolvedValueOnce({ key: { id: "group-alias-control" } });
535+
await listener.sendMessage("[email protected]", "gateway group message");
536+
537+
expect(
538+
isRecentOutboundMessage({
539+
accountId: "default",
540+
remoteJid: "[email protected]",
541+
alternateRemoteJid: "[email protected]",
542+
messageId: "group-alias-control",
543+
}),
544+
).toBe(false);
545+
546+
await listener.close();
547+
});
548+
418549
it("handles append messages by marking them read but skipping auto-reply", async () => {
419550
const { onMessage, listener, sock } = await openInboxMonitor();
420551
const staleTs = Math.floor(Date.now() / 1000) - 300;

0 commit comments

Comments
 (0)