Skip to content

Commit 42b46e0

Browse files
committed
fix(chatbot): closed session should not block bot re-activation
When a chatbot session exists with status='closed', the emit() method returned early, preventing the bot from re-activating on new messages. Root cause: the guard 'if (session.status === closed) return' was meant to skip sessions not awaiting user input, but it also prevented new conversations from starting after a bot flow completed. Fix: nullify the session instead of returning, so processBot enters the '!session' branch and creates a fresh session. Also adds null guards: - getConversationMessage: return empty string instead of undefined - findBotByTrigger: handle null/undefined content gracefully
1 parent cd800f2 commit 42b46e0

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ export const getConversationMessage = (msg: any) => {
7676

7777
const messageContent = getMessageContent(types);
7878

79-
return messageContent;
79+
return messageContent ?? '';
8080
};

0 commit comments

Comments
 (0)