From f8f2153cb4ae80bff1b805281e3ccfcdbd22bb80 Mon Sep 17 00:00:00 2001 From: Rafael Souza Date: Thu, 17 Jul 2025 15:29:04 -0300 Subject: [PATCH 1/5] Fix erro key --- .../chatwoot/services/chatwoot.service.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index ba31ab33..cdac0fcc 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -567,6 +567,12 @@ export class ChatwootService { } public async createConversation(instance: InstanceDto, body: any) { + if (!body?.key) { + this.logger.warn('body.key is null or undefined in createConversation'); + this.logger.warn('Full body object:', JSON.stringify(body, null, 2)); + return null; + } + const isLid = body.key.previousRemoteJid?.includes('@lid') && body.key.senderPn; const remoteJid = body.key.remoteJid; const cacheKey = `${instance.instanceName}:createConversation-${remoteJid}`; @@ -1938,6 +1944,12 @@ export class ChatwootService { } if (event === 'messages.upsert' || event === 'send.message') { + if (!body?.key) { + this.logger.warn('body.key is null or undefined'); + this.logger.warn('Full body object:', JSON.stringify(body, null, 2)); + return; + } + if (body.key.remoteJid === 'status@broadcast') { return; } @@ -2260,10 +2272,16 @@ export class ChatwootService { } if (event === 'messages.edit' || event === 'send.message.update') { + if (!body?.key?.id) { + this.logger.warn('body.key.id is null or undefined in messages.edit'); + this.logger.warn('Full body object:', JSON.stringify(body, null, 2)); + return; + } + const editedText = `${ body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text }\n\n_\`${i18next.t('cw.message.edited')}.\`_`; - const message = await this.getMessageByKeyId(instance, body?.key?.id); + const message = await this.getMessageByKeyId(instance, body.key.id); const key = message.key as { id: string; fromMe: boolean; From e304b1dcdfc34ee812a6eeda0cb09f62322ad96d Mon Sep 17 00:00:00 2001 From: Rafael Souza Date: Thu, 17 Jul 2025 15:32:25 -0300 Subject: [PATCH 2/5] Fix erro key --- .../chatbot/chatwoot/services/chatwoot.service.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index cdac0fcc..a72e1329 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -568,8 +568,7 @@ export class ChatwootService { public async createConversation(instance: InstanceDto, body: any) { if (!body?.key) { - this.logger.warn('body.key is null or undefined in createConversation'); - this.logger.warn('Full body object:', JSON.stringify(body, null, 2)); + this.logger.warn(`body.key is null or undefined in createConversation. Full body object: ${JSON.stringify(body)}`); return null; } @@ -1945,8 +1944,7 @@ export class ChatwootService { if (event === 'messages.upsert' || event === 'send.message') { if (!body?.key) { - this.logger.warn('body.key is null or undefined'); - this.logger.warn('Full body object:', JSON.stringify(body, null, 2)); + this.logger.warn(`body.key is null or undefined. Full body object: ${JSON.stringify(body)}`); return; } @@ -2273,8 +2271,7 @@ export class ChatwootService { if (event === 'messages.edit' || event === 'send.message.update') { if (!body?.key?.id) { - this.logger.warn('body.key.id is null or undefined in messages.edit'); - this.logger.warn('Full body object:', JSON.stringify(body, null, 2)); + this.logger.warn(`body.key.id is null or undefined in messages.edit. Full body object: ${JSON.stringify(body)}`); return; } From 44d4781f6f01f2372c8ac3b2db105e7255901ce8 Mon Sep 17 00:00:00 2001 From: Rafael Souza Date: Thu, 17 Jul 2025 15:37:37 -0300 Subject: [PATCH 3/5] Ignore events that are not messages --- .../chatbot/chatwoot/services/chatwoot.service.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index a72e1329..f7ee2249 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -1898,6 +1898,12 @@ export class ChatwootService { public async eventWhatsapp(event: string, instance: InstanceDto, body: any) { try { + // Ignora eventos que não são mensagens (como EPHEMERAL_SYNC_RESPONSE) + if (body?.type && body.type !== 'message' && body.type !== 'conversation') { + this.logger.verbose(`Ignoring non-message event type: ${body.type}`); + return; + } + const waInstance = this.waMonitor.waInstances[instance.instanceName]; if (!waInstance) { @@ -2270,6 +2276,12 @@ export class ChatwootService { } if (event === 'messages.edit' || event === 'send.message.update') { + // Ignora eventos que não são mensagens (como EPHEMERAL_SYNC_RESPONSE) + if (body?.type && body.type !== 'message') { + this.logger.verbose(`Ignoring non-message event type: ${body.type}`); + return; + } + if (!body?.key?.id) { this.logger.warn(`body.key.id is null or undefined in messages.edit. Full body object: ${JSON.stringify(body)}`); return; From b3dae7a68e80438d9b258797da636531355823f0 Mon Sep 17 00:00:00 2001 From: Rafael Souza Date: Thu, 17 Jul 2025 15:40:31 -0300 Subject: [PATCH 4/5] Ignore events that are not messages --- .../chatbot/chatwoot/services/chatwoot.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index f7ee2249..6e7f9969 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -1898,7 +1898,7 @@ export class ChatwootService { public async eventWhatsapp(event: string, instance: InstanceDto, body: any) { try { - // Ignora eventos que não são mensagens (como EPHEMERAL_SYNC_RESPONSE) + // Ignore events that are not messages (like EPHEMERAL_SYNC_RESPONSE) if (body?.type && body.type !== 'message' && body.type !== 'conversation') { this.logger.verbose(`Ignoring non-message event type: ${body.type}`); return; @@ -2276,7 +2276,7 @@ export class ChatwootService { } if (event === 'messages.edit' || event === 'send.message.update') { - // Ignora eventos que não são mensagens (como EPHEMERAL_SYNC_RESPONSE) + // Ignore events that are not messages (like EPHEMERAL_SYNC_RESPONSE) if (body?.type && body.type !== 'message') { this.logger.verbose(`Ignoring non-message event type: ${body.type}`); return; From afd0e01ddb38918a1940518048ca3fd22b0d449d Mon Sep 17 00:00:00 2001 From: Rafael Souza Date: Thu, 17 Jul 2025 15:59:33 -0300 Subject: [PATCH 5/5] fix lint --- .../chatwoot/services/chatwoot.service.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index 6e7f9969..5561ee6f 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -568,10 +568,12 @@ export class ChatwootService { public async createConversation(instance: InstanceDto, body: any) { if (!body?.key) { - this.logger.warn(`body.key is null or undefined in createConversation. Full body object: ${JSON.stringify(body)}`); + this.logger.warn( + `body.key is null or undefined in createConversation. Full body object: ${JSON.stringify(body)}`, + ); return null; } - + const isLid = body.key.previousRemoteJid?.includes('@lid') && body.key.senderPn; const remoteJid = body.key.remoteJid; const cacheKey = `${instance.instanceName}:createConversation-${remoteJid}`; @@ -1903,7 +1905,7 @@ export class ChatwootService { this.logger.verbose(`Ignoring non-message event type: ${body.type}`); return; } - + const waInstance = this.waMonitor.waInstances[instance.instanceName]; if (!waInstance) { @@ -1953,7 +1955,7 @@ export class ChatwootService { this.logger.warn(`body.key is null or undefined. Full body object: ${JSON.stringify(body)}`); return; } - + if (body.key.remoteJid === 'status@broadcast') { return; } @@ -2281,12 +2283,14 @@ export class ChatwootService { this.logger.verbose(`Ignoring non-message event type: ${body.type}`); return; } - + if (!body?.key?.id) { - this.logger.warn(`body.key.id is null or undefined in messages.edit. Full body object: ${JSON.stringify(body)}`); + this.logger.warn( + `body.key.id is null or undefined in messages.edit. Full body object: ${JSON.stringify(body)}`, + ); return; } - + const editedText = `${ body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text }\n\n_\`${i18next.t('cw.message.edited')}.\`_`;