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

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

View File

@@ -1,7 +1,7 @@
import { isURL } from 'class-validator';
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 { BadRequestException } from '../../../../exceptions';
import { InstanceDto } from '../../../dto/instance.dto';
@@ -21,6 +21,8 @@ export class ChatwootController {
) {}
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');
if (data.enabled) {
@@ -76,6 +78,8 @@ export class ChatwootController {
}
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');
const result = await this.chatwootService.find(instance);
@@ -102,6 +106,8 @@ export class ChatwootController {
}
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');
const chatwootCache = new CacheService(new CacheEngine(this.configService, ChatwootService.name).getEngine());

View File

@@ -1,5 +1,5 @@
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 { Logger } from '../config/logger.config';
import { ChatController } from './controllers/chat.controller';
@@ -32,8 +32,12 @@ import { WebhookService } from './services/webhook.service';
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());
const chatwootCache = new CacheService(new CacheEngine(configService, ChatwootService.name).getEngine());
const baileysCache = new CacheService(new CacheEngine(configService, 'baileys').getEngine());
const providerFiles = new ProviderFiles(configService);

View File

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

View File

@@ -56,6 +56,7 @@ import sharp from 'sharp';
import { CacheEngine } from '../../../cache/cacheengine';
import {
CacheConf,
Chatwoot,
ConfigService,
configService,
ConfigSessionPhone,
@@ -283,7 +284,7 @@ export class BaileysStartupService extends ChannelStartupService {
statusCode: DisconnectReason.badSession,
});
if (this.localChatwoot.enabled) {
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp(
Events.QRCODE_UPDATED,
{ 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(
Events.QRCODE_UPDATED,
{ instanceName: this.instance.name },
@@ -399,7 +400,7 @@ export class BaileysStartupService extends ChannelStartupService {
status: 'closed',
});
if (this.localChatwoot.enabled) {
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp(
Events.STATUS_INSTANCE,
{ 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(
Events.CONNECTION_UPDATE,
{ 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);
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 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}`);
const date = new Date();
@@ -977,6 +986,7 @@ export class BaileysStartupService extends ChannelStartupService {
});
const messagesRaw: any[] = [];
const messagesRepository = new Set(
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);
}
@@ -1044,7 +1057,12 @@ export class BaileysStartupService extends ChannelStartupService {
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(
instance,
messagesRaw.filter((msg) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid)),
@@ -1082,6 +1100,7 @@ export class BaileysStartupService extends ChannelStartupService {
this.logger.verbose('Event received: messages.upsert');
for (const received of messages) {
if (
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
this.localChatwoot.enabled &&
(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.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(
Events.MESSAGES_UPSERT,
{ 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.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
if (this.localChatwoot.enabled) {
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
await this.chatwootService.eventWhatsapp(
Events.CONTACTS_UPDATE,
{ instanceName: this.instance.name },
@@ -1299,7 +1322,7 @@ export class BaileysStartupService extends ChannelStartupService {
}
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 });
}
}
@@ -1365,7 +1388,7 @@ export class BaileysStartupService extends ChannelStartupService {
data: message,
});
if (this.localChatwoot.enabled) {
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp(
Events.MESSAGES_DELETE,
{ instanceName: this.instance.name },
@@ -1653,6 +1676,7 @@ export class BaileysStartupService extends ChannelStartupService {
const instance: InstanceDto = { instanceName: this.instance.name };
if (
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
this.localChatwoot.enabled &&
this.localChatwoot.importMessages &&
this.isSyncNotificationFromUsedSyncType(msg)
@@ -1789,7 +1813,7 @@ export class BaileysStartupService extends ChannelStartupService {
this.logger.verbose(`Exists: "${isWA.exists}" | jid: ${isWA.jid}`);
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 = {
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.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);
}

View File

@@ -5,7 +5,7 @@ import FormData from 'form-data';
import fs from 'fs/promises';
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 { NumberBusiness } from '../../dto/chat.dto';
import {
@@ -409,7 +409,7 @@ export class BusinessStartupService extends ChannelStartupService {
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(
Events.MESSAGES_UPSERT,
{ 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.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
if (this.localChatwoot.enabled) {
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
await this.chatwootService.eventWhatsapp(
Events.CONTACTS_UPDATE,
{ instanceName: this.instance.name },
@@ -551,7 +551,7 @@ export class BusinessStartupService extends ChannelStartupService {
data: message,
});
if (this.localChatwoot.enabled) {
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
this.chatwootService.eventWhatsapp(
Events.MESSAGES_DELETE,
{ 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.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);
}

View File

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