added chatwoot integration activation

This commit is contained in:
Davidson Gomes 2024-06-06 16:38:43 -03:00
parent 99c8cc0242
commit 1be9c7f95d
10 changed files with 119 additions and 58 deletions

View File

@ -119,9 +119,10 @@ CONFIG_SESSION_PHONE_VERSION='2,2413,1'
QRCODE_LIMIT=30 QRCODE_LIMIT=30
QRCODE_COLOR='#175197' QRCODE_COLOR='#175197'
TYPEBOT_ENABLED=false
TYPEBOT_API_VERSION=latest TYPEBOT_API_VERSION=latest
CHATWOOT_MESSAGE_DELETE=false CHATWOOT_ENABLED=false
CHATWOOT_MESSAGE_READ=false CHATWOOT_MESSAGE_READ=false
CHATWOOT_IMPORT_DATABASE_CONNECTION_URI=postgresql://user:pass@host:5432/dbname CHATWOOT_IMPORT_DATABASE_CONNECTION_URI=postgresql://user:pass@host:5432/dbname
CHATWOOT_IMPORT_PLACEHOLDER_MEDIA_MESSAGE=false CHATWOOT_IMPORT_PLACEHOLDER_MEDIA_MESSAGE=false

View File

@ -196,11 +196,12 @@ model Label {
model Proxy { model Proxy {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
enabled Boolean? @default(true) @db.Boolean enabled Boolean @default(false) @db.Boolean
host String? @db.VarChar(100) host String @db.VarChar(100)
port String? @db.VarChar(100) port String @db.VarChar(100)
username String? @db.VarChar(100) protocol String @db.VarChar(100)
password String? @db.VarChar(100) username String @db.VarChar(100)
password String @db.VarChar(100)
createdAt DateTime? @default(now()) @db.Date createdAt DateTime? @default(now()) @db.Date
updatedAt DateTime @updatedAt @db.Date updatedAt DateTime @updatedAt @db.Date
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
@ -209,13 +210,13 @@ model Proxy {
model Setting { model Setting {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
rejectCall Boolean? @default(false) @db.Boolean rejectCall Boolean @default(false) @db.Boolean
msgCall String? @db.VarChar(100) msgCall String? @db.VarChar(100)
groupsIgnore Boolean? @default(false) @db.Boolean groupsIgnore Boolean @default(false) @db.Boolean
alwaysOnline Boolean? @default(false) @db.Boolean alwaysOnline Boolean @default(false) @db.Boolean
readMessages Boolean? @default(false) @db.Boolean readMessages Boolean @default(false) @db.Boolean
readStatus Boolean? @default(false) @db.Boolean readStatus Boolean @default(false) @db.Boolean
syncFullHistory Boolean? @default(false) @db.Boolean syncFullHistory Boolean @default(false) @db.Boolean
createdAt DateTime? @default(now()) @db.Date createdAt DateTime? @default(now()) @db.Date
updatedAt DateTime @updatedAt @db.Date updatedAt DateTime @updatedAt @db.Date
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
@ -224,8 +225,8 @@ model Setting {
model Rabbitmq { model Rabbitmq {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
enabled Boolean? @default(false) @db.Boolean enabled Boolean @default(false) @db.Boolean
events Json? @db.JsonB events Json @db.JsonB
createdAt DateTime? @default(now()) @db.Date createdAt DateTime? @default(now()) @db.Date
updatedAt DateTime @updatedAt @db.Date updatedAt DateTime @updatedAt @db.Date
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
@ -234,8 +235,8 @@ model Rabbitmq {
model Sqs { model Sqs {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
enabled Boolean? @default(false) @db.Boolean enabled Boolean @default(false) @db.Boolean
events Json? @db.JsonB events Json @db.JsonB
createdAt DateTime? @default(now()) @db.Date createdAt DateTime? @default(now()) @db.Date
updatedAt DateTime @updatedAt @db.Date updatedAt DateTime @updatedAt @db.Date
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
@ -244,8 +245,8 @@ model Sqs {
model Websocket { model Websocket {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
enabled Boolean? @default(false) @db.Boolean enabled Boolean @default(false) @db.Boolean
events Json? @db.JsonB events Json @db.JsonB
createdAt DateTime? @default(now()) @db.Date createdAt DateTime? @default(now()) @db.Date
updatedAt DateTime @updatedAt @db.Date updatedAt DateTime @updatedAt @db.Date
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
@ -254,14 +255,14 @@ model Websocket {
model Typebot { model Typebot {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
enabled Boolean? @default(true) @db.Boolean enabled Boolean @default(true) @db.Boolean
url String @db.VarChar(500) url String @db.VarChar(500)
typebot String @db.VarChar(100) typebot String @db.VarChar(100)
expire Int? @db.Integer expire Int @default(0) @db.Integer
keywordFinish String? @db.VarChar(100) keywordFinish String? @db.VarChar(100)
delayMessage Int? @db.Integer delayMessage Int? @db.Integer
unknownMessage String? @db.VarChar(100) unknownMessage String? @db.VarChar(100)
listeningFromMe Boolean? @default(false) @db.Boolean listeningFromMe Boolean @default(false) @db.Boolean
createdAt DateTime? @default(now()) @db.Date createdAt DateTime? @default(now()) @db.Date
updatedAt DateTime? @updatedAt @db.Date updatedAt DateTime? @updatedAt @db.Date
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)

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, ConfigService, HttpServer, WaBusiness } from '../../config/env.config'; import { Auth, Chatwoot, ConfigService, HttpServer, 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';
@ -494,6 +494,9 @@ export class InstanceController {
return result; return result;
} }
if (!this.configService.get<Chatwoot>('CHATWOOT').ENABLED)
throw new BadRequestException('Chatwoot is not enabled');
if (!chatwootAccountId) { if (!chatwootAccountId) {
throw new BadRequestException('accountId is required'); throw new BadRequestException('accountId is required');
} }
@ -658,7 +661,7 @@ export class InstanceController {
switch (state) { switch (state) {
case 'open': case 'open':
this.logger.verbose('logging out instance: ' + instanceName); this.logger.verbose('logging out instance: ' + instanceName);
instance.clearCacheChatwoot(); if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) instance.clearCacheChatwoot();
await instance.reloadConnection(); await instance.reloadConnection();
await delay(2000); await delay(2000);
@ -749,7 +752,7 @@ export class InstanceController {
try { try {
const waInstances = this.waMonitor.waInstances[instanceName]; const waInstances = this.waMonitor.waInstances[instanceName];
waInstances?.removeRabbitmqQueues(); waInstances?.removeRabbitmqQueues();
waInstances?.clearCacheChatwoot(); if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) waInstances?.clearCacheChatwoot();
if (instance.state === 'connecting') { if (instance.state === 'connecting') {
this.logger.verbose('logging out instance: ' + instanceName); this.logger.verbose('logging out instance: ' + instanceName);

View File

@ -1,7 +1,7 @@
import { isURL } from 'class-validator'; import { isURL } from 'class-validator';
import { CacheEngine } from '../../../../cache/cacheengine'; import { CacheEngine } from '../../../../cache/cacheengine';
import { ConfigService, HttpServer } from '../../../../config/env.config'; import { Chatwoot, ConfigService, HttpServer } from '../../../../config/env.config';
import { Logger } from '../../../../config/logger.config'; import { Logger } from '../../../../config/logger.config';
import { BadRequestException } from '../../../../exceptions'; import { BadRequestException } from '../../../../exceptions';
import { InstanceDto } from '../../../dto/instance.dto'; import { InstanceDto } from '../../../dto/instance.dto';
@ -21,6 +21,8 @@ export class ChatwootController {
) {} ) {}
public async createChatwoot(instance: InstanceDto, data: ChatwootDto) { public async createChatwoot(instance: InstanceDto, data: ChatwootDto) {
if (!this.configService.get<Chatwoot>('CHATWOOT').ENABLED) throw new BadRequestException('Chatwoot is disabled');
logger.verbose('requested createChatwoot from ' + instance.instanceName + ' instance'); logger.verbose('requested createChatwoot from ' + instance.instanceName + ' instance');
if (data.enabled) { if (data.enabled) {
@ -76,6 +78,8 @@ export class ChatwootController {
} }
public async findChatwoot(instance: InstanceDto) { public async findChatwoot(instance: InstanceDto) {
if (!this.configService.get<Chatwoot>('CHATWOOT').ENABLED) throw new BadRequestException('Chatwoot is disabled');
logger.verbose('requested findChatwoot from ' + instance.instanceName + ' instance'); logger.verbose('requested findChatwoot from ' + instance.instanceName + ' instance');
const result = await this.chatwootService.find(instance); const result = await this.chatwootService.find(instance);
@ -102,6 +106,8 @@ export class ChatwootController {
} }
public async receiveWebhook(instance: InstanceDto, data: any) { public async receiveWebhook(instance: InstanceDto, data: any) {
if (!this.configService.get<Chatwoot>('CHATWOOT').ENABLED) throw new BadRequestException('Chatwoot is disabled');
logger.verbose('requested receiveWebhook from ' + instance.instanceName + ' instance'); logger.verbose('requested receiveWebhook from ' + instance.instanceName + ' instance');
const chatwootCache = new CacheService(new CacheEngine(this.configService, ChatwootService.name).getEngine()); const chatwootCache = new CacheService(new CacheEngine(this.configService, ChatwootService.name).getEngine());

View File

@ -1,5 +1,5 @@
import { CacheEngine } from '../cache/cacheengine'; import { CacheEngine } from '../cache/cacheengine';
import { configService } from '../config/env.config'; import { Chatwoot, configService } from '../config/env.config';
import { eventEmitter } from '../config/event.config'; import { eventEmitter } from '../config/event.config';
import { Logger } from '../config/logger.config'; import { Logger } from '../config/logger.config';
import { ChatController } from './controllers/chat.controller'; import { ChatController } from './controllers/chat.controller';
@ -32,8 +32,12 @@ import { WebhookService } from './services/webhook.service';
const logger = new Logger('WA MODULE'); const logger = new Logger('WA MODULE');
let chatwootCache: CacheService = null;
if (configService.get<Chatwoot>('CHATWOOT').ENABLED) {
chatwootCache = new CacheService(new CacheEngine(configService, ChatwootService.name).getEngine());
}
export const cache = new CacheService(new CacheEngine(configService, 'instance').getEngine()); export const cache = new CacheService(new CacheEngine(configService, 'instance').getEngine());
const chatwootCache = new CacheService(new CacheEngine(configService, ChatwootService.name).getEngine());
const baileysCache = new CacheService(new CacheEngine(configService, 'baileys').getEngine()); const baileysCache = new CacheService(new CacheEngine(configService, 'baileys').getEngine());
const providerFiles = new ProviderFiles(configService); const providerFiles = new ProviderFiles(configService);

View File

@ -8,6 +8,7 @@ import { v4 } from 'uuid';
import { import {
Auth, Auth,
Chatwoot,
CleanStoreConf, CleanStoreConf,
ConfigService, ConfigService,
Database, Database,
@ -91,7 +92,7 @@ export class ChannelStartupService {
status: 'created', status: 'created',
}); });
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp( this.chatwootService.eventWhatsapp(
Events.STATUS_INSTANCE, Events.STATUS_INSTANCE,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -358,6 +359,10 @@ export class ChannelStartupService {
} }
public async loadChatwoot() { public async loadChatwoot() {
if (!this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
return;
}
this.logger.verbose('Loading chatwoot'); this.logger.verbose('Loading chatwoot');
const data = await this.prismaRepository.chatwoot.findUnique({ const data = await this.prismaRepository.chatwoot.findUnique({
where: { where: {
@ -411,6 +416,11 @@ export class ChannelStartupService {
} }
public async setChatwoot(data: ChatwootDto) { public async setChatwoot(data: ChatwootDto) {
if (!this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
this.logger.verbose('Chatwoot is not enabled');
return;
}
this.logger.verbose('Setting chatwoot'); this.logger.verbose('Setting chatwoot');
await this.prismaRepository.chatwoot.create({ await this.prismaRepository.chatwoot.create({
data: { data: {
@ -452,6 +462,11 @@ export class ChannelStartupService {
} }
public async findChatwoot() { public async findChatwoot() {
if (!this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
this.logger.verbose('Chatwoot is not enabled');
return null;
}
this.logger.verbose('Finding chatwoot'); this.logger.verbose('Finding chatwoot');
const data = await this.prismaRepository.chatwoot.findUnique({ const data = await this.prismaRepository.chatwoot.findUnique({
where: { where: {
@ -781,6 +796,7 @@ export class ChannelStartupService {
this.localProxy.proxy = { this.localProxy.proxy = {
host: data?.host, host: data?.host,
port: `${data?.port}`, port: `${data?.port}`,
protocol: data?.protocol,
username: data?.username, username: data?.username,
password: data?.password, password: data?.password,
}; };
@ -797,6 +813,7 @@ export class ChannelStartupService {
enabled: data.enabled, enabled: data.enabled,
host: data.host, host: data.host,
port: data.port, port: data.port,
protocol: data.protocol,
username: data.username, username: data.username,
password: data.password, password: data.password,
instanceId: this.instanceId, instanceId: this.instanceId,

View File

@ -56,6 +56,7 @@ import sharp from 'sharp';
import { CacheEngine } from '../../../cache/cacheengine'; import { CacheEngine } from '../../../cache/cacheengine';
import { import {
CacheConf, CacheConf,
Chatwoot,
ConfigService, ConfigService,
configService, configService,
ConfigSessionPhone, ConfigSessionPhone,
@ -283,7 +284,7 @@ export class BaileysStartupService extends ChannelStartupService {
statusCode: DisconnectReason.badSession, statusCode: DisconnectReason.badSession,
}); });
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp( this.chatwootService.eventWhatsapp(
Events.QRCODE_UPDATED, Events.QRCODE_UPDATED,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -346,7 +347,7 @@ export class BaileysStartupService extends ChannelStartupService {
}, },
}); });
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp( this.chatwootService.eventWhatsapp(
Events.QRCODE_UPDATED, Events.QRCODE_UPDATED,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -399,7 +400,7 @@ export class BaileysStartupService extends ChannelStartupService {
status: 'closed', status: 'closed',
}); });
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp( this.chatwootService.eventWhatsapp(
Events.STATUS_INSTANCE, Events.STATUS_INSTANCE,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -437,7 +438,7 @@ export class BaileysStartupService extends ChannelStartupService {
`, `,
); );
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp( this.chatwootService.eventWhatsapp(
Events.CONNECTION_UPDATE, Events.CONNECTION_UPDATE,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -854,7 +855,12 @@ export class BaileysStartupService extends ChannelStartupService {
}); });
} }
if (this.localChatwoot.enabled && this.localChatwoot.importContacts && contactsRaw.length) { if (
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
this.localChatwoot.enabled &&
this.localChatwoot.importContacts &&
contactsRaw.length
) {
this.chatwootService.addHistoryContacts({ instanceName: this.instance.name }, contactsRaw); this.chatwootService.addHistoryContacts({ instanceName: this.instance.name }, contactsRaw);
chatwootImport.importHistoryContacts({ instanceName: this.instance.name }, this.localChatwoot); chatwootImport.importHistoryContacts({ instanceName: this.instance.name }, this.localChatwoot);
} }
@ -931,7 +937,10 @@ export class BaileysStartupService extends ChannelStartupService {
const instance: InstanceDto = { instanceName: this.instance.name }; const instance: InstanceDto = { instanceName: this.instance.name };
const daysLimitToImport = this.localChatwoot.enabled ? this.localChatwoot.daysLimitImportMessages : 1000; const daysLimitToImport =
this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled
? this.localChatwoot.daysLimitImportMessages
: 1000;
this.logger.verbose(`Param days limit import messages is: ${daysLimitToImport}`); this.logger.verbose(`Param days limit import messages is: ${daysLimitToImport}`);
const date = new Date(); const date = new Date();
@ -977,6 +986,7 @@ export class BaileysStartupService extends ChannelStartupService {
}); });
const messagesRaw: any[] = []; const messagesRaw: any[] = [];
const messagesRepository = new Set( const messagesRepository = new Set(
chatwootImport.getRepositoryMessagesCache(instance) ?? chatwootImport.getRepositoryMessagesCache(instance) ??
( (
@ -993,7 +1003,10 @@ export class BaileysStartupService extends ChannelStartupService {
}), }),
); );
if (chatwootImport.getRepositoryMessagesCache(instance) === null) { if (
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
chatwootImport.getRepositoryMessagesCache(instance) === null
) {
chatwootImport.setRepositoryMessagesCache(instance, messagesRepository); chatwootImport.setRepositoryMessagesCache(instance, messagesRepository);
} }
@ -1044,7 +1057,12 @@ export class BaileysStartupService extends ChannelStartupService {
skipDuplicates: true, skipDuplicates: true,
}); });
if (this.localChatwoot.enabled && this.localChatwoot.importMessages && messagesRaw.length > 0) { if (
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
this.localChatwoot.enabled &&
this.localChatwoot.importMessages &&
messagesRaw.length > 0
) {
this.chatwootService.addHistoryMessages( this.chatwootService.addHistoryMessages(
instance, instance,
messagesRaw.filter((msg) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid)), messagesRaw.filter((msg) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid)),
@ -1082,6 +1100,7 @@ export class BaileysStartupService extends ChannelStartupService {
this.logger.verbose('Event received: messages.upsert'); this.logger.verbose('Event received: messages.upsert');
for (const received of messages) { for (const received of messages) {
if ( if (
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
this.localChatwoot.enabled && this.localChatwoot.enabled &&
(received.message?.protocolMessage?.editedMessage || received.message?.editedMessage?.message) (received.message?.protocolMessage?.editedMessage || received.message?.editedMessage?.message)
) { ) {
@ -1186,7 +1205,11 @@ export class BaileysStartupService extends ChannelStartupService {
this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT'); this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw); this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
if (this.localChatwoot.enabled && !received.key.id.includes('@broadcast')) { if (
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
this.localChatwoot.enabled &&
!received.key.id.includes('@broadcast')
) {
const chatwootSentMessage = await this.chatwootService.eventWhatsapp( const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
Events.MESSAGES_UPSERT, Events.MESSAGES_UPSERT,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -1251,7 +1274,7 @@ export class BaileysStartupService extends ChannelStartupService {
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE'); this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw); this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
await this.chatwootService.eventWhatsapp( await this.chatwootService.eventWhatsapp(
Events.CONTACTS_UPDATE, Events.CONTACTS_UPDATE,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -1299,7 +1322,7 @@ export class BaileysStartupService extends ChannelStartupService {
} }
if (status[update.status] === 'READ' && key.fromMe) { if (status[update.status] === 'READ' && key.fromMe) {
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp('messages.read', { instanceName: this.instance.name }, { key: key }); this.chatwootService.eventWhatsapp('messages.read', { instanceName: this.instance.name }, { key: key });
} }
} }
@ -1365,7 +1388,7 @@ export class BaileysStartupService extends ChannelStartupService {
data: message, data: message,
}); });
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp( this.chatwootService.eventWhatsapp(
Events.MESSAGES_DELETE, Events.MESSAGES_DELETE,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -1653,6 +1676,7 @@ export class BaileysStartupService extends ChannelStartupService {
const instance: InstanceDto = { instanceName: this.instance.name }; const instance: InstanceDto = { instanceName: this.instance.name };
if ( if (
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
this.localChatwoot.enabled && this.localChatwoot.enabled &&
this.localChatwoot.importMessages && this.localChatwoot.importMessages &&
this.isSyncNotificationFromUsedSyncType(msg) this.isSyncNotificationFromUsedSyncType(msg)
@ -1789,7 +1813,7 @@ export class BaileysStartupService extends ChannelStartupService {
this.logger.verbose(`Exists: "${isWA.exists}" | jid: ${isWA.jid}`); this.logger.verbose(`Exists: "${isWA.exists}" | jid: ${isWA.jid}`);
if (!isWA.exists && !isJidGroup(isWA.jid) && !isWA.jid.includes('@broadcast')) { if (!isWA.exists && !isJidGroup(isWA.jid) && !isWA.jid.includes('@broadcast')) {
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
const body = { const body = {
key: { remoteJid: isWA.jid }, key: { remoteJid: isWA.jid },
}; };
@ -2012,7 +2036,7 @@ export class BaileysStartupService extends ChannelStartupService {
this.logger.verbose('Sending data to webhook in event SEND_MESSAGE'); this.logger.verbose('Sending data to webhook in event SEND_MESSAGE');
this.sendDataWebhook(Events.SEND_MESSAGE, messageRaw); this.sendDataWebhook(Events.SEND_MESSAGE, messageRaw);
if (this.localChatwoot.enabled && !isChatwoot) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled && !isChatwoot) {
this.chatwootService.eventWhatsapp(Events.SEND_MESSAGE, { instanceName: this.instance.name }, messageRaw); this.chatwootService.eventWhatsapp(Events.SEND_MESSAGE, { instanceName: this.instance.name }, messageRaw);
} }

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 { ConfigService, Database, WaBusiness } from '../../../config/env.config'; import { Chatwoot, ConfigService, Database, 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 {
@ -409,7 +409,7 @@ export class BusinessStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw); this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
const chatwootSentMessage = await this.chatwootService.eventWhatsapp( const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
Events.MESSAGES_UPSERT, Events.MESSAGES_UPSERT,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -474,7 +474,7 @@ export class BusinessStartupService extends ChannelStartupService {
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE'); this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw); this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
await this.chatwootService.eventWhatsapp( await this.chatwootService.eventWhatsapp(
Events.CONTACTS_UPDATE, Events.CONTACTS_UPDATE,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -551,7 +551,7 @@ export class BusinessStartupService extends ChannelStartupService {
data: message, data: message,
}); });
if (this.localChatwoot.enabled) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp( this.chatwootService.eventWhatsapp(
Events.MESSAGES_DELETE, Events.MESSAGES_DELETE,
{ instanceName: this.instance.name }, { instanceName: this.instance.name },
@ -881,7 +881,7 @@ export class BusinessStartupService extends ChannelStartupService {
this.logger.verbose('Sending data to webhook in event SEND_MESSAGE'); this.logger.verbose('Sending data to webhook in event SEND_MESSAGE');
this.sendDataWebhook(Events.SEND_MESSAGE, messageRaw); this.sendDataWebhook(Events.SEND_MESSAGE, messageRaw);
if (this.localChatwoot.enabled && !isChatwoot) { if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled && !isChatwoot) {
this.chatwootService.eventWhatsapp(Events.SEND_MESSAGE, { instanceName: this.instance.name }, messageRaw); this.chatwootService.eventWhatsapp(Events.SEND_MESSAGE, { instanceName: this.instance.name }, messageRaw);
} }

View File

@ -6,6 +6,7 @@ import { join } from 'path';
import { import {
Auth, Auth,
CacheConf, CacheConf,
Chatwoot,
ConfigService, ConfigService,
Database, Database,
DelInstance, DelInstance,
@ -86,9 +87,9 @@ export class WAMonitoringService {
if (value) { if (value) {
this.logger.verbose('get instance info: ' + key); this.logger.verbose('get instance info: ' + key);
let chatwoot: any; let chatwoot: any;
const urlServer = this.configService.get<HttpServer>('SERVER').URL; const urlServer = this.configService.get<HttpServer>('SERVER').URL;
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
const findChatwoot = await this.waInstances[key].findChatwoot(); const findChatwoot = await this.waInstances[key].findChatwoot();
if (findChatwoot && findChatwoot.enabled) { if (findChatwoot && findChatwoot.enabled) {
@ -97,6 +98,7 @@ export class WAMonitoringService {
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`, webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
}; };
} }
}
const findIntegration = await this.waInstances[key].findIntegration(); const findIntegration = await this.waInstances[key].findIntegration();
@ -132,7 +134,7 @@ export class WAMonitoringService {
}) })
)?.apikey; )?.apikey;
instanceData.instance['chatwoot'] = chatwoot; if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot;
instanceData.instance['integration'] = integration; instanceData.instance['integration'] = integration;
} }
@ -158,7 +160,7 @@ export class WAMonitoringService {
}) })
)?.apikey; )?.apikey;
instanceData.instance['chatwoot'] = chatwoot; if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot;
instanceData.instance['integration'] = integration; instanceData.instance['integration'] = integration;
} }
@ -459,7 +461,7 @@ export class WAMonitoringService {
this.eventEmitter.on('logout.instance', async (instanceName: string) => { this.eventEmitter.on('logout.instance', async (instanceName: string) => {
this.logger.verbose('logout instance: ' + instanceName); this.logger.verbose('logout instance: ' + instanceName);
try { try {
this.waInstances[instanceName]?.clearCacheChatwoot(); if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) this.waInstances[instanceName]?.clearCacheChatwoot();
this.logger.verbose('request cleaning up instance: ' + instanceName); this.logger.verbose('request cleaning up instance: ' + instanceName);
this.cleaningUp(instanceName); this.cleaningUp(instanceName);
} finally { } finally {

View File

@ -190,8 +190,9 @@ export type SslConf = { PRIVKEY: string; FULLCHAIN: string };
export type Webhook = { GLOBAL?: GlobalWebhook; EVENTS: EventsWebhook }; export type Webhook = { GLOBAL?: GlobalWebhook; EVENTS: EventsWebhook };
export type ConfigSessionPhone = { CLIENT: string; NAME: string; VERSION: string }; export type ConfigSessionPhone = { CLIENT: string; NAME: string; VERSION: string };
export type QrCode = { LIMIT: number; COLOR: string }; export type QrCode = { LIMIT: number; COLOR: string };
export type Typebot = { API_VERSION: string; KEEP_OPEN: boolean }; export type Typebot = { ENABLED: boolean; API_VERSION: string; KEEP_OPEN: boolean };
export type Chatwoot = { export type Chatwoot = {
ENABLED: boolean;
MESSAGE_DELETE: boolean; MESSAGE_DELETE: boolean;
MESSAGE_READ: boolean; MESSAGE_READ: boolean;
IMPORT: { IMPORT: {
@ -430,10 +431,12 @@ export class ConfigService {
COLOR: process.env.QRCODE_COLOR || '#198754', COLOR: process.env.QRCODE_COLOR || '#198754',
}, },
TYPEBOT: { TYPEBOT: {
ENABLED: process.env?.TYPEBOT_ENABLED === 'true',
API_VERSION: process.env?.TYPEBOT_API_VERSION || 'old', API_VERSION: process.env?.TYPEBOT_API_VERSION || 'old',
KEEP_OPEN: process.env.TYPEBOT_KEEP_OPEN === 'true', KEEP_OPEN: process.env.TYPEBOT_KEEP_OPEN === 'true',
}, },
CHATWOOT: { CHATWOOT: {
ENABLED: process.env?.CHATWOOT_ENABLED === 'true',
MESSAGE_DELETE: process.env.CHATWOOT_MESSAGE_DELETE === 'false', MESSAGE_DELETE: process.env.CHATWOOT_MESSAGE_DELETE === 'false',
MESSAGE_READ: process.env.CHATWOOT_MESSAGE_READ === 'false', MESSAGE_READ: process.env.CHATWOOT_MESSAGE_READ === 'false',
IMPORT: { IMPORT: {