From 22e99f7934bd88da9f1dd175e702912de393357c Mon Sep 17 00:00:00 2001 From: Guilherme Gomes Date: Tue, 27 May 2025 13:07:15 -0300 Subject: [PATCH] refactor: simplify TypebotController and TypebotService methods This commit refactors the TypebotController and TypebotService to streamline the processTypebot method, aligning it with the base class pattern. It reduces complexity by consolidating parameters and improving readability. Additionally, it updates the TypebotDto and TypebotSettingDto to remove unused properties, enhancing code clarity and maintainability. --- .../typebot/controllers/typebot.controller.ts | 45 ++++++--------- .../chatbot/typebot/dto/typebot.dto.ts | 24 -------- .../chatbot/typebot/routes/typebot.router.ts | 5 +- .../typebot/services/typebot.service.ts | 55 ++++--------------- 4 files changed, 32 insertions(+), 97 deletions(-) diff --git a/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts b/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts index 2d710976..58f61bd5 100644 --- a/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts +++ b/src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts @@ -90,34 +90,20 @@ export class TypebotController extends BaseChatbotController { if (this.isAudioMessage(content) && msg) { try { this.logger.debug(`[EvolutionBot] Downloading audio for Whisper transcription`); - const transcription = await this.openaiService.speechToText(msg); + const transcription = await this.openaiService.speechToText(msg, instance); if (transcription) { - reqData.message = transcription; - } else { - reqData.message = '[Audio message could not be transcribed]'; + reqData.message = `[audio] ${transcription}`; } } catch (err) { this.logger.error(`[EvolutionBot] Failed to transcribe audio: ${err}`); - reqData.message = '[Audio message could not be transcribed]'; } } @@ -107,9 +103,6 @@ export class TypebotService extends BaseChatbotService { response?.data?.input, response?.data?.clientSideActions, ); - - // Send telemetry data - sendTelemetry('/message/sendText'); } catch (error) { this.logger.error(`Error in sendMessageToBot for Typebot: ${error.message || JSON.stringify(error)}`); } @@ -526,44 +519,20 @@ export class TypebotService extends BaseChatbotService { return { text, buttons: [] }; } } - /** - * Main process method for handling Typebot messages - * This is called directly from the controller + * Simplified method that matches the base class pattern + * This should be the preferred way for the controller to call */ public async processTypebot( - instance: Instance, + instance: any, remoteJid: string, - msg: Message, - session: IntegrationSession, bot: TypebotModel, - url: string, - expire: number, - typebot: string, - keywordFinish: string, - delayMessage: number, - unknownMessage: string, - listeningFromMe: boolean, - stopBotFromMe: boolean, - keepOpen: boolean, + session: IntegrationSession, + settings: any, content: string, - prefilledVariables?: any, - ) { - try { - const settings = { - expire, - keywordFinish, - delayMessage, - unknownMessage, - listeningFromMe, - stopBotFromMe, - keepOpen, - }; - - // Use the base class process method to handle the message - await this.process(instance, remoteJid, bot, session, settings, content, msg.pushName, prefilledVariables || msg); - } catch (error) { - this.logger.error(`Error in processTypebot: ${error}`); - } + pushName?: string, + msg?: any, + ): Promise { + return this.process(instance, remoteJid, bot, session, settings, content, pushName, msg); } }