diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index cd530148..24e5e239 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -4,7 +4,7 @@ import { isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; import { v4 } from 'uuid'; -import { Auth, Chatwoot, ConfigService, HttpServer, WaBusiness } from '../../config/env.config'; +import { Auth, Chatwoot, ConfigService, HttpServer, Typebot, WaBusiness } from '../../config/env.config'; import { Logger } from '../../config/logger.config'; import { BadRequestException, InternalServerErrorException, UnauthorizedException } from '../../exceptions'; import { InstanceDto, SetPresenceDto } from '../dto/instance.dto'; @@ -381,7 +381,7 @@ export class InstanceController { }); } - if (typebotUrl) { + if (this.configService.get('TYPEBOT').ENABLED && typebotUrl) { try { if (!isURL(typebotUrl, { require_tld: false })) { throw new BadRequestException('Invalid "url" property in typebotUrl'); diff --git a/src/api/integrations/typebot/controllers/typebot.controller.ts b/src/api/integrations/typebot/controllers/typebot.controller.ts index bb52a370..c4c9684b 100644 --- a/src/api/integrations/typebot/controllers/typebot.controller.ts +++ b/src/api/integrations/typebot/controllers/typebot.controller.ts @@ -1,4 +1,6 @@ +import { configService, Typebot } from '../../../../config/env.config'; import { Logger } from '../../../../config/logger.config'; +import { BadRequestException } from '../../../../exceptions'; import { InstanceDto } from '../../../dto/instance.dto'; import { TypebotDto } from '../dto/typebot.dto'; import { TypebotService } from '../services/typebot.service'; @@ -9,6 +11,8 @@ export class TypebotController { constructor(private readonly typebotService: TypebotService) {} public async createTypebot(instance: InstanceDto, data: TypebotDto) { + if (!configService.get('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled'); + logger.verbose('requested createTypebot from ' + instance.instanceName + ' instance'); if (!data.enabled) { @@ -30,16 +34,22 @@ export class TypebotController { } public async findTypebot(instance: InstanceDto) { + if (!configService.get('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled'); + logger.verbose('requested findTypebot from ' + instance.instanceName + ' instance'); return this.typebotService.find(instance); } public async changeStatus(instance: InstanceDto, data: any) { + if (!configService.get('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled'); + logger.verbose('requested changeStatus from ' + instance.instanceName + ' instance'); return this.typebotService.changeStatus(instance, data); } public async startTypebot(instance: InstanceDto, data: any) { + if (!configService.get('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled'); + logger.verbose('requested startTypebot from ' + instance.instanceName + ' instance'); return this.typebotService.startTypebot(instance, data); } diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index 208f85b4..b9e0d902 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -16,6 +16,7 @@ import { Log, Rabbitmq, Sqs, + Typebot, Webhook, Websocket, } from '../../config/env.config'; @@ -681,6 +682,9 @@ export class ChannelStartupService { } public async loadTypebot() { + if (!this.configService.get('TYPEBOT').ENABLED) { + return; + } this.logger.verbose('Loading typebot'); const data = await this.prismaRepository.typebot.findUnique({ where: { @@ -721,6 +725,10 @@ export class ChannelStartupService { } public async setTypebot(data: TypebotDto) { + if (!this.configService.get('TYPEBOT').ENABLED) { + this.logger.verbose('Typebot is not enabled'); + return; + } this.logger.verbose('Setting typebot'); const typebot = await this.prismaRepository.typebot.create({ @@ -754,6 +762,10 @@ export class ChannelStartupService { } public async findTypebot() { + if (!this.configService.get('TYPEBOT').ENABLED) { + this.logger.verbose('Typebot is not enabled'); + return; + } this.logger.verbose('Finding typebot'); const data = await this.prismaRepository.typebot.findUnique({ where: { diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index ea2a18e4..a2056d87 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -64,6 +64,7 @@ import { Log, ProviderSession, QrCode, + Typebot, } from '../../../config/env.config'; import { INSTANCE_DIR } from '../../../config/path.config'; import { BadRequestException, InternalServerErrorException, NotFoundException } from '../../../exceptions'; @@ -1225,18 +1226,20 @@ export class BaileysStartupService extends ChannelStartupService { } } - const typebotSessionRemoteJid = this.localTypebot.sessions?.find( - (session) => session.remoteJid === received.key.remoteJid, - ); + if (this.configService.get('TYPEBOT').ENABLED) { + const typebotSessionRemoteJid = this.localTypebot.sessions?.find( + (session) => session.remoteJid === received.key.remoteJid, + ); - if ((this.localTypebot.enabled && type === 'notify') || typebotSessionRemoteJid) { - if (!(this.localTypebot.listeningFromMe === false && messageRaw.key.fromMe === true)) { - if (messageRaw.messageType !== 'reactionMessage') - await this.typebotService.sendTypebot( - { instanceName: this.instance.name }, - messageRaw.key.remoteJid, - messageRaw, - ); + if ((this.localTypebot.enabled && type === 'notify') || typebotSessionRemoteJid) { + if (!(this.localTypebot.listeningFromMe === false && messageRaw.key.fromMe === true)) { + if (messageRaw.messageType !== 'reactionMessage') + await this.typebotService.sendTypebot( + { instanceName: this.instance.name }, + messageRaw.key.remoteJid, + messageRaw, + ); + } } } diff --git a/src/api/services/channels/whatsapp.business.service.ts b/src/api/services/channels/whatsapp.business.service.ts index ed0a8d7a..7d61219e 100644 --- a/src/api/services/channels/whatsapp.business.service.ts +++ b/src/api/services/channels/whatsapp.business.service.ts @@ -5,7 +5,7 @@ import FormData from 'form-data'; import fs from 'fs/promises'; import { getMIMEType } from 'node-mime-types'; -import { Chatwoot, ConfigService, Database, WaBusiness } from '../../../config/env.config'; +import { Chatwoot, ConfigService, Database, Typebot, WaBusiness } from '../../../config/env.config'; import { BadRequestException, InternalServerErrorException } from '../../../exceptions'; import { NumberBusiness } from '../../dto/chat.dto'; import { @@ -425,18 +425,20 @@ export class BusinessStartupService extends ChannelStartupService { } } - const typebotSessionRemoteJid = this.localTypebot.sessions?.find( - (session) => session.remoteJid === key.remoteJid, - ); + if (this.configService.get('TYPEBOT').ENABLED) { + const typebotSessionRemoteJid = this.localTypebot.sessions?.find( + (session) => session.remoteJid === key.remoteJid, + ); - if (this.localTypebot.enabled || typebotSessionRemoteJid) { - if (!(this.localTypebot.listeningFromMe === false && key.fromMe === true)) { - if (messageRaw.messageType !== 'reactionMessage') - await this.typebotService.sendTypebot( - { instanceName: this.instance.name }, - messageRaw.key.remoteJid, - messageRaw, - ); + if (this.localTypebot.enabled || typebotSessionRemoteJid) { + if (!(this.localTypebot.listeningFromMe === false && key.fromMe === true)) { + if (messageRaw.messageType !== 'reactionMessage') + await this.typebotService.sendTypebot( + { instanceName: this.instance.name }, + messageRaw.key.remoteJid, + messageRaw, + ); + } } }