fix: For compatibility reasons, container mode has been removed

This commit is contained in:
Davidson Gomes 2023-07-21 13:12:55 -03:00
parent 091b920a22
commit 19039aa281
8 changed files with 121 additions and 418 deletions

View File

@ -7,6 +7,7 @@
* Adjustments to search endpoint for contacts, chats, messages and Status messages
* Now when deleting the instance, the data referring to it in mongodb is also deleted
* It is now validated if the instance name contains uppercase and special characters
* For compatibility reasons, container mode has been removed
# 1.3.1 (2023-07-20 07:48)

View File

@ -97,13 +97,3 @@ AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true
# seconds - 3600s ===1h | zero (0) - never expires
AUTHENTICATION_JWT_EXPIRIN_IN=0
AUTHENTICATION_JWT_SECRET='L0YWtjb2w554WFqPG'
# Set the instance name and webhook url to create an instance in init the application
# With this option activated, you work with a url per webhook event, respecting the local url and the name of each event
# container or server
AUTHENTICATION_INSTANCE_MODE=server
# if you are using container mode, set the container name and the webhook url to default instance
AUTHENTICATION_INSTANCE_NAME=evolution
AUTHENTICATION_INSTANCE_WEBHOOK_URL=''
AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID=1
AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN=123456
AUTHENTICATION_INSTANCE_CHATWOOT_URL=''

View File

@ -94,20 +94,12 @@ export type EventsWebhook = {
export type ApiKey = { KEY: string };
export type Jwt = { EXPIRIN_IN: number; SECRET: string };
export type Instance = {
NAME: string;
WEBHOOK_URL: string;
MODE: string;
CHATWOOT_ACCOUNT_ID: string;
CHATWOOT_TOKEN: string;
CHATWOOT_URL: string;
};
export type Auth = {
API_KEY: ApiKey;
EXPOSE_IN_FETCH_INSTANCES: boolean;
JWT: Jwt;
TYPE: 'jwt' | 'apikey';
INSTANCE: Instance;
};
export type DelInstance = number | boolean;
@ -276,15 +268,6 @@ export class ConfigService {
: 3600,
SECRET: process.env.AUTHENTICATION_JWT_SECRET,
},
INSTANCE: {
NAME: process.env.AUTHENTICATION_INSTANCE_NAME,
WEBHOOK_URL: process.env.AUTHENTICATION_INSTANCE_WEBHOOK_URL,
MODE: process.env.AUTHENTICATION_INSTANCE_MODE,
CHATWOOT_ACCOUNT_ID:
process.env.AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID || '',
CHATWOOT_TOKEN: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN || '',
CHATWOOT_URL: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_URL || '',
},
},
};
}

View File

@ -136,14 +136,4 @@ AUTHENTICATION:
# Set the secret key to encrypt and decrypt your token and its expiration time.
JWT:
EXPIRIN_IN: 0 # seconds - 3600s === 1h | zero (0) - never expires
SECRET: L=0YWt]b2w[WF>#>:&E`
# Set the instance name and webhook url to create an instance in init the application
INSTANCE:
# With this option activated, you work with a url per webhook event, respecting the local url and the name of each event
MODE: server # container or server
# if you are using container mode, set the container name and the webhook url to default instance
NAME: evolution
WEBHOOK_URL: <url>
CHATWOOT_ACCOUNT_ID: 1
CHATWOOT_TOKEN: 123456
CHATWOOT_URL: <url>
SECRET: L=0YWt]b2w[WF>#>:&E`

View File

@ -48,273 +48,80 @@ export class InstanceController {
);
}
const mode = this.configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
this.logger.verbose('checking duplicate token');
await this.authService.checkDuplicateToken(token);
if (mode === 'container') {
this.logger.verbose('container mode');
this.logger.verbose('creating instance');
const instance = new WAStartupService(
this.configService,
this.eventEmitter,
this.repository,
this.cache,
);
instance.instanceName = instanceName
.toLowerCase()
.replace(/[^a-z0-9]/g, '')
.replace(' ', '');
if (Object.keys(this.waMonitor.waInstances).length > 0) {
throw new BadRequestException([
'Instance already created',
'Only one instance can be created',
]);
this.logger.verbose('instance: ' + instance.instanceName + ' created');
this.waMonitor.waInstances[instance.instanceName] = instance;
this.waMonitor.delInstanceTime(instance.instanceName);
this.logger.verbose('generating hash');
const hash = await this.authService.generateHash(
{
instanceName: instance.instanceName,
},
token,
);
this.logger.verbose('hash: ' + hash + ' generated');
let getEvents: string[];
if (webhook) {
if (!isURL(webhook, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in webhook');
}
this.logger.verbose('checking duplicate token');
await this.authService.checkDuplicateToken(token);
this.logger.verbose('creating instance');
const instance = new WAStartupService(
this.configService,
this.eventEmitter,
this.repository,
this.cache,
);
instance.instanceName = instanceName
.toLowerCase()
.replace(/[^a-z0-9]/g, '')
.replace(' ', '');
this.logger.verbose('instance: ' + instance.instanceName + ' created');
this.waMonitor.waInstances[instance.instanceName] = instance;
this.waMonitor.delInstanceTime(instance.instanceName);
this.logger.verbose('generating hash');
const hash = await this.authService.generateHash(
{
instanceName: instance.instanceName,
},
token,
);
this.logger.verbose('hash: ' + hash + ' generated');
let getEvents: string[];
if (webhook) {
if (!isURL(webhook, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in webhook');
}
this.logger.verbose('creating webhook');
try {
this.webhookService.create(instance, {
enabled: true,
url: webhook,
events,
webhook_by_events,
});
getEvents = (await this.webhookService.find(instance)).events;
} catch (error) {
this.logger.log(error);
}
}
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
this.logger.verbose('instance created');
this.logger.verbose({
instance: {
instanceName: instance.instanceName,
status: 'created',
},
hash,
webhook,
events: getEvents,
});
return {
instance: {
instanceName: instance.instanceName,
status: 'created',
},
hash,
webhook,
events: getEvents,
};
}
if (!chatwoot_account_id) {
throw new BadRequestException('account_id is required');
}
if (!chatwoot_token) {
throw new BadRequestException('token is required');
}
if (!chatwoot_url) {
throw new BadRequestException('url is required');
}
if (!isURL(chatwoot_url, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in chatwoot');
}
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
this.logger.verbose('creating webhook');
try {
this.chatwootService.create(instance, {
this.webhookService.create(instance, {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
name_inbox: instance.instanceName,
url: webhook,
events,
webhook_by_events,
});
this.chatwootService.initInstanceChatwoot(
instance,
instance.instanceName,
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
qrcode,
);
getEvents = (await this.webhookService.find(instance)).events;
} catch (error) {
this.logger.log(error);
}
}
return {
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
let getQrcode: wa.QrCode;
if (qrcode) {
this.logger.verbose('creating qrcode');
await instance.connectToWhatsapp();
await delay(2000);
getQrcode = instance.qrCode;
}
this.logger.verbose('instance created');
this.logger.verbose({
instance: {
instanceName: instance.instanceName,
status: 'created',
},
hash,
chatwoot: {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
name_inbox: instance.instanceName,
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
},
};
} else {
this.logger.verbose('server mode');
this.logger.verbose('checking duplicate token');
await this.authService.checkDuplicateToken(token);
this.logger.verbose('creating instance');
const instance = new WAStartupService(
this.configService,
this.eventEmitter,
this.repository,
this.cache,
);
instance.instanceName = instanceName
.toLowerCase()
.replace(/[^a-z0-9]/g, '')
.replace(' ', '');
this.logger.verbose('instance: ' + instance.instanceName + ' created');
this.waMonitor.waInstances[instance.instanceName] = instance;
this.waMonitor.delInstanceTime(instance.instanceName);
this.logger.verbose('generating hash');
const hash = await this.authService.generateHash(
{
instanceName: instance.instanceName,
},
token,
);
this.logger.verbose('hash: ' + hash + ' generated');
let getEvents: string[];
if (webhook) {
if (!isURL(webhook, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in webhook');
}
this.logger.verbose('creating webhook');
try {
this.webhookService.create(instance, {
enabled: true,
url: webhook,
events,
webhook_by_events,
});
getEvents = (await this.webhookService.find(instance)).events;
} catch (error) {
this.logger.log(error);
}
}
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
let getQrcode: wa.QrCode;
if (qrcode) {
this.logger.verbose('creating qrcode');
await instance.connectToWhatsapp();
await delay(2000);
getQrcode = instance.qrCode;
}
this.logger.verbose('instance created');
this.logger.verbose({
instance: {
instanceName: instance.instanceName,
status: 'created',
},
hash,
webhook,
webhook_by_events,
events: getEvents,
qrcode: getQrcode,
});
return {
instance: {
instanceName: instance.instanceName,
status: 'created',
},
hash,
webhook,
webhook_by_events,
events: getEvents,
qrcode: getQrcode,
};
}
if (!chatwoot_account_id) {
throw new BadRequestException('account_id is required');
}
if (!chatwoot_token) {
throw new BadRequestException('token is required');
}
if (!chatwoot_url) {
throw new BadRequestException('url is required');
}
if (!isURL(chatwoot_url, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in chatwoot');
}
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
try {
this.chatwootService.create(instance, {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
name_inbox: instance.instanceName,
});
this.chatwootService.initInstanceChatwoot(
instance,
instance.instanceName,
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
qrcode,
);
} catch (error) {
this.logger.log(error);
}
webhook,
webhook_by_events,
events: getEvents,
qrcode: getQrcode,
});
return {
instance: {
@ -325,17 +132,67 @@ export class InstanceController {
webhook,
webhook_by_events,
events: getEvents,
chatwoot: {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
name_inbox: instance.instanceName,
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
},
qrcode: getQrcode,
};
}
if (!chatwoot_account_id) {
throw new BadRequestException('account_id is required');
}
if (!chatwoot_token) {
throw new BadRequestException('token is required');
}
if (!chatwoot_url) {
throw new BadRequestException('url is required');
}
if (!isURL(chatwoot_url, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in chatwoot');
}
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
try {
this.chatwootService.create(instance, {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
name_inbox: instance.instanceName,
});
this.chatwootService.initInstanceChatwoot(
instance,
instance.instanceName,
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
qrcode,
);
} catch (error) {
this.logger.log(error);
}
return {
instance: {
instanceName: instance.instanceName,
status: 'created',
},
hash,
webhook,
webhook_by_events,
events: getEvents,
chatwoot: {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
name_inbox: instance.instanceName,
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
},
};
}
public async connectToWhatsapp({ instanceName }: InstanceDto) {

View File

@ -15,7 +15,6 @@ import {
import { RepositoryBroker } from '../repository/repository.manager';
import { NotFoundException } from '../../exceptions';
import { Db } from 'mongodb';
import { initInstance } from '../whatsapp.module';
import { RedisCache } from '../../db/redis.client';
import { execSync } from 'child_process';
import { dbserver } from '../../db/db.connect';
@ -287,7 +286,6 @@ export class WAMonitoringService {
keys.forEach(async (k) => await set(k.split(':')[1]));
} else {
this.logger.verbose('no instance keys found');
initInstance();
}
return;
}
@ -303,7 +301,6 @@ export class WAMonitoringService {
);
} else {
this.logger.verbose('no collections found');
initInstance();
}
return;
}
@ -324,7 +321,6 @@ export class WAMonitoringService {
await set(dirent.name);
} else {
this.logger.verbose('no instance files found');
initInstance();
}
}
} catch (error) {

View File

@ -346,7 +346,6 @@ export class WAStartupService {
const serverUrl = this.configService.get<HttpServer>('SERVER').URL;
const we = event.replace(/[\.-]/gm, '_').toUpperCase();
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
const instance = this.configService.get<Auth>('AUTHENTICATION').INSTANCE;
const expose =
this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES;
@ -355,7 +354,7 @@ export class WAStartupService {
const globalApiKey = this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
if (local && instance.MODE !== 'container') {
if (local) {
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
this.logger.verbose('Sending data to webhook local');
let baseURL;
@ -432,13 +431,7 @@ export class WAStartupService {
globalURL = globalWebhook.URL;
}
let localUrl;
if (instance.MODE === 'container') {
localUrl = instance.WEBHOOK_URL;
} else {
localUrl = this.localWebhook.url;
}
const localUrl = this.localWebhook.url;
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
const logData = {

View File

@ -91,111 +91,4 @@ export const sendMessageController = new SendMessageController(waMonitor);
export const chatController = new ChatController(waMonitor);
export const groupController = new GroupController(waMonitor);
export async function initInstance() {
const instance = new WAStartupService(configService, eventEmitter, repository, cache);
const mode = configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
logger.verbose('Sending data webhook for event: ' + Events.APPLICATION_STARTUP);
instance.sendDataWebhook(
Events.APPLICATION_STARTUP,
{
message: 'Application startup',
mode,
},
false,
);
if (mode === 'container') {
logger.verbose('Application startup in container mode');
const instanceName = configService.get<Auth>('AUTHENTICATION').INSTANCE.NAME;
logger.verbose('Instance name: ' + instanceName);
const instanceWebhook =
configService.get<Auth>('AUTHENTICATION').INSTANCE.WEBHOOK_URL;
logger.verbose('Instance webhook: ' + instanceWebhook);
// const chatwootAccountId =
// configService.get<Auth>('AUTHENTICATION').INSTANCE.CHATWOOT_ACCOUNT_ID;
// logger.verbose('Chatwoot account id: ' + chatwootAccountId);
// const chatwootToken =
// configService.get<Auth>('AUTHENTICATION').INSTANCE.CHATWOOT_TOKEN;
// logger.verbose('Chatwoot token: ' + chatwootToken);
// const chatwootUrl = configService.get<Auth>('AUTHENTICATION').INSTANCE.CHATWOOT_URL;
// logger.verbose('Chatwoot url: ' + chatwootUrl);
instance.instanceName = instanceName;
waMonitor.waInstances[instance.instanceName] = instance;
waMonitor.delInstanceTime(instance.instanceName);
const hash = await authService.generateHash({
instanceName: instance.instanceName,
token: configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
});
logger.verbose('Hash generated: ' + hash);
if (instanceWebhook) {
logger.verbose('Creating webhook for instance: ' + instanceName);
try {
webhookService.create(instance, { enabled: true, url: instanceWebhook });
logger.verbose('Webhook created');
} catch (error) {
logger.log(error);
}
}
// if (chatwootUrl && chatwootToken && chatwootAccountId) {
// logger.verbose('Creating chatwoot for instance: ' + instanceName);
// try {
// chatwootService.create(instance, {
// enabled: true,
// url: chatwootUrl,
// token: chatwootToken,
// account_id: chatwootAccountId,
// sign_msg: false,
// });
// logger.verbose('Chatwoot created');
// } catch (error) {
// logger.log(error);
// }
// }
try {
const state = instance.connectionStatus?.state;
switch (state) {
case 'close':
await instance.connectToWhatsapp();
await delay(2000);
return instance.qrCode;
case 'connecting':
return instance.qrCode;
default:
return await this.connectionState({ instanceName });
}
} catch (error) {
logger.log(error);
}
const result = {
instance: {
instanceName: instance.instanceName,
status: 'created',
},
hash,
webhook: instanceWebhook,
};
logger.info(result);
return result;
}
return null;
}
logger.info('Module - ON');