Skip to content

Commit 2786f17

Browse files
committed
Add develop debug branch
1 parent 9cdb897 commit 2786f17

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

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

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -775,29 +775,46 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
775775

776776
// Base implementation for emit
777777
public async emit({ instance, remoteJid, msg }: EmitData) {
778-
if (!this.integrationEnabled) return;
778+
this.logger.log(`🚀 [${this.integrationName}] EMIT STARTED - remoteJid: ${remoteJid}, instance: ${instance.instanceName}`);
779+
780+
if (!this.integrationEnabled) {
781+
this.logger.warn(`❌ [${this.integrationName}] Integration is DISABLED`);
782+
return;
783+
}
779784

780785
try {
786+
this.logger.log(`🔍 [${this.integrationName}] Looking for settings...`);
781787
const settings = await this.settingsRepository.findFirst({
782788
where: {
783789
instanceId: instance.instanceId,
784790
},
785791
});
792+
793+
this.logger.log(`⚙️ [${this.integrationName}] Settings found: ${settings ? 'YES' : 'NO'}`);
786794

787-
if (this.checkIgnoreJids(settings?.ignoreJids, remoteJid)) return;
795+
if (this.checkIgnoreJids(settings?.ignoreJids, remoteJid)) {
796+
this.logger.warn(`🚫 [${this.integrationName}] Message ignored due to ignoreJids`);
797+
return;
798+
}
788799

800+
this.logger.log(`🔍 [${this.integrationName}] Looking for session...`);
789801
const session = await this.getSession(remoteJid, instance);
802+
this.logger.log(`📱 [${this.integrationName}] Session found: ${session ? 'YES' : 'NO'}`);
790803

791804
const content = getConversationMessage(msg);
805+
this.logger.log(`💬 [${this.integrationName}] Content: ${content}`);
792806

793807
// Get integration type
794808
// const integrationType = this.getIntegrationType();
795809

796810
// Find a bot for this message
811+
this.logger.log(`🤖 [${this.integrationName}] Looking for bot trigger...`);
797812
let findBot: any = await this.findBotTrigger(this.botRepository, content, instance, session);
813+
this.logger.log(`🤖 [${this.integrationName}] Bot found: ${findBot ? 'YES' : 'NO'} - ID: ${findBot?.id || 'NONE'}`);
798814

799815
// If no bot is found, try to use fallback
800816
if (!findBot) {
817+
this.logger.warn(`⚠️ [${this.integrationName}] No bot found, trying fallback...`);
801818
const fallback = await this.settingsRepository.findFirst({
802819
where: {
803820
instanceId: instance.instanceId,
@@ -806,6 +823,7 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
806823

807824
// Get the fallback ID for this integration type
808825
const fallbackId = this.getFallbackBotId(fallback);
826+
this.logger.log(`🔄 [${this.integrationName}] Fallback ID: ${fallbackId || 'NONE'}`);
809827

810828
if (fallbackId) {
811829
const findFallback = await this.botRepository.findFirst({
@@ -815,13 +833,16 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
815833
});
816834

817835
findBot = findFallback;
836+
this.logger.log(`🔄 [${this.integrationName}] Fallback bot found: ${findFallback ? 'YES' : 'NO'}`);
818837
} else {
838+
this.logger.warn(`❌ [${this.integrationName}] No fallback bot available`);
819839
return;
820840
}
821841
}
822842

823843
// If we still don't have a bot, return
824844
if (!findBot) {
845+
this.logger.warn(`❌ [${this.integrationName}] Still no bot found, returning`);
825846
return;
826847
}
827848

@@ -904,29 +925,42 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
904925

905926
// Process with debounce if needed
906927
if (debounceTime && debounceTime > 0) {
928+
this.logger.log(`⏱️ [${this.integrationName}] Processing with debounce (${debounceTime}s)...`);
907929
this.processDebounce(this.userMessageDebounce, content, remoteJid, debounceTime, async (debouncedContent) => {
930+
this.logger.log(`🚀 [${this.integrationName}] Debounce complete! Calling processBot...`);
931+
try {
932+
await this.processBot(
933+
this.waMonitor.waInstances[instance.instanceName],
934+
remoteJid,
935+
findBot,
936+
session,
937+
mergedSettings,
938+
debouncedContent,
939+
msg?.pushName,
940+
msg,
941+
);
942+
this.logger.log(`✅ [${this.integrationName}] processBot completed successfully`);
943+
} catch (error) {
944+
this.logger.error(`❌ [${this.integrationName}] Error in processBot: ${error.message}`);
945+
}
946+
});
947+
} else {
948+
this.logger.log(`🚀 [${this.integrationName}] Processing without debounce...`);
949+
try {
908950
await this.processBot(
909951
this.waMonitor.waInstances[instance.instanceName],
910952
remoteJid,
911953
findBot,
912954
session,
913955
mergedSettings,
914-
debouncedContent,
956+
content,
915957
msg?.pushName,
916958
msg,
917959
);
918-
});
919-
} else {
920-
await this.processBot(
921-
this.waMonitor.waInstances[instance.instanceName],
922-
remoteJid,
923-
findBot,
924-
session,
925-
mergedSettings,
926-
content,
927-
msg?.pushName,
928-
msg,
929-
);
960+
this.logger.log(`✅ [${this.integrationName}] processBot completed successfully`);
961+
} catch (error) {
962+
this.logger.error(`❌ [${this.integrationName}] Error in processBot: ${error.message}`);
963+
}
930964
}
931965
} catch (error) {
932966
this.logger.error(error);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
5252
msg?: any,
5353
): Promise<void> {
5454
try {
55-
this.logger.log(`Starting process for remoteJid: ${remoteJid}, bot type: ${openaiBot.botType}`);
55+
this.logger.log(`🚀 [OpenaiService] PROCESS STARTED - remoteJid: ${remoteJid}, bot type: ${openaiBot.botType}`);
56+
this.logger.log(`🤖 [OpenaiService] Bot ID: ${openaiBot.id}, enabled: ${openaiBot.enabled}`);
57+
this.logger.log(`💬 [OpenaiService] Content: "${content}"`);
58+
this.logger.log(`📱 [OpenaiService] Session: ${session ? 'EXISTS' : 'NEW'}`);
5659

5760
// Handle audio message transcription
5861
if (content.startsWith('audioMessage|') && msg) {

src/utils/findBotByTrigger.ts

Lines changed: 5 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+
console.log(`🔍 [findBotByTrigger] Searching for bot - content: "${content}", instanceId: ${instanceId}`);
5+
46
// Check for triggerType 'all' or 'none' (both should match any message)
57
const findTriggerAllOrNone = await botRepository.findFirst({
68
where: {
@@ -12,7 +14,10 @@ export const findBotByTrigger = async (botRepository: any, content: string, inst
1214
},
1315
});
1416

17+
console.log(`🤖 [findBotByTrigger] All/None trigger found: ${findTriggerAllOrNone ? 'YES' : 'NO'} - ID: ${findTriggerAllOrNone?.id || 'NONE'}`);
18+
1519
if (findTriggerAllOrNone) {
20+
console.log(`✅ [findBotByTrigger] Returning bot with triggerType: ${findTriggerAllOrNone.triggerType}`);
1621
return findTriggerAllOrNone;
1722
}
1823

0 commit comments

Comments
 (0)