Skip to content

Commit 800df08

Browse files
Merge pull request #2429 from sosamilton/fix/meta-cloud-api-chatbot
Fix/meta cloud api chatbot
2 parents 138ff23 + 4c039fd commit 800df08

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
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,

src/api/integrations/chatbot/base-chatbot.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
797797

798798
if (this.checkIgnoreJids(settings?.ignoreJids, remoteJid)) return;
799799

800-
const session = await this.getSession(remoteJid, instance);
800+
let session = await this.getSession(remoteJid, instance);
801801

802802
const content = getConversationMessage(msg);
803803

@@ -896,9 +896,9 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
896896
return;
897897
}
898898

899-
// Skip if session exists but not awaiting user input
899+
// If session is closed, nullify it so processBot creates a new conversation
900900
if (session && session.status === 'closed') {
901-
return;
901+
session = null;
902902
}
903903

904904
// Merged settings

src/utils/findBotByTrigger.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { advancedOperatorsSearch } from './advancedOperatorsSearch';
22

33
export const findBotByTrigger = async (botRepository: any, content: string, instanceId: string) => {
4+
const normalizedContent = content?.trim() || '';
5+
46
// Check for triggerType 'all' or 'none' (both should match any message)
57
const findTriggerAllOrNone = await botRepository.findFirst({
68
where: {
@@ -16,6 +18,12 @@ export const findBotByTrigger = async (botRepository: any, content: string, inst
1618
return findTriggerAllOrNone;
1719
}
1820

21+
// If content is empty (null, undefined, whitespace-only, or media-only messages),
22+
// only 'all'/'none' triggers apply — skip keyword/regex matching
23+
if (!normalizedContent) {
24+
return null;
25+
}
26+
1927
const findTriggerAdvanced = await botRepository.findMany({
2028
where: {
2129
enabled: true,

src/utils/getConversationMessage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,6 @@ const getMessageContent = (types: any) => {
8585
export const getConversationMessage = (msg: any) => {
8686
const types = getTypeMessage(msg);
8787
const messageContent = getMessageContent(types);
88-
return messageContent;
88+
89+
return messageContent ?? '';
8990
};

0 commit comments

Comments
 (0)