feat: Now you can register several typebots with triggers

This commit is contained in:
Davidson Gomes
2024-06-08 19:04:57 -03:00
parent 56df0caab0
commit fe9803b828
19 changed files with 1023 additions and 282 deletions

View File

@@ -16,7 +16,6 @@ import {
Log,
Rabbitmq,
Sqs,
Typebot,
Webhook,
Websocket,
} from '../../config/env.config';
@@ -33,7 +32,6 @@ import { RabbitmqDto } from '../integrations/rabbitmq/dto/rabbitmq.dto';
import { getAMQP, removeQueues } from '../integrations/rabbitmq/libs/amqp.server';
import { SqsDto } from '../integrations/sqs/dto/sqs.dto';
import { getSQS, removeQueues as removeQueuesSQS } from '../integrations/sqs/libs/sqs.server';
import { TypebotDto } from '../integrations/typebot/dto/typebot.dto';
import { TypebotService } from '../integrations/typebot/services/typebot.service';
import { WebsocketDto } from '../integrations/websocket/dto/websocket.dto';
import { getIO } from '../integrations/websocket/libs/socket.server';
@@ -59,7 +57,6 @@ export class ChannelStartupService {
public readonly localWebsocket: wa.LocalWebsocket = {};
public readonly localRabbitmq: wa.LocalRabbitmq = {};
public readonly localSqs: wa.LocalSqs = {};
public readonly localTypebot: wa.LocalTypebot = {};
public readonly localProxy: wa.LocalProxy = {};
public readonly localSettings: wa.LocalSettings = {};
public readonly storePath = join(ROOT_DIR, 'store');
@@ -71,7 +68,7 @@ export class ChannelStartupService {
this.chatwootCache,
);
public typebotService = new TypebotService(waMonitor, this.configService, this.prismaRepository, this.eventEmitter);
public typebotService = new TypebotService(waMonitor, this.configService, this.prismaRepository);
public setInstance(instance: InstanceDto) {
this.instance.name = instance.instanceName;
@@ -478,78 +475,6 @@ export class ChannelStartupService {
}
}
public async loadTypebot() {
if (!this.configService.get<Typebot>('TYPEBOT').ENABLED) {
return;
}
const data = await this.prismaRepository.typebot.findUnique({
where: {
instanceId: this.instanceId,
},
include: {
sessions: true,
},
});
this.localTypebot.enabled = data?.enabled;
this.localTypebot.url = data?.url;
this.localTypebot.typebot = data?.typebot;
this.localTypebot.expire = data?.expire;
this.localTypebot.keywordFinish = data?.keywordFinish;
this.localTypebot.delayMessage = data?.delayMessage;
this.localTypebot.unknownMessage = data?.unknownMessage;
this.localTypebot.listeningFromMe = data?.listeningFromMe;
this.localTypebot.sessions = data?.sessions;
}
public async setTypebot(data: TypebotDto) {
if (!this.configService.get<Typebot>('TYPEBOT').ENABLED) {
return;
}
const typebot = await this.prismaRepository.typebot.create({
data: {
enabled: data.enabled,
url: data.url,
typebot: data.typebot,
expire: data.expire,
keywordFinish: data.keywordFinish,
delayMessage: data.delayMessage,
unknownMessage: data.unknownMessage,
listeningFromMe: data.listeningFromMe,
instanceId: this.instanceId,
},
});
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: typebot.id,
},
});
Object.assign(this.localTypebot, data);
}
public async findTypebot() {
if (!this.configService.get<Typebot>('TYPEBOT').ENABLED) {
return;
}
const data = await this.prismaRepository.typebot.findUnique({
where: {
instanceId: this.instanceId,
},
include: {
sessions: true,
},
});
if (!data) {
throw new NotFoundException('Typebot not found');
}
return data;
}
public async loadProxy() {
const data = await this.prismaRepository.proxy.findUnique({
where: {

View File

@@ -502,7 +502,6 @@ export class BaileysStartupService extends ChannelStartupService {
this.loadWebsocket();
this.loadRabbitmq();
this.loadSqs();
this.loadTypebot();
this.loadProxy();
this.instance.authState = await this.defineAuthState();
@@ -1182,19 +1181,13 @@ export class BaileysStartupService extends ChannelStartupService {
}
if (this.configService.get<Typebot>('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, instanceId: this.instanceId },
messageRaw.key.remoteJid,
messageRaw,
);
}
if (type === 'notify') {
if (messageRaw.messageType !== 'reactionMessage')
await this.typebotService.sendTypebot(
{ instanceName: this.instance.name, instanceId: this.instanceId },
messageRaw.key.remoteJid,
messageRaw,
);
}
}

View File

@@ -133,7 +133,6 @@ export class BusinessStartupService extends ChannelStartupService {
this.loadWebsocket();
this.loadRabbitmq();
this.loadSqs();
this.loadTypebot();
this.eventHandler(content);
@@ -399,20 +398,12 @@ export class BusinessStartupService extends ChannelStartupService {
}
if (this.configService.get<Typebot>('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 (messageRaw.messageType !== 'reactionMessage')
await this.typebotService.sendTypebot(
{ instanceName: this.instance.name },
messageRaw.key.remoteJid,
messageRaw,
);
}
await this.prismaRepository.message.create({

View File

@@ -262,6 +262,7 @@ export class WAMonitoringService {
public async saveInstance(data: any) {
try {
if (this.db.ENABLED) {
const clientName = await this.configService.get<Database>('DATABASE').CONNECTION.CLIENT_NAME;
await this.prismaRepository.instance.create({
data: {
id: data.instanceId,
@@ -270,6 +271,7 @@ export class WAMonitoringService {
number: data.number,
integration: data.integration || Integration.WHATSAPP_BAILEYS,
token: data.hash,
clientName: clientName,
},
});
}