added typebot integration activation

This commit is contained in:
Davidson Gomes 2024-06-06 16:44:18 -03:00
parent 1be9c7f95d
commit 161814a9e5
5 changed files with 52 additions and 25 deletions

View File

@ -4,7 +4,7 @@ import { isURL } from 'class-validator';
import EventEmitter2 from 'eventemitter2'; import EventEmitter2 from 'eventemitter2';
import { v4 } from 'uuid'; 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 { Logger } from '../../config/logger.config';
import { BadRequestException, InternalServerErrorException, UnauthorizedException } from '../../exceptions'; import { BadRequestException, InternalServerErrorException, UnauthorizedException } from '../../exceptions';
import { InstanceDto, SetPresenceDto } from '../dto/instance.dto'; import { InstanceDto, SetPresenceDto } from '../dto/instance.dto';
@ -381,7 +381,7 @@ export class InstanceController {
}); });
} }
if (typebotUrl) { if (this.configService.get<Typebot>('TYPEBOT').ENABLED && typebotUrl) {
try { try {
if (!isURL(typebotUrl, { require_tld: false })) { if (!isURL(typebotUrl, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in typebotUrl'); throw new BadRequestException('Invalid "url" property in typebotUrl');

View File

@ -1,4 +1,6 @@
import { configService, Typebot } from '../../../../config/env.config';
import { Logger } from '../../../../config/logger.config'; import { Logger } from '../../../../config/logger.config';
import { BadRequestException } from '../../../../exceptions';
import { InstanceDto } from '../../../dto/instance.dto'; import { InstanceDto } from '../../../dto/instance.dto';
import { TypebotDto } from '../dto/typebot.dto'; import { TypebotDto } from '../dto/typebot.dto';
import { TypebotService } from '../services/typebot.service'; import { TypebotService } from '../services/typebot.service';
@ -9,6 +11,8 @@ export class TypebotController {
constructor(private readonly typebotService: TypebotService) {} constructor(private readonly typebotService: TypebotService) {}
public async createTypebot(instance: InstanceDto, data: TypebotDto) { public async createTypebot(instance: InstanceDto, data: TypebotDto) {
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
logger.verbose('requested createTypebot from ' + instance.instanceName + ' instance'); logger.verbose('requested createTypebot from ' + instance.instanceName + ' instance');
if (!data.enabled) { if (!data.enabled) {
@ -30,16 +34,22 @@ export class TypebotController {
} }
public async findTypebot(instance: InstanceDto) { public async findTypebot(instance: InstanceDto) {
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
logger.verbose('requested findTypebot from ' + instance.instanceName + ' instance'); logger.verbose('requested findTypebot from ' + instance.instanceName + ' instance');
return this.typebotService.find(instance); return this.typebotService.find(instance);
} }
public async changeStatus(instance: InstanceDto, data: any) { public async changeStatus(instance: InstanceDto, data: any) {
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
logger.verbose('requested changeStatus from ' + instance.instanceName + ' instance'); logger.verbose('requested changeStatus from ' + instance.instanceName + ' instance');
return this.typebotService.changeStatus(instance, data); return this.typebotService.changeStatus(instance, data);
} }
public async startTypebot(instance: InstanceDto, data: any) { public async startTypebot(instance: InstanceDto, data: any) {
if (!configService.get<Typebot>('TYPEBOT').ENABLED) throw new BadRequestException('Typebot is disabled');
logger.verbose('requested startTypebot from ' + instance.instanceName + ' instance'); logger.verbose('requested startTypebot from ' + instance.instanceName + ' instance');
return this.typebotService.startTypebot(instance, data); return this.typebotService.startTypebot(instance, data);
} }

View File

@ -16,6 +16,7 @@ import {
Log, Log,
Rabbitmq, Rabbitmq,
Sqs, Sqs,
Typebot,
Webhook, Webhook,
Websocket, Websocket,
} from '../../config/env.config'; } from '../../config/env.config';
@ -681,6 +682,9 @@ export class ChannelStartupService {
} }
public async loadTypebot() { public async loadTypebot() {
if (!this.configService.get<Typebot>('TYPEBOT').ENABLED) {
return;
}
this.logger.verbose('Loading typebot'); this.logger.verbose('Loading typebot');
const data = await this.prismaRepository.typebot.findUnique({ const data = await this.prismaRepository.typebot.findUnique({
where: { where: {
@ -721,6 +725,10 @@ export class ChannelStartupService {
} }
public async setTypebot(data: TypebotDto) { public async setTypebot(data: TypebotDto) {
if (!this.configService.get<Typebot>('TYPEBOT').ENABLED) {
this.logger.verbose('Typebot is not enabled');
return;
}
this.logger.verbose('Setting typebot'); this.logger.verbose('Setting typebot');
const typebot = await this.prismaRepository.typebot.create({ const typebot = await this.prismaRepository.typebot.create({
@ -754,6 +762,10 @@ export class ChannelStartupService {
} }
public async findTypebot() { public async findTypebot() {
if (!this.configService.get<Typebot>('TYPEBOT').ENABLED) {
this.logger.verbose('Typebot is not enabled');
return;
}
this.logger.verbose('Finding typebot'); this.logger.verbose('Finding typebot');
const data = await this.prismaRepository.typebot.findUnique({ const data = await this.prismaRepository.typebot.findUnique({
where: { where: {

View File

@ -64,6 +64,7 @@ import {
Log, Log,
ProviderSession, ProviderSession,
QrCode, QrCode,
Typebot,
} from '../../../config/env.config'; } from '../../../config/env.config';
import { INSTANCE_DIR } from '../../../config/path.config'; import { INSTANCE_DIR } from '../../../config/path.config';
import { BadRequestException, InternalServerErrorException, NotFoundException } from '../../../exceptions'; import { BadRequestException, InternalServerErrorException, NotFoundException } from '../../../exceptions';
@ -1225,6 +1226,7 @@ export class BaileysStartupService extends ChannelStartupService {
} }
} }
if (this.configService.get<Typebot>('TYPEBOT').ENABLED) {
const typebotSessionRemoteJid = this.localTypebot.sessions?.find( const typebotSessionRemoteJid = this.localTypebot.sessions?.find(
(session) => session.remoteJid === received.key.remoteJid, (session) => session.remoteJid === received.key.remoteJid,
); );
@ -1239,6 +1241,7 @@ export class BaileysStartupService extends ChannelStartupService {
); );
} }
} }
}
this.logger.verbose('Inserting message in database'); this.logger.verbose('Inserting message in database');
await this.prismaRepository.message.create({ await this.prismaRepository.message.create({

View File

@ -5,7 +5,7 @@ import FormData from 'form-data';
import fs from 'fs/promises'; import fs from 'fs/promises';
import { getMIMEType } from 'node-mime-types'; 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 { BadRequestException, InternalServerErrorException } from '../../../exceptions';
import { NumberBusiness } from '../../dto/chat.dto'; import { NumberBusiness } from '../../dto/chat.dto';
import { import {
@ -425,6 +425,7 @@ export class BusinessStartupService extends ChannelStartupService {
} }
} }
if (this.configService.get<Typebot>('TYPEBOT').ENABLED) {
const typebotSessionRemoteJid = this.localTypebot.sessions?.find( const typebotSessionRemoteJid = this.localTypebot.sessions?.find(
(session) => session.remoteJid === key.remoteJid, (session) => session.remoteJid === key.remoteJid,
); );
@ -439,6 +440,7 @@ export class BusinessStartupService extends ChannelStartupService {
); );
} }
} }
}
this.logger.verbose('Inserting message in database'); this.logger.verbose('Inserting message in database');
await this.prismaRepository.message.create({ await this.prismaRepository.message.create({