diff --git a/CHANGELOG.md b/CHANGELOG.md index d793a0ad7..9bb494b94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * [WIDGET-WORKS] Fix `PrismaClientValidationError` in `messages.update` (missing `Message` relation) by guarding `messageUpdate.create` calls. * [WIDGET-WORKS] Guard `messages.update` cache cleanup with a derived key and optional `MESSAGE_UPDATE_CACHE_DELETE_DISABLED` flag to prevent cache.delete crash-loops. * [WIDGET-WORKS] Save messages before Chatwoot sync in `/message/sendText` to avoid missing `chatwootMessageId` and log async sync failures. +* [WIDGET-WORKS] Cache sent message keys in Baileys so WhatsApp echoes are ignored (prevents Chatwoot automation duplicates when `isIntegration=true`). * [WIDGET-WORKS] Skip Chatwoot sync in `messages.upsert` when a message key already has `chatwootMessageId` (handles Chatwoot automation echoes/duplicates). * [WIDGET-WORKS] Avoid dropping group conversations when Chatwoot `updateContact` fails by logging the error and continuing with the existing contact. diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 98832cc04..ac221411f 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -2212,6 +2212,16 @@ export class BaileysStartupService extends ChannelStartupService { const messageRaw = this.prepareMessage(messageSent); + // [WIDGET-WORKS] Cache sent message key so messages.upsert treats the WhatsApp echo as duplicate + try { + const messageKey = `${this.instance.id}_${messageRaw.key.id}`; + await this.baileysCache.set(messageKey, true, 5 * 60); + } catch (error) { + this.logger.warn( + `[WIDGET-WORKS] Failed to cache sent message ${messageRaw?.key?.id ?? 'unknown'}: ${error?.message}`, + ); + } + const isMedia = messageSent?.message?.imageMessage || messageSent?.message?.videoMessage ||