Skip to content

Commit 679b56e

Browse files
committed
fix(chatwoot): skip duplicate messages when device redelivers same key.id
When a sender device redelivers a message with the same key.id (for example an unofficial client stuck in a resend loop), the live event path posted a new Chatwoot message on every arrival; one production report accumulated 837 copies of a single message. Reuse the dedup check that sendData already applies to media messages: query Chatwoot's messages.source_id through getExistingSourceIds before posting, gated by isImportHistoryAvailable, and skip the message when the WAID source id already exists in the target conversation. The helper is fail open: on lookup errors it logs and returns an empty set, so delivery is never blocked. The check sits at the top of the messages.upsert/send.message block so it covers text, reactions, interactive buttons, ads and media, and does not touch the messages.edit path, which legitimately re-posts using the original message's source id. Fixes #2675
1 parent e273b90 commit 679b56e

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,17 @@ export class ChatwootService {
22402240
return;
22412241
}
22422242

2243+
if (body.key.id && this.isImportHistoryAvailable()) {
2244+
const sourceId = 'WAID:' + body.key.id;
2245+
const messageAlreadySaved = await chatwootImport.getExistingSourceIds([sourceId], getConversation);
2246+
if (messageAlreadySaved) {
2247+
if (messageAlreadySaved.size > 0) {
2248+
this.logger.warn('Message already saved on chatwoot');
2249+
return;
2250+
}
2251+
}
2252+
}
2253+
22432254
const messageType = body.key.fromMe ? 'outgoing' : 'incoming';
22442255

22452256
if (isMedia) {

0 commit comments

Comments
 (0)