Skip to content

Commit 1930f6a

Browse files
possebonclaude
andcommitted
fix(chatwoot): fix multi-attachment message sending failures
Three bugs in the attachment send loop caused multi-file messages from Chatwoot to fail: 1. No error isolation: if one attachment failed, all subsequent ones were skipped. Now each attachment has its own try/catch. 2. Caption duplication: the message text was sent with every attachment. Now only the first attachment carries the caption. 3. No delay between sends: rapid sequential sends overwhelmed Baileys. Added 1.5s delay between attachments. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0bda8ef commit 1930f6a

1 file changed

Lines changed: 41 additions & 25 deletions

File tree

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

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,38 +1506,54 @@ export class ChatwootService {
15061506

15071507
for (const message of body.conversation.messages) {
15081508
if (message.attachments && message.attachments.length > 0) {
1509+
let isFirstAttachment = true;
1510+
15091511
for (const attachment of message.attachments) {
1510-
if (!messageReceived) {
1511-
formatText = null;
1512-
}
1512+
// Only send caption with the first attachment to avoid duplication
1513+
const caption = !messageReceived ? null : isFirstAttachment ? formatText : null;
15131514

15141515
const options: Options = {
1515-
quoted: await this.getQuotedMessage(body, instance),
1516+
quoted: isFirstAttachment ? await this.getQuotedMessage(body, instance) : undefined,
15161517
};
15171518

1518-
const messageSent = await this.sendAttachment(
1519-
waInstance,
1520-
chatId,
1521-
attachment.data_url,
1522-
formatText,
1523-
options,
1524-
);
1525-
if (!messageSent && body.conversation?.id) {
1526-
this.onSendMessageError(instance, body.conversation?.id);
1519+
try {
1520+
// Add delay between sequential attachments to avoid overwhelming Baileys
1521+
if (!isFirstAttachment) {
1522+
await new Promise((resolve) => setTimeout(resolve, 1500));
1523+
}
1524+
1525+
const messageSent = await this.sendAttachment(
1526+
waInstance,
1527+
chatId,
1528+
attachment.data_url,
1529+
caption,
1530+
options,
1531+
);
1532+
if (!messageSent && body.conversation?.id) {
1533+
this.onSendMessageError(instance, body.conversation?.id);
1534+
}
1535+
1536+
await this.updateChatwootMessageId(
1537+
{
1538+
...messageSent,
1539+
},
1540+
{
1541+
messageId: body.id,
1542+
inboxId: body.inbox?.id,
1543+
conversationId: body.conversation?.id,
1544+
contactInboxSourceId: body.conversation?.contact_inbox?.source_id,
1545+
},
1546+
instance,
1547+
);
1548+
} catch (error) {
1549+
this.logger.error(`Failed to send attachment: ${error?.message || error}`);
1550+
if (body.conversation?.id) {
1551+
this.onSendMessageError(instance, body.conversation.id, error);
1552+
}
1553+
// Continue sending remaining attachments instead of aborting
15271554
}
15281555

1529-
await this.updateChatwootMessageId(
1530-
{
1531-
...messageSent,
1532-
},
1533-
{
1534-
messageId: body.id,
1535-
inboxId: body.inbox?.id,
1536-
conversationId: body.conversation?.id,
1537-
contactInboxSourceId: body.conversation?.contact_inbox?.source_id,
1538-
},
1539-
instance,
1540-
);
1556+
isFirstAttachment = false;
15411557
}
15421558
} else {
15431559
const data: SendTextDto = {

0 commit comments

Comments
 (0)