From 5b817028a90b224bd7e12e0164e721a1fe187c88 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Wed, 21 May 2025 22:27:59 -0300 Subject: [PATCH] refactor(chatbot): enhance EvoaiService with OpenAI integration - Integrated OpenAI service into the EvoaiService to streamline audio message transcription. - Updated the constructor to initialize OpenaiService, allowing for direct transcription of audio messages. - Refactored audio message handling to utilize the OpenAI service for improved reliability and maintainability. - Adjusted the processing logic to handle transcription results more effectively, ensuring fallback content is provided when transcription fails. This commit focuses on enhancing the EvoaiService's capabilities by leveraging OpenAI for audio transcription, improving overall service functionality and code structure. --- .../chatbot/evoai/services/evoai.service.ts | 36 +++++++++---------- src/api/services/channel.service.ts | 6 ++-- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/api/integrations/chatbot/evoai/services/evoai.service.ts b/src/api/integrations/chatbot/evoai/services/evoai.service.ts index 4c282cf3..952a5b5b 100644 --- a/src/api/integrations/chatbot/evoai/services/evoai.service.ts +++ b/src/api/integrations/chatbot/evoai/services/evoai.service.ts @@ -9,10 +9,13 @@ import { downloadMediaMessage } from 'baileys'; import { v4 as uuidv4 } from 'uuid'; import { BaseChatbotService } from '../../base-chatbot.service'; - +import { OpenaiService } from '../../openai/services/openai.service'; export class EvoaiService extends BaseChatbotService { + private openaiService: OpenaiService; + constructor(waMonitor: WAMonitoringService, prismaRepository: PrismaRepository, configService: ConfigService) { super(waMonitor, prismaRepository, 'EvoaiService', configService); + this.openaiService = new OpenaiService(waMonitor, prismaRepository, configService); } /** @@ -42,33 +45,26 @@ export class EvoaiService extends BaseChatbotService { try { this.logger.debug(`[EvoAI] Processing message with custom process method`); + let contentProcessed = content; + // Check if this is an audio message that we should try to transcribe - if (msg?.messageType === 'audioMessage' && msg?.message?.audioMessage) { - this.logger.debug(`[EvoAI] Detected audio message, attempting transcription`); - + if (this.isAudioMessage(content) && msg) { try { - // Download the audio using the whole msg object - const mediaBuffer = await downloadMediaMessage(msg, 'buffer', {}); - this.logger.debug(`[EvoAI] Downloaded audio: ${mediaBuffer?.length || 0} bytes`); - - // Transcribe with OpenAI's Whisper - const transcribedText = await this.speechToText(mediaBuffer); - this.logger.debug(`[EvoAI] Transcription result: ${transcribedText || 'FAILED'}`); - - if (transcribedText) { - // Use the transcribed text instead of the original content - this.logger.debug(`[EvoAI] Using transcribed text: ${transcribedText}`); - - // Call the parent process method with the transcribed text - return super.process(instance, remoteJid, bot, session, settings, transcribedText, pushName, msg); + this.logger.debug(`[Dify] Downloading audio for Whisper transcription`); + const transcription = await this.openaiService.speechToText(msg); + if (transcription) { + contentProcessed = transcription; + } else { + contentProcessed = '[Audio message could not be transcribed]'; } } catch (err) { - this.logger.error(`[EvoAI] Audio transcription error: ${err}`); + this.logger.error(`[Dify] Failed to transcribe audio: ${err}`); + contentProcessed = '[Audio message could not be transcribed]'; } } // For non-audio messages or if transcription failed, proceed normally - return super.process(instance, remoteJid, bot, session, settings, content, pushName, msg); + return super.process(instance, remoteJid, bot, session, settings, contentProcessed, pushName, msg); } catch (error) { this.logger.error(`[EvoAI] Error in process: ${error}`); return; diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index 2dc97f70..1b78a971 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -45,11 +45,11 @@ export class ChannelStartupService { this.chatwootCache, ); - public typebotService = new TypebotService(waMonitor, this.configService, this.prismaRepository); - public openaiService = new OpenaiService(waMonitor, this.prismaRepository, this.configService); - public difyService = new DifyService(waMonitor, this.configService, this.prismaRepository); + public typebotService = new TypebotService(waMonitor, this.configService, this.prismaRepository, this.openaiService); + + public difyService = new DifyService(waMonitor, this.configService, this.prismaRepository, this.openaiService); public setInstance(instance: InstanceDto) { this.logger.setInstance(instance.instanceName);