From 04575d805188632a2703b0c47dd9d08e9b66affe Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Tue, 9 Apr 2024 06:44:59 -0300 Subject: [PATCH] adjusts in websocket --- Docker/.env.example | 1 + Dockerfile | 1 + src/config/env.config.ts | 2 + src/dev-env.yml | 1 + src/whatsapp/services/whatsapp.service.ts | 56 +++++++++++++++++------ 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Docker/.env.example b/Docker/.env.example index f55e3d65..865ef877 100644 --- a/Docker/.env.example +++ b/Docker/.env.example @@ -53,6 +53,7 @@ RABBITMQ_EXCHANGE_NAME=evolution_exchange RABBITMQ_URI=amqp://guest:guest@rabbitmq:5672 WEBSOCKET_ENABLED=false +WEBSOCKET_GLOBAL_EVENTS=false WA_BUSINESS_TOKEN_WEBHOOK=evolution WA_BUSINESS_URL=https://graph.facebook.com diff --git a/Dockerfile b/Dockerfile index 3f24af28..599f8cca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -68,6 +68,7 @@ ENV RABBITMQ_EXCHANGE_NAME=evolution_exchange ENV RABBITMQ_URI=amqp://guest:guest@rabbitmq:5672 ENV WEBSOCKET_ENABLED=false +ENV WEBSOCKET_GLOBAL_EVENTS=false ENV WA_BUSINESS_TOKEN_WEBHOOK=evolution ENV WA_BUSINESS_URL=https://graph.facebook.com diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 9ee778c1..b3991d08 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -86,6 +86,7 @@ export type Sqs = { export type Websocket = { ENABLED: boolean; + GLOBAL_EVENTS: boolean; }; export type WaBusiness = { @@ -299,6 +300,7 @@ export class ConfigService { }, WEBSOCKET: { ENABLED: process.env?.WEBSOCKET_ENABLED === 'true', + GLOBAL_EVENTS: process.env?.WEBSOCKET_GLOBAL_EVENTS === 'true', }, WA_BUSINESS: { TOKEN_WEBHOOK: process.env.WA_BUSINESS_TOKEN_WEBHOOK || '', diff --git a/src/dev-env.yml b/src/dev-env.yml index adc308e8..f791b721 100644 --- a/src/dev-env.yml +++ b/src/dev-env.yml @@ -97,6 +97,7 @@ SQS: WEBSOCKET: ENABLED: false + GLOBAL_EVENTS: false WA_BUSINESS: TOKEN_WEBHOOK: evolution diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 030285b8..7c50b043 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -828,28 +828,56 @@ export class WAStartupService { } } - if (this.configService.get('WEBSOCKET')?.ENABLED && this.localWebsocket.enabled) { + if (this.configService.get('WEBSOCKET')?.ENABLED) { this.logger.verbose('Sending data to websocket on channel: ' + this.instance.name); - if (Array.isArray(websocketLocal) && websocketLocal.includes(we)) { - this.logger.verbose('Sending data to websocket on event: ' + event); - const io = getIO(); + const io = getIO(); - const message = { - event, - instance: this.instance.name, - data, - server_url: serverUrl, - date_time: now, - sender: this.wuid, - }; + const message = { + event, + instance: this.instance.name, + data, + server_url: serverUrl, + date_time: now, + sender: this.wuid, + }; - if (expose && instanceApikey) { - message['apikey'] = instanceApikey; + if (expose && instanceApikey) { + message['apikey'] = instanceApikey; + } + + if (this.configService.get('WEBSOCKET')?.GLOBAL_EVENTS) { + io.emit(event, message); + + if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { + const logData = { + local: WAStartupService.name + '.sendData-WebsocketGlobal', + event, + instance: this.instance.name, + data, + server_url: serverUrl, + apikey: (expose && instanceApikey) || null, + date_time: now, + sender: this.wuid, + }; + + if (expose && instanceApikey) { + logData['apikey'] = instanceApikey; + } + + this.logger.log(logData); } + } + + if (this.localWebsocket.enabled && Array.isArray(websocketLocal) && websocketLocal.includes(we)) { + this.logger.verbose('Sending data to websocket on event: ' + event); this.logger.verbose('Sending data to socket.io in channel: ' + this.instance.name); io.of(`/${this.instance.name}`).emit(event, message); + if (this.configService.get('WEBSOCKET')?.GLOBAL_EVENTS) { + io.emit(event, message); + } + if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { const logData = { local: WAStartupService.name + '.sendData-Websocket',