Skip to content

Commit 22e99f7

Browse files
committed
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.
1 parent dd0dfd4 commit 22e99f7

File tree

4 files changed

+32
-97
lines changed

4 files changed

+32
-97
lines changed

src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -90,34 +90,20 @@ export class TypebotController extends BaseChatbotController<TypebotModel, Typeb
9090
pushName?: string,
9191
msg?: any,
9292
) {
93-
await this.typebotService.processTypebot(
94-
instance,
95-
remoteJid,
96-
msg,
97-
session,
98-
bot,
99-
bot.url,
100-
settings.expire,
101-
bot.typebot,
102-
settings.keywordFinish,
103-
settings.delayMessage,
104-
settings.unknownMessage,
105-
settings.listeningFromMe,
106-
settings.stopBotFromMe,
107-
settings.keepOpen,
108-
content,
109-
);
93+
// Use the simplified service method that follows the base class pattern
94+
await this.typebotService.processTypebot(instance, remoteJid, bot, session, settings, content, pushName, msg);
11095
}
11196

11297
// TypeBot specific method for starting a bot from API
11398
public async startBot(instance: InstanceDto, data: any) {
114-
if (!this.integrationEnabled) throw new BadRequestException('Typebot is disabled');
99+
if (!this.integrationEnabled)
100+
throw new BadRequestException('Typebot is disabled');
115101

116102
if (data.remoteJid === 'status@broadcast') return;
117103

118104
const instanceData = await this.prismaRepository.instance.findFirst({
119105
where: {
120-
name: instance.instanceName,
106+
id: instance.instanceId,
121107
},
122108
});
123109

@@ -226,22 +212,25 @@ export class TypebotController extends BaseChatbotController<TypebotModel, Typeb
226212
},
227213
});
228214

229-
await this.typebotService.processTypebot(
230-
instanceData,
231-
remoteJid,
232-
null,
233-
null,
234-
findBot,
235-
url,
215+
// Use the simplified service method instead of the complex one
216+
const settings = {
236217
expire,
237-
typebot,
238218
keywordFinish,
239219
delayMessage,
240220
unknownMessage,
241221
listeningFromMe,
242222
stopBotFromMe,
243223
keepOpen,
224+
};
225+
226+
await this.typebotService.processTypebot(
227+
instanceData,
228+
remoteJid,
229+
findBot,
230+
null, // session
231+
settings,
244232
'init',
233+
null, // pushName
245234
prefilledVariables,
246235
);
247236
} else {
@@ -287,7 +276,7 @@ export class TypebotController extends BaseChatbotController<TypebotModel, Typeb
287276
request.data.clientSideActions,
288277
);
289278

290-
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_START, {
279+
this.waMonitor.waInstances[instance.instanceId].sendDataWebhook(Events.TYPEBOT_START, {
291280
remoteJid: remoteJid,
292281
url: url,
293282
typebot: typebot,
Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { TriggerOperator, TriggerType } from '@prisma/client';
2-
31
import { BaseChatbotDto, BaseChatbotSettingDto } from '../../base-chatbot.dto';
42

53
export class PrefilledVariables {
@@ -12,30 +10,8 @@ export class PrefilledVariables {
1210
export class TypebotDto extends BaseChatbotDto {
1311
url: string;
1412
typebot: string;
15-
description: string;
16-
expire?: number;
17-
keywordFinish?: string | null;
18-
delayMessage?: number;
19-
unknownMessage?: string;
20-
listeningFromMe?: boolean;
21-
stopBotFromMe?: boolean;
22-
keepOpen?: boolean;
23-
debounceTime?: number;
24-
triggerType: TriggerType;
25-
triggerOperator?: TriggerOperator;
26-
triggerValue?: string;
27-
ignoreJids?: any;
2813
}
2914

3015
export class TypebotSettingDto extends BaseChatbotSettingDto {
31-
expire?: number;
32-
keywordFinish?: string | null;
33-
delayMessage?: number;
34-
unknownMessage?: string;
35-
listeningFromMe?: boolean;
36-
stopBotFromMe?: boolean;
37-
keepOpen?: boolean;
38-
debounceTime?: number;
3916
typebotIdFallback?: string;
40-
ignoreJids?: any;
4117
}

src/api/integrations/chatbot/typebot/routes/typebot.router.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { RouterBroker } from '@api/abstract/abstract.router';
21
import { IgnoreJidDto } from '@api/dto/chatbot.dto';
32
import { InstanceDto } from '@api/dto/instance.dto';
43
import { TypebotDto, TypebotSettingDto } from '@api/integrations/chatbot/typebot/dto/typebot.dto';
54
import { HttpStatus } from '@api/routes/index.router';
6-
import { typebotController } from '@api/server.module';
75
import {
86
instanceSchema,
97
typebotIgnoreJidSchema,
@@ -12,8 +10,11 @@ import {
1210
typebotStartSchema,
1311
typebotStatusSchema,
1412
} from '@validate/validate.schema';
13+
import { typebotController } from '@api/server.module';
1514
import { RequestHandler, Router } from 'express';
1615

16+
import { RouterBroker } from '@api/abstract/abstract.router';
17+
1718
export class TypebotRouter extends RouterBroker {
1819
constructor(...guards: RequestHandler[]) {
1920
super();

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

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { PrismaRepository } from '@api/repository/repository.service';
22
import { WAMonitoringService } from '@api/services/monitor.service';
33
import { Auth, ConfigService, HttpServer, Typebot } from '@config/env.config';
4-
import { Instance, IntegrationSession, Message, Typebot as TypebotModel } from '@prisma/client';
5-
import { sendTelemetry } from '@utils/sendTelemetry';
4+
import { IntegrationSession, Typebot as TypebotModel } from '@prisma/client';
65
import axios from 'axios';
76

87
import { BaseChatbotService } from '../../base-chatbot.service';
@@ -83,15 +82,12 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
8382
if (this.isAudioMessage(content) && msg) {
8483
try {
8584
this.logger.debug(`[EvolutionBot] Downloading audio for Whisper transcription`);
86-
const transcription = await this.openaiService.speechToText(msg);
85+
const transcription = await this.openaiService.speechToText(msg, instance);
8786
if (transcription) {
88-
reqData.message = transcription;
89-
} else {
90-
reqData.message = '[Audio message could not be transcribed]';
87+
reqData.message = `[audio] ${transcription}`;
9188
}
9289
} catch (err) {
9390
this.logger.error(`[EvolutionBot] Failed to transcribe audio: ${err}`);
94-
reqData.message = '[Audio message could not be transcribed]';
9591
}
9692
}
9793

@@ -107,9 +103,6 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
107103
response?.data?.input,
108104
response?.data?.clientSideActions,
109105
);
110-
111-
// Send telemetry data
112-
sendTelemetry('/message/sendText');
113106
} catch (error) {
114107
this.logger.error(`Error in sendMessageToBot for Typebot: ${error.message || JSON.stringify(error)}`);
115108
}
@@ -526,44 +519,20 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
526519
return { text, buttons: [] };
527520
}
528521
}
529-
530522
/**
531-
* Main process method for handling Typebot messages
532-
* This is called directly from the controller
523+
* Simplified method that matches the base class pattern
524+
* This should be the preferred way for the controller to call
533525
*/
534526
public async processTypebot(
535-
instance: Instance,
527+
instance: any,
536528
remoteJid: string,
537-
msg: Message,
538-
session: IntegrationSession,
539529
bot: TypebotModel,
540-
url: string,
541-
expire: number,
542-
typebot: string,
543-
keywordFinish: string,
544-
delayMessage: number,
545-
unknownMessage: string,
546-
listeningFromMe: boolean,
547-
stopBotFromMe: boolean,
548-
keepOpen: boolean,
530+
session: IntegrationSession,
531+
settings: any,
549532
content: string,
550-
prefilledVariables?: any,
551-
) {
552-
try {
553-
const settings = {
554-
expire,
555-
keywordFinish,
556-
delayMessage,
557-
unknownMessage,
558-
listeningFromMe,
559-
stopBotFromMe,
560-
keepOpen,
561-
};
562-
563-
// Use the base class process method to handle the message
564-
await this.process(instance, remoteJid, bot, session, settings, content, msg.pushName, prefilledVariables || msg);
565-
} catch (error) {
566-
this.logger.error(`Error in processTypebot: ${error}`);
567-
}
533+
pushName?: string,
534+
msg?: any,
535+
): Promise<void> {
536+
return this.process(instance, remoteJid, bot, session, settings, content, pushName, msg);
568537
}
569538
}

0 commit comments

Comments
 (0)