Added Typebot integration

This commit is contained in:
Davidson Gomes
2023-08-04 15:00:55 -03:00
parent 84386847e2
commit 3b3118d764
13 changed files with 648 additions and 2 deletions

View File

@@ -111,7 +111,7 @@ import {
SendTextDto,
StatusMessage,
} from '../dto/sendMessage.dto';
import { RabbitmqRaw, SettingsRaw } from '../models';
import { RabbitmqRaw, SettingsRaw, TypebotRaw } from '../models';
import { ChatRaw } from '../models/chat.model';
import { ChatwootRaw } from '../models/chatwoot.model';
import { ContactRaw } from '../models/contact.model';
@@ -125,6 +125,7 @@ import { RepositoryBroker } from '../repository/repository.manager';
import { Events, MessageSubtype, TypeMediaMessage, wa } from '../types/wa.types';
import { waMonitor } from '../whatsapp.module';
import { ChatwootService } from './chatwoot.service';
import { TypebotService } from './typebot.service';
export class WAStartupService {
constructor(
@@ -146,6 +147,7 @@ export class WAStartupService {
private readonly localSettings: wa.LocalSettings = {};
private readonly localWebsocket: wa.LocalWebsocket = {};
private readonly localRabbitmq: wa.LocalRabbitmq = {};
private readonly localTypebot: wa.LocalTypebot = {};
public stateConnection: wa.StateConnection = { state: 'close' };
public readonly storePath = join(ROOT_DIR, 'store');
private readonly msgRetryCounterCache: CacheStore = new NodeCache();
@@ -157,6 +159,8 @@ export class WAStartupService {
private chatwootService = new ChatwootService(waMonitor, this.configService);
private typebotService = new TypebotService(waMonitor);
public set instanceName(name: string) {
this.logger.verbose(`Initializing instance '${name}'`);
if (!name) {
@@ -483,6 +487,48 @@ export class WAStartupService {
return data;
}
private async loadTypebot() {
this.logger.verbose('Loading typebot');
const data = await this.repository.typebot.find(this.instanceName);
this.localTypebot.enabled = data?.enabled;
this.logger.verbose(`Typebot enabled: ${this.localTypebot.enabled}`);
this.localTypebot.url = data?.url;
this.logger.verbose(`Typebot url: ${this.localTypebot.url}`);
this.localTypebot.typebot = data?.typebot;
this.logger.verbose(`Typebot typebot: ${this.localTypebot.typebot}`);
this.localTypebot.expire = data?.expire;
this.logger.verbose(`Typebot expire: ${this.localTypebot.expire}`);
this.localTypebot.sessions = data?.sessions;
this.logger.verbose('Typebot loaded');
}
public async setTypebot(data: TypebotRaw) {
this.logger.verbose('Setting typebot');
await this.repository.typebot.create(data, this.instanceName);
this.logger.verbose(`Typebot typebot: ${data.typebot}`);
this.logger.verbose(`Typebot expire: ${data.expire}`);
Object.assign(this.localTypebot, data);
this.logger.verbose('Typebot set');
}
public async findTypebot() {
this.logger.verbose('Finding typebot');
const data = await this.repository.typebot.find(this.instanceName);
if (!data) {
this.logger.verbose('Typebot not found');
throw new NotFoundException('Typebot not found');
}
return data;
}
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
const webhookGlobal = this.configService.get<Webhook>('WEBHOOK');
const webhookLocal = this.localWebhook.events;
@@ -943,6 +989,7 @@ export class WAStartupService {
this.loadSettings();
this.loadWebsocket();
this.loadRabbitmq();
this.loadTypebot();
this.instance.authState = await this.defineAuthState();
@@ -1250,6 +1297,14 @@ export class WAStartupService {
);
}
if (this.localTypebot.enabled) {
await this.typebotService.sendTypebot(
{ instanceName: this.instance.name },
messageRaw.key.remoteJid,
messageRaw,
);
}
this.logger.verbose('Inserting message in database');
await this.repository.message.insert([messageRaw], this.instance.name, database.SAVE_DATA.NEW_MESSAGE);