From 22048fe2117c784faf5292dfd69bfb1a5dbabbfb Mon Sep 17 00:00:00 2001 From: ValdecirMysian Date: Thu, 29 Jan 2026 13:03:06 -0300 Subject: [PATCH] Refactor message data handling in chatwoot.service.ts Filtered null/undefined values from replyToIds before sending and constructed messageData object to include valid content_attributes. --- .../chatwoot/services/chatwoot.service.ts | 54 +++++++++++++------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index ff6d28f3..2a0117aa 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -947,20 +947,39 @@ export class ChatwootService { const sourceReplyId = quotedMsg?.chatwootMessageId || null; + // Filtra valores null/undefined do content_attributes para evitar erro 406 + const filteredReplyToIds = Object.fromEntries( + Object.entries(replyToIds).filter(([_, value]) => value != null) + ); + + // Monta o objeto data, incluindo content_attributes apenas se houver dados vĂ¡lidos + const messageData: any = { + content: content, + message_type: messageType, + content_type: 'text', // Explicitamente define como texto para Chatwoot 4.x + attachments: attachments, + private: privateMessage || false, + }; + + // Adiciona source_id apenas se existir + if (sourceId) { + messageData.source_id = sourceId; + } + + // Adiciona content_attributes apenas se houver dados vĂ¡lidos + if (Object.keys(filteredReplyToIds).length > 0) { + messageData.content_attributes = filteredReplyToIds; + } + + // Adiciona source_reply_id apenas se existir + if (sourceReplyId) { + messageData.source_reply_id = sourceReplyId.toString(); + } + const message = await client.messages.create({ accountId: this.provider.accountId, conversationId: conversationId, - data: { - content: content, - message_type: messageType, - attachments: attachments, - private: privateMessage || false, - source_id: sourceId, - content_attributes: { - ...replyToIds, - }, - source_reply_id: sourceReplyId ? sourceReplyId.toString() : null, - }, + data: messageData, }); if (!message) { @@ -1086,11 +1105,14 @@ export class ChatwootService { if (messageBody && instance) { const replyToIds = await this.getReplyToIds(messageBody, instance); - if (replyToIds.in_reply_to || replyToIds.in_reply_to_external_id) { - const content = JSON.stringify({ - ...replyToIds, - }); - data.append('content_attributes', content); + // Filtra valores null/undefined antes de enviar + const filteredReplyToIds = Object.fromEntries( + Object.entries(replyToIds).filter(([_, value]) => value != null) + ); + + if (Object.keys(filteredReplyToIds).length > 0) { + const contentAttrs = JSON.stringify(filteredReplyToIds); + data.append('content_attributes', contentAttrs); } }