From 6585e8f0c2aeae061265a2d84ec2c55a528e7a2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=AAnio=20An=C3=ADbal?= Date: Wed, 21 Aug 2024 14:55:18 -0300 Subject: [PATCH 1/4] Fix events structure to prevent "Instance not found" exception --- src/api/controllers/instance.controller.ts | 14 ++++++++------ .../rabbitmq/controllers/rabbitmq.controller.ts | 2 +- .../event/sqs/controllers/sqs.controller.ts | 2 +- .../webhook/controllers/webhook.controller.ts | 2 +- .../websocket/controllers/websocket.controller.ts | 2 +- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index 20846250..e596585c 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -52,6 +52,8 @@ export class InstanceController { const instanceId = v4(); + instanceData.instanceId = instanceId; + let hash: string; if (!instanceData.token) hash = v4().toUpperCase(); @@ -75,16 +77,16 @@ export class InstanceController { businessId: instanceData.businessId, }); - instance.sendDataWebhook(Events.INSTANCE_CREATE, { - instanceName: instanceData.instanceName, - instanceId: instanceId, - }); - this.waMonitor.waInstances[instance.instanceName] = instance; this.waMonitor.delInstanceTime(instance.instanceName); // set events - eventController.setInstance(instance.instanceName, instanceData); + await eventController.setInstance(instance.instanceName, instanceData); + + instance.sendDataWebhook(Events.INSTANCE_CREATE, { + instanceName: instanceData.instanceName, + instanceId: instanceId, + }); if (instanceData.proxyHost && instanceData.proxyPort && instanceData.proxyProtocol) { const testProxy = await this.proxyService.testProxy({ diff --git a/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts b/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts index af33ae6f..b2a26162 100644 --- a/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts +++ b/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts @@ -102,7 +102,7 @@ export class RabbitmqController extends EventController { }); if (!data) { - return null; + throw new NotFoundException('Instance websocket not found'); } return data; diff --git a/src/api/integrations/event/sqs/controllers/sqs.controller.ts b/src/api/integrations/event/sqs/controllers/sqs.controller.ts index 519e2762..aeead83a 100644 --- a/src/api/integrations/event/sqs/controllers/sqs.controller.ts +++ b/src/api/integrations/event/sqs/controllers/sqs.controller.ts @@ -88,7 +88,7 @@ export class SqsController extends EventController { }); if (!data) { - return null; + throw new NotFoundException('Instance websocket not found'); } return data; diff --git a/src/api/integrations/event/webhook/controllers/webhook.controller.ts b/src/api/integrations/event/webhook/controllers/webhook.controller.ts index 5f924c6b..9eb5216b 100644 --- a/src/api/integrations/event/webhook/controllers/webhook.controller.ts +++ b/src/api/integrations/event/webhook/controllers/webhook.controller.ts @@ -62,7 +62,7 @@ export class WebhookController extends EventController { }); if (!data) { - return null; + throw new NotFoundException('Instance websocket not found'); } return data; diff --git a/src/api/integrations/event/websocket/controllers/websocket.controller.ts b/src/api/integrations/event/websocket/controllers/websocket.controller.ts index 7bfaa1b4..6625a814 100644 --- a/src/api/integrations/event/websocket/controllers/websocket.controller.ts +++ b/src/api/integrations/event/websocket/controllers/websocket.controller.ts @@ -99,7 +99,7 @@ export class WebsocketController extends EventController { }); if (!data) { - return null; + throw new NotFoundException('Instance websocket not found'); } return data; From 912df56c55b6c6fb169dbf04c871638cf84fd165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=AAnio=20An=C3=ADbal?= Date: Wed, 21 Aug 2024 14:56:12 -0300 Subject: [PATCH 2/4] Remove event emit from channel service preventing exceptions --- src/api/services/channel.service.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index 345ec813..26e9253f 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -58,11 +58,6 @@ export class ChannelStartupService { this.instance.token = instance.token; this.instance.businessId = instance.businessId; - this.sendDataWebhook(Events.STATUS_INSTANCE, { - instance: this.instance.name, - status: 'created', - }); - if (this.configService.get('CHATWOOT').ENABLED && this.localChatwoot.enabled) { this.chatwootService.eventWhatsapp( Events.STATUS_INSTANCE, From 0aeecde84726dc7bfbc4f36556849a2ca1c1ba93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=AAnio=20An=C3=ADbal?= Date: Wed, 21 Aug 2024 14:56:41 -0300 Subject: [PATCH 3/4] Add BOT_CONTACT env option to Chatwoot --- .env.example | 2 ++ .../chatwoot/services/chatwoot.service.ts | 24 +++++++++++++++++-- src/config/env.config.ts | 2 ++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index d1695c94..83f581ce 100644 --- a/.env.example +++ b/.env.example @@ -151,6 +151,8 @@ CHATWOOT_ENABLED=false CHATWOOT_MESSAGE_READ=true # If you leave this option as true, when sending a message in Chatwoot, the client's last message will be marked as read on WhatsApp. CHATWOOT_MESSAGE_DELETE=true +# If you leave this option as true, a contact will be created on Chatwoot to provide the QR Code and update messages about the instance. +CHATWOOT_BOT_CONTACT=true # This db connection is used to import messages from whatsapp to chatwoot database CHATWOOT_IMPORT_DATABASE_CONNECTION_URI=postgresql://user:passwprd@host:5432/chatwoot?sslmode=disable CHATWOOT_IMPORT_PLACEHOLDER_MEDIA_MESSAGE=true diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index f32b3764..3e70b276 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -215,7 +215,13 @@ export class ChatwootService { inboxId = inbox.id; } - this.logger.log(`Inox created - inboxId: ${inboxId}`); + this.logger.log(`Inbox created - inboxId: ${inboxId}`); + + if (!this.configService.get('CHATWOOT').BOT_CONTACT) { + this.logger.log('Chatwoot bot contact is disabled'); + + return true; + } this.logger.log('Creating chatwoot bot contact'); const contact = @@ -826,6 +832,12 @@ export class ChatwootService { return null; } + if (!this.configService.get('CHATWOOT').BOT_CONTACT) { + this.logger.log('Chatwoot bot contact is disabled'); + + return true; + } + const contact = await this.findContact(instance, '123456'); if (!contact) { @@ -940,6 +952,12 @@ export class ChatwootService { return null; } + if (!this.configService.get('CHATWOOT').BOT_CONTACT) { + this.logger.log('Chatwoot bot contact is disabled'); + + return true; + } + const contact = await this.findContact(instance, '123456'); if (!contact) { @@ -1159,7 +1177,9 @@ export class ChatwootService { return { message: 'bot' }; } - if (chatId === '123456' && body.message_type === 'outgoing') { + const cwBotContact = this.configService.get('CHATWOOT').BOT_CONTACT; + + if (cwBotContact && chatId === '123456' && body.message_type === 'outgoing') { const command = messageReceived.replace('/', ''); if (command.includes('init') || command.includes('iniciar')) { diff --git a/src/config/env.config.ts b/src/config/env.config.ts index f3e3f0ae..64c0ea11 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -181,6 +181,7 @@ export type Chatwoot = { ENABLED: boolean; MESSAGE_DELETE: boolean; MESSAGE_READ: boolean; + BOT_CONTACT: boolean; IMPORT: { DATABASE: { CONNECTION: { @@ -426,6 +427,7 @@ export class ConfigService { ENABLED: process.env?.CHATWOOT_ENABLED === 'true', MESSAGE_DELETE: process.env.CHATWOOT_MESSAGE_DELETE === 'true', MESSAGE_READ: process.env.CHATWOOT_MESSAGE_READ === 'true', + BOT_CONTACT: process.env.CHATWOOT_BOT_CONTACT === 'true', IMPORT: { DATABASE: { CONNECTION: { From cca063dfed1e4547cf11ec77ad622247346454df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=AAnio=20An=C3=ADbal?= Date: Wed, 21 Aug 2024 15:02:40 -0300 Subject: [PATCH 4/4] Fix not found event message for the event services --- .../event/rabbitmq/controllers/rabbitmq.controller.ts | 2 +- src/api/integrations/event/sqs/controllers/sqs.controller.ts | 2 +- .../event/webhook/controllers/webhook.controller.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts b/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts index 692027ae..304245c5 100644 --- a/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts +++ b/src/api/integrations/event/rabbitmq/controllers/rabbitmq.controller.ts @@ -106,7 +106,7 @@ export class RabbitmqController extends EventController implements EventControll }); if (!data) { - throw new NotFoundException('Instance websocket not found'); + throw new NotFoundException('Instance rabbitmq not found'); } return data; diff --git a/src/api/integrations/event/sqs/controllers/sqs.controller.ts b/src/api/integrations/event/sqs/controllers/sqs.controller.ts index 5c1514a3..d9d5a1fd 100644 --- a/src/api/integrations/event/sqs/controllers/sqs.controller.ts +++ b/src/api/integrations/event/sqs/controllers/sqs.controller.ts @@ -91,7 +91,7 @@ export class SqsController extends EventController implements EventControllerInt }); if (!data) { - throw new NotFoundException('Instance websocket not found'); + throw new NotFoundException('Instance SQS not found'); } return data; diff --git a/src/api/integrations/event/webhook/controllers/webhook.controller.ts b/src/api/integrations/event/webhook/controllers/webhook.controller.ts index 175b588b..2f9699bb 100644 --- a/src/api/integrations/event/webhook/controllers/webhook.controller.ts +++ b/src/api/integrations/event/webhook/controllers/webhook.controller.ts @@ -63,7 +63,7 @@ export class WebhookController extends EventController implements EventControlle }); if (!data) { - throw new NotFoundException('Instance websocket not found'); + throw new NotFoundException('Instance webhook not found'); } return data;