fix(whatsapp): drop fromMe echoes in self-chat DMs using outbound ID tracking

This commit is contained in:
joelnishanth 2026-03-25 09:46:12 -07:00 committed by Marcus Castro
parent 6fd9d2ff38
commit c88acdb59a
No known key found for this signature in database
GPG Key ID: AA66624B544825AA
2 changed files with 43 additions and 2 deletions

View File

@ -218,8 +218,10 @@ export async function monitorWebInbox(options: {
}
const group = isGroupJid(remoteJid);
// Drop echoes of messages the gateway itself sent (tracked by sendTrackedMessage).
// Applies to both groups and DMs/self-chat — without this, self-chat mode
// re-processes the bot's own replies as new inbound user messages.
if (
group &&
Boolean(msg.key?.fromMe) &&
id &&
isRecentOutboundMessage({
@ -228,7 +230,7 @@ export async function monitorWebInbox(options: {
messageId: id,
})
) {
logVerbose(`Skipping recent outbound WhatsApp group echo ${id} for ${remoteJid}`);
logVerbose(`Skipping recent outbound WhatsApp echo ${id} for ${remoteJid}`);
return null;
}
if (id) {

View File

@ -298,6 +298,45 @@ describe("web monitor inbox", () => {
await listener.close();
});
it("filters self-chat DM fromMe echoes when the gateway sent the matching message id", async () => {
mockLoadConfig.mockReturnValue({
channels: {
whatsapp: {
selfChatMode: true,
allowFrom: ["+123"],
},
},
messages: DEFAULT_MESSAGES_CFG,
});
const onMessage = vi.fn();
const { listener, sock } = await startInboxMonitor(onMessage);
sock.sendMessage.mockResolvedValueOnce({ key: { id: "bot-dm-echo-1" } });
await listener.sendMessage("123@s.whatsapp.net", "self-chat reply");
sock.ev.emit("messages.upsert", {
type: "notify",
messages: [
{
key: {
id: "bot-dm-echo-1",
fromMe: true,
remoteJid: "123@s.whatsapp.net",
},
message: { conversation: "self-chat reply" },
messageTimestamp: nowSeconds(),
pushName: "Owner",
},
],
});
await settleInboundWork();
expect(onMessage).not.toHaveBeenCalled();
await listener.close();
});
it("handles append messages by marking them read but skipping auto-reply", async () => {
const { onMessage, listener, sock } = await openInboxMonitor();
const staleTs = Math.floor(Date.now() / 1000) - 300;