Skip to content

Commit 22048fe

Browse files
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.
1 parent 367153e commit 22048fe

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -947,20 +947,39 @@ export class ChatwootService {
947947

948948
const sourceReplyId = quotedMsg?.chatwootMessageId || null;
949949

950+
// Filtra valores null/undefined do content_attributes para evitar erro 406
951+
const filteredReplyToIds = Object.fromEntries(
952+
Object.entries(replyToIds).filter(([_, value]) => value != null)
953+
);
954+
955+
// Monta o objeto data, incluindo content_attributes apenas se houver dados válidos
956+
const messageData: any = {
957+
content: content,
958+
message_type: messageType,
959+
content_type: 'text', // Explicitamente define como texto para Chatwoot 4.x
960+
attachments: attachments,
961+
private: privateMessage || false,
962+
};
963+
964+
// Adiciona source_id apenas se existir
965+
if (sourceId) {
966+
messageData.source_id = sourceId;
967+
}
968+
969+
// Adiciona content_attributes apenas se houver dados válidos
970+
if (Object.keys(filteredReplyToIds).length > 0) {
971+
messageData.content_attributes = filteredReplyToIds;
972+
}
973+
974+
// Adiciona source_reply_id apenas se existir
975+
if (sourceReplyId) {
976+
messageData.source_reply_id = sourceReplyId.toString();
977+
}
978+
950979
const message = await client.messages.create({
951980
accountId: this.provider.accountId,
952981
conversationId: conversationId,
953-
data: {
954-
content: content,
955-
message_type: messageType,
956-
attachments: attachments,
957-
private: privateMessage || false,
958-
source_id: sourceId,
959-
content_attributes: {
960-
...replyToIds,
961-
},
962-
source_reply_id: sourceReplyId ? sourceReplyId.toString() : null,
963-
},
982+
data: messageData,
964983
});
965984

966985
if (!message) {
@@ -1086,11 +1105,14 @@ export class ChatwootService {
10861105
if (messageBody && instance) {
10871106
const replyToIds = await this.getReplyToIds(messageBody, instance);
10881107

1089-
if (replyToIds.in_reply_to || replyToIds.in_reply_to_external_id) {
1090-
const content = JSON.stringify({
1091-
...replyToIds,
1092-
});
1093-
data.append('content_attributes', content);
1108+
// Filtra valores null/undefined antes de enviar
1109+
const filteredReplyToIds = Object.fromEntries(
1110+
Object.entries(replyToIds).filter(([_, value]) => value != null)
1111+
);
1112+
1113+
if (Object.keys(filteredReplyToIds).length > 0) {
1114+
const contentAttrs = JSON.stringify(filteredReplyToIds);
1115+
data.append('content_attributes', contentAttrs);
10941116
}
10951117
}
10961118

0 commit comments

Comments
 (0)