Skip to content

Commit 5b81702

Browse files
committed
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.
1 parent 6a0fc19 commit 5b81702

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import { downloadMediaMessage } from 'baileys';
99
import { v4 as uuidv4 } from 'uuid';
1010

1111
import { BaseChatbotService } from '../../base-chatbot.service';
12-
12+
import { OpenaiService } from '../../openai/services/openai.service';
1313
export class EvoaiService extends BaseChatbotService<Evoai, EvoaiSetting> {
14+
private openaiService: OpenaiService;
15+
1416
constructor(waMonitor: WAMonitoringService, prismaRepository: PrismaRepository, configService: ConfigService) {
1517
super(waMonitor, prismaRepository, 'EvoaiService', configService);
18+
this.openaiService = new OpenaiService(waMonitor, prismaRepository, configService);
1619
}
1720

1821
/**
@@ -42,33 +45,26 @@ export class EvoaiService extends BaseChatbotService<Evoai, EvoaiSetting> {
4245
try {
4346
this.logger.debug(`[EvoAI] Processing message with custom process method`);
4447

45-
// Check if this is an audio message that we should try to transcribe
46-
if (msg?.messageType === 'audioMessage' && msg?.message?.audioMessage) {
47-
this.logger.debug(`[EvoAI] Detected audio message, attempting transcription`);
48+
let contentProcessed = content;
4849

50+
// Check if this is an audio message that we should try to transcribe
51+
if (this.isAudioMessage(content) && msg) {
4952
try {
50-
// Download the audio using the whole msg object
51-
const mediaBuffer = await downloadMediaMessage(msg, 'buffer', {});
52-
this.logger.debug(`[EvoAI] Downloaded audio: ${mediaBuffer?.length || 0} bytes`);
53-
54-
// Transcribe with OpenAI's Whisper
55-
const transcribedText = await this.speechToText(mediaBuffer);
56-
this.logger.debug(`[EvoAI] Transcription result: ${transcribedText || 'FAILED'}`);
57-
58-
if (transcribedText) {
59-
// Use the transcribed text instead of the original content
60-
this.logger.debug(`[EvoAI] Using transcribed text: ${transcribedText}`);
61-
62-
// Call the parent process method with the transcribed text
63-
return super.process(instance, remoteJid, bot, session, settings, transcribedText, pushName, msg);
53+
this.logger.debug(`[Dify] Downloading audio for Whisper transcription`);
54+
const transcription = await this.openaiService.speechToText(msg);
55+
if (transcription) {
56+
contentProcessed = transcription;
57+
} else {
58+
contentProcessed = '[Audio message could not be transcribed]';
6459
}
6560
} catch (err) {
66-
this.logger.error(`[EvoAI] Audio transcription error: ${err}`);
61+
this.logger.error(`[Dify] Failed to transcribe audio: ${err}`);
62+
contentProcessed = '[Audio message could not be transcribed]';
6763
}
6864
}
6965

7066
// For non-audio messages or if transcription failed, proceed normally
71-
return super.process(instance, remoteJid, bot, session, settings, content, pushName, msg);
67+
return super.process(instance, remoteJid, bot, session, settings, contentProcessed, pushName, msg);
7268
} catch (error) {
7369
this.logger.error(`[EvoAI] Error in process: ${error}`);
7470
return;

src/api/services/channel.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ export class ChannelStartupService {
4545
this.chatwootCache,
4646
);
4747

48-
public typebotService = new TypebotService(waMonitor, this.configService, this.prismaRepository);
49-
5048
public openaiService = new OpenaiService(waMonitor, this.prismaRepository, this.configService);
5149

52-
public difyService = new DifyService(waMonitor, this.configService, this.prismaRepository);
50+
public typebotService = new TypebotService(waMonitor, this.configService, this.prismaRepository, this.openaiService);
51+
52+
public difyService = new DifyService(waMonitor, this.configService, this.prismaRepository, this.openaiService);
5353

5454
public setInstance(instance: InstanceDto) {
5555
this.logger.setInstance(instance.instanceName);

0 commit comments

Comments
 (0)