Skip to content

Commit cb4a14d

Browse files
committed
fix(meta): normalize execution order and fix chatwootIds in Cloud API
Two bugs in BusinessStartupService message processing: 1. Execution order: Chatwoot was processed AFTER the bot emit(), but Baileys channel processes Chatwoot FIRST. This inconsistency meant the bot could not access chatwootConversationId/chatwootInboxId when processing messages from the Cloud API. 2. chatwootIds assignment: chatwootInboxId and chatwootConversationId were both incorrectly set to chatwootSentMessage.id instead of .inbox_id and .conversation_id respectively. Fix: reorder to Chatwoot-first (consistent with Baileys) and use the correct property names from the Chatwoot response object.
1 parent 42b46e0 commit cb4a14d

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/api/integrations/channel/meta/whatsapp.business.service.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -668,29 +668,30 @@ export class BusinessStartupService extends ChannelStartupService {
668668

669669
sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);
670670

671-
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
672-
673-
await chatbotController.emit({
674-
instance: { instanceName: this.instance.name, instanceId: this.instanceId },
675-
remoteJid: messageRaw.key.remoteJid,
676-
msg: messageRaw,
677-
pushName: messageRaw.pushName,
678-
});
679-
671+
// Normalized order: Chatwoot first, then bot (consistent with Baileys channel)
680672
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled) {
681673
const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
682674
Events.MESSAGES_UPSERT,
683675
{ instanceName: this.instance.name, instanceId: this.instanceId },
684676
messageRaw,
685677
);
686678

687-
if (chatwootSentMessage?.id) {
679+
if (chatwootSentMessage) {
688680
messageRaw.chatwootMessageId = chatwootSentMessage.id;
689-
messageRaw.chatwootInboxId = chatwootSentMessage.id;
690-
messageRaw.chatwootConversationId = chatwootSentMessage.id;
681+
messageRaw.chatwootInboxId = chatwootSentMessage.inbox_id;
682+
messageRaw.chatwootConversationId = chatwootSentMessage.conversation_id;
691683
}
692684
}
693685

686+
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
687+
688+
await chatbotController.emit({
689+
instance: { instanceName: this.instance.name, instanceId: this.instanceId },
690+
remoteJid: messageRaw.key.remoteJid,
691+
msg: messageRaw,
692+
pushName: messageRaw.pushName,
693+
});
694+
694695
if (!this.isMediaMessage(message) && message.type !== 'sticker') {
695696
await this.prismaRepository.message.create({
696697
data: messageRaw,

0 commit comments

Comments
 (0)