From 2786f1700898ce4e6ef7661d5bdd132ff89d9ef7 Mon Sep 17 00:00:00 2001 From: Saulo Mendes Martins Date: Thu, 21 Aug 2025 07:36:41 -0300 Subject: [PATCH 1/5] Add develop debug branch --- .../chatbot/base-chatbot.controller.ts | 64 ++++++++++++++----- .../chatbot/openai/services/openai.service.ts | 5 +- src/utils/findBotByTrigger.ts | 5 ++ 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/api/integrations/chatbot/base-chatbot.controller.ts b/src/api/integrations/chatbot/base-chatbot.controller.ts index 3a472cd8..f72d8c3a 100644 --- a/src/api/integrations/chatbot/base-chatbot.controller.ts +++ b/src/api/integrations/chatbot/base-chatbot.controller.ts @@ -775,29 +775,46 @@ export abstract class BaseChatbotController 0) { + this.logger.log(`โฑ๏ธ [${this.integrationName}] Processing with debounce (${debounceTime}s)...`); this.processDebounce(this.userMessageDebounce, content, remoteJid, debounceTime, async (debouncedContent) => { + this.logger.log(`๐Ÿš€ [${this.integrationName}] Debounce complete! Calling processBot...`); + try { + await this.processBot( + this.waMonitor.waInstances[instance.instanceName], + remoteJid, + findBot, + session, + mergedSettings, + debouncedContent, + msg?.pushName, + msg, + ); + this.logger.log(`โœ… [${this.integrationName}] processBot completed successfully`); + } catch (error) { + this.logger.error(`โŒ [${this.integrationName}] Error in processBot: ${error.message}`); + } + }); + } else { + this.logger.log(`๐Ÿš€ [${this.integrationName}] Processing without debounce...`); + try { await this.processBot( this.waMonitor.waInstances[instance.instanceName], remoteJid, findBot, session, mergedSettings, - debouncedContent, + content, msg?.pushName, msg, ); - }); - } else { - await this.processBot( - this.waMonitor.waInstances[instance.instanceName], - remoteJid, - findBot, - session, - mergedSettings, - content, - msg?.pushName, - msg, - ); + this.logger.log(`โœ… [${this.integrationName}] processBot completed successfully`); + } catch (error) { + this.logger.error(`โŒ [${this.integrationName}] Error in processBot: ${error.message}`); + } } } catch (error) { this.logger.error(error); diff --git a/src/api/integrations/chatbot/openai/services/openai.service.ts b/src/api/integrations/chatbot/openai/services/openai.service.ts index dd00b04c..8c299b48 100644 --- a/src/api/integrations/chatbot/openai/services/openai.service.ts +++ b/src/api/integrations/chatbot/openai/services/openai.service.ts @@ -52,7 +52,10 @@ export class OpenaiService extends BaseChatbotService msg?: any, ): Promise { try { - this.logger.log(`Starting process for remoteJid: ${remoteJid}, bot type: ${openaiBot.botType}`); + this.logger.log(`๐Ÿš€ [OpenaiService] PROCESS STARTED - remoteJid: ${remoteJid}, bot type: ${openaiBot.botType}`); + this.logger.log(`๐Ÿค– [OpenaiService] Bot ID: ${openaiBot.id}, enabled: ${openaiBot.enabled}`); + this.logger.log(`๐Ÿ’ฌ [OpenaiService] Content: "${content}"`); + this.logger.log(`๐Ÿ“ฑ [OpenaiService] Session: ${session ? 'EXISTS' : 'NEW'}`); // Handle audio message transcription if (content.startsWith('audioMessage|') && msg) { diff --git a/src/utils/findBotByTrigger.ts b/src/utils/findBotByTrigger.ts index eea5db82..6d3bee92 100644 --- a/src/utils/findBotByTrigger.ts +++ b/src/utils/findBotByTrigger.ts @@ -1,6 +1,8 @@ import { advancedOperatorsSearch } from './advancedOperatorsSearch'; export const findBotByTrigger = async (botRepository: any, content: string, instanceId: string) => { + console.log(`๐Ÿ” [findBotByTrigger] Searching for bot - content: "${content}", instanceId: ${instanceId}`); + // Check for triggerType 'all' or 'none' (both should match any message) const findTriggerAllOrNone = await botRepository.findFirst({ where: { @@ -12,7 +14,10 @@ export const findBotByTrigger = async (botRepository: any, content: string, inst }, }); + console.log(`๐Ÿค– [findBotByTrigger] All/None trigger found: ${findTriggerAllOrNone ? 'YES' : 'NO'} - ID: ${findTriggerAllOrNone?.id || 'NONE'}`); + if (findTriggerAllOrNone) { + console.log(`โœ… [findBotByTrigger] Returning bot with triggerType: ${findTriggerAllOrNone.triggerType}`); return findTriggerAllOrNone; } From 7632ee7f99757f29e82be230926d6988955f8b21 Mon Sep 17 00:00:00 2001 From: Saulo Mendes Martins Date: Thu, 21 Aug 2025 07:45:23 -0300 Subject: [PATCH 2/5] Add develop debug branch --- .../whatsapp/baileysMessage.processor.ts | 5 ++- .../whatsapp/whatsapp.baileys.service.ts | 22 ++++++++--- .../chatbot/chatbot.controller.ts | 37 +++++++++++++++---- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts index 79fedd93..589ee4f3 100644 --- a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts +++ b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts @@ -22,7 +22,10 @@ export class BaileysMessageProcessor { this.subscription = this.messageSubject .pipe( tap(({ messages }) => { - this.processorLogs.log(`Processing batch of ${messages.length} messages`); + this.processorLogs.log(`๐Ÿš€ [BaileysMessageProcessor] Processing batch of ${messages.length} messages`); + messages.forEach((msg, index) => { + this.processorLogs.log(`๐Ÿ“ฑ [BaileysMessageProcessor] Message ${index + 1}: ${msg.key?.remoteJid} - ${msg.message?.conversation || msg.message?.extendedTextMessage?.text || 'NO_TEXT'}`); + }); }), concatMap(({ messages, type, requestId, settings }) => from(onMessageReceive({ messages, type, requestId }, settings)).pipe( diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 758a5bf9..ea2081b5 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1047,7 +1047,10 @@ export class BaileysStartupService extends ChannelStartupService { settings: any, ) => { try { + this.logger.log(`๐Ÿš€ [Baileys] MESSAGES.UPSERT STARTED - type: ${type}, messages: ${messages.length}`); + for (const received of messages) { + this.logger.log(`๐Ÿ“ฑ [Baileys] Processing message: ${received.key.id} from ${received.key.remoteJid} - text: "${received.message?.conversation || received.message?.extendedTextMessage?.text || 'NO_TEXT'}"`); if (received.key.remoteJid?.includes('@lid') && received.key.senderPn) { (received.key as { previousRemoteJid?: string | null }).previousRemoteJid = received.key.remoteJid; received.key.remoteJid = received.key.senderPn; @@ -1336,12 +1339,19 @@ export class BaileysStartupService extends ChannelStartupService { this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw); - await chatbotController.emit({ - instance: { instanceName: this.instance.name, instanceId: this.instanceId }, - remoteJid: messageRaw.key.remoteJid, - msg: messageRaw, - pushName: messageRaw.pushName, - }); + this.logger.log(`๐Ÿค– [Baileys] Calling chatbotController.emit for remoteJid: ${messageRaw.key.remoteJid}`); + + try { + await chatbotController.emit({ + instance: { instanceName: this.instance.name, instanceId: this.instanceId }, + remoteJid: messageRaw.key.remoteJid, + msg: messageRaw, + pushName: messageRaw.pushName, + }); + this.logger.log(`โœ… [Baileys] chatbotController.emit completed successfully`); + } catch (error) { + this.logger.error(`โŒ [Baileys] Error in chatbotController.emit: ${error.message}`); + } const contact = await this.prismaRepository.contact.findFirst({ where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId }, diff --git a/src/api/integrations/chatbot/chatbot.controller.ts b/src/api/integrations/chatbot/chatbot.controller.ts index a2312f6e..3d7dcc45 100644 --- a/src/api/integrations/chatbot/chatbot.controller.ts +++ b/src/api/integrations/chatbot/chatbot.controller.ts @@ -84,6 +84,8 @@ export class ChatbotController { pushName?: string; isIntegration?: boolean; }): Promise { + this.logger.log(`๐Ÿš€ [ChatbotController] EMIT STARTED - remoteJid: ${remoteJid}, instance: ${instance.instanceName}`); + const emitData = { instance, remoteJid, @@ -91,19 +93,40 @@ export class ChatbotController { pushName, isIntegration, }; - await evolutionBotController.emit(emitData); + + try { + this.logger.log(`๐Ÿค– [ChatbotController] Calling evolutionBotController.emit...`); + await evolutionBotController.emit(emitData); + this.logger.log(`โœ… [ChatbotController] evolutionBotController.emit completed`); - await typebotController.emit(emitData); + this.logger.log(`๐Ÿค– [ChatbotController] Calling typebotController.emit...`); + await typebotController.emit(emitData); + this.logger.log(`โœ… [ChatbotController] typebotController.emit completed`); - await openaiController.emit(emitData); + this.logger.log(`๐Ÿค– [ChatbotController] Calling openaiController.emit...`); + await openaiController.emit(emitData); + this.logger.log(`โœ… [ChatbotController] openaiController.emit completed`); - await difyController.emit(emitData); + this.logger.log(`๐Ÿค– [ChatbotController] Calling difyController.emit...`); + await difyController.emit(emitData); + this.logger.log(`โœ… [ChatbotController] difyController.emit completed`); - await n8nController.emit(emitData); + this.logger.log(`๐Ÿค– [ChatbotController] Calling n8nController.emit...`); + await n8nController.emit(emitData); + this.logger.log(`โœ… [ChatbotController] n8nController.emit completed`); - await evoaiController.emit(emitData); + this.logger.log(`๐Ÿค– [ChatbotController] Calling evoaiController.emit...`); + await evoaiController.emit(emitData); + this.logger.log(`โœ… [ChatbotController] evoaiController.emit completed`); - await flowiseController.emit(emitData); + this.logger.log(`๐Ÿค– [ChatbotController] Calling flowiseController.emit...`); + await flowiseController.emit(emitData); + this.logger.log(`โœ… [ChatbotController] flowiseController.emit completed`); + + this.logger.log(`๐ŸŽ‰ [ChatbotController] All controllers completed successfully`); + } catch (error) { + this.logger.error(`โŒ [ChatbotController] Error in emit: ${error.message}`); + } } public processDebounce( From 541a87f6d26b69c2adcdd1ee3426a27dff6f3eab Mon Sep 17 00:00:00 2001 From: Saulo Mendes Martins Date: Thu, 21 Aug 2025 07:48:05 -0300 Subject: [PATCH 3/5] Add develop debug branch --- .../integrations/channel/whatsapp/baileysMessage.processor.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts index 589ee4f3..19f9f892 100644 --- a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts +++ b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts @@ -19,10 +19,12 @@ export class BaileysMessageProcessor { }>(); mount({ onMessageReceive }: MountProps) { + this.processorLogs.log(`๐Ÿงช [TESTE] mount chamado - BaileysMessageProcessor inicializado`); this.subscription = this.messageSubject .pipe( tap(({ messages }) => { this.processorLogs.log(`๐Ÿš€ [BaileysMessageProcessor] Processing batch of ${messages.length} messages`); + this.processorLogs.log(`๐Ÿงช [TESTE] LOG DE TESTE FUNCIONANDO - ${new Date().toISOString()}`); messages.forEach((msg, index) => { this.processorLogs.log(`๐Ÿ“ฑ [BaileysMessageProcessor] Message ${index + 1}: ${msg.key?.remoteJid} - ${msg.message?.conversation || msg.message?.extendedTextMessage?.text || 'NO_TEXT'}`); }); @@ -52,6 +54,7 @@ export class BaileysMessageProcessor { processMessage(payload: MessageUpsertPayload, settings: any) { const { messages, type, requestId } = payload; + this.processorLogs.log(`๐Ÿงช [TESTE] processMessage chamado - messages: ${messages.length}, type: ${type}`); this.messageSubject.next({ messages, type, requestId, settings }); } From 4385db29120a44240b7aa6eeeac126c4b6fe5547 Mon Sep 17 00:00:00 2001 From: Saulo Mendes Martins Date: Thu, 21 Aug 2025 08:08:08 -0300 Subject: [PATCH 4/5] Add develop debug branch --- .../channel/whatsapp/baileysMessage.processor.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts index 19f9f892..7abd6ed7 100644 --- a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts +++ b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts @@ -2,6 +2,9 @@ import { Logger } from '@config/logger.config'; import { BaileysEventMap, MessageUpsertType, proto } from 'baileys'; import { catchError, concatMap, delay, EMPTY, from, retryWhen, Subject, Subscription, take, tap } from 'rxjs'; +// Log de teste que confirma que o arquivo foi carregado +require('fs').writeFileSync('/tmp/debug.log', `๐Ÿงช [TESTE] ARQUIVO CARREGADO - BaileysMessageProcessor - ${new Date().toISOString()}\n`, { flag: 'a' }); + type MessageUpsertPayload = BaileysEventMap['messages.upsert']; type MountProps = { onMessageReceive: (payload: MessageUpsertPayload, settings: any) => Promise; @@ -19,10 +22,14 @@ export class BaileysMessageProcessor { }>(); mount({ onMessageReceive }: MountProps) { + // Log que nรฃo รฉ sobrescrito - escreve em arquivo + require('fs').writeFileSync('/tmp/debug.log', `๐Ÿงช [TESTE] mount chamado - ${new Date().toISOString()}\n`, { flag: 'a' }); this.processorLogs.log(`๐Ÿงช [TESTE] mount chamado - BaileysMessageProcessor inicializado`); this.subscription = this.messageSubject .pipe( tap(({ messages }) => { + // Log que nรฃo รฉ sobrescrito - escreve em arquivo + require('fs').writeFileSync('/tmp/debug.log', `๐Ÿš€ [BaileysMessageProcessor] Processing batch of ${messages.length} messages - ${new Date().toISOString()}\n`, { flag: 'a' }); this.processorLogs.log(`๐Ÿš€ [BaileysMessageProcessor] Processing batch of ${messages.length} messages`); this.processorLogs.log(`๐Ÿงช [TESTE] LOG DE TESTE FUNCIONANDO - ${new Date().toISOString()}`); messages.forEach((msg, index) => { @@ -54,6 +61,8 @@ export class BaileysMessageProcessor { processMessage(payload: MessageUpsertPayload, settings: any) { const { messages, type, requestId } = payload; + // Log que nรฃo รฉ sobrescrito - escreve em arquivo + require('fs').writeFileSync('/tmp/debug.log', `๐Ÿงช [TESTE] processMessage chamado - messages: ${messages.length}, type: ${type} - ${new Date().toISOString()}\n`, { flag: 'a' }); this.processorLogs.log(`๐Ÿงช [TESTE] processMessage chamado - messages: ${messages.length}, type: ${type}`); this.messageSubject.next({ messages, type, requestId, settings }); } From b30c004d7ba5abd8a85bb0d2a24d65fde422ec7d Mon Sep 17 00:00:00 2001 From: Saulo Mendes Martins Date: Thu, 21 Aug 2025 08:08:53 -0300 Subject: [PATCH 5/5] Add develop debug branch --- .../channel/whatsapp/baileysMessage.processor.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts index 7abd6ed7..19f9f892 100644 --- a/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts +++ b/src/api/integrations/channel/whatsapp/baileysMessage.processor.ts @@ -2,9 +2,6 @@ import { Logger } from '@config/logger.config'; import { BaileysEventMap, MessageUpsertType, proto } from 'baileys'; import { catchError, concatMap, delay, EMPTY, from, retryWhen, Subject, Subscription, take, tap } from 'rxjs'; -// Log de teste que confirma que o arquivo foi carregado -require('fs').writeFileSync('/tmp/debug.log', `๐Ÿงช [TESTE] ARQUIVO CARREGADO - BaileysMessageProcessor - ${new Date().toISOString()}\n`, { flag: 'a' }); - type MessageUpsertPayload = BaileysEventMap['messages.upsert']; type MountProps = { onMessageReceive: (payload: MessageUpsertPayload, settings: any) => Promise; @@ -22,14 +19,10 @@ export class BaileysMessageProcessor { }>(); mount({ onMessageReceive }: MountProps) { - // Log que nรฃo รฉ sobrescrito - escreve em arquivo - require('fs').writeFileSync('/tmp/debug.log', `๐Ÿงช [TESTE] mount chamado - ${new Date().toISOString()}\n`, { flag: 'a' }); this.processorLogs.log(`๐Ÿงช [TESTE] mount chamado - BaileysMessageProcessor inicializado`); this.subscription = this.messageSubject .pipe( tap(({ messages }) => { - // Log que nรฃo รฉ sobrescrito - escreve em arquivo - require('fs').writeFileSync('/tmp/debug.log', `๐Ÿš€ [BaileysMessageProcessor] Processing batch of ${messages.length} messages - ${new Date().toISOString()}\n`, { flag: 'a' }); this.processorLogs.log(`๐Ÿš€ [BaileysMessageProcessor] Processing batch of ${messages.length} messages`); this.processorLogs.log(`๐Ÿงช [TESTE] LOG DE TESTE FUNCIONANDO - ${new Date().toISOString()}`); messages.forEach((msg, index) => { @@ -61,8 +54,6 @@ export class BaileysMessageProcessor { processMessage(payload: MessageUpsertPayload, settings: any) { const { messages, type, requestId } = payload; - // Log que nรฃo รฉ sobrescrito - escreve em arquivo - require('fs').writeFileSync('/tmp/debug.log', `๐Ÿงช [TESTE] processMessage chamado - messages: ${messages.length}, type: ${type} - ${new Date().toISOString()}\n`, { flag: 'a' }); this.processorLogs.log(`๐Ÿงช [TESTE] processMessage chamado - messages: ${messages.length}, type: ${type}`); this.messageSubject.next({ messages, type, requestId, settings }); }