diff --git a/CHANGELOG.md b/CHANGELOG.md index 552a7d0d..1dc24749 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# 1.5.0 (homolog) + +### Fixed + +* Solved problem when disconnecting from the instance the instance was deleted + +### Integrations + +- Chatwoot: v2.18.0 - v3.0.0 + # 1.4.8 (2023-07-27 10:27) ### Fixed diff --git a/package.json b/package.json index 54511a9c..4d6b2389 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "evolution-api", - "version": "1.4.8", + "version": "1.5.0", "description": "Rest api for communication with WhatsApp", "main": "./dist/src/main.js", "scripts": { diff --git a/src/whatsapp/controllers/chatwoot.controller.ts b/src/whatsapp/controllers/chatwoot.controller.ts index ab291c43..9dc493dc 100644 --- a/src/whatsapp/controllers/chatwoot.controller.ts +++ b/src/whatsapp/controllers/chatwoot.controller.ts @@ -90,10 +90,4 @@ export class ChatwootController { return chatwootService.receiveWebhook(instance, data); } - - public async newInstance(data: any) { - const chatwootService = new ChatwootService(waMonitor, this.configService); - - return chatwootService.newInstance(data); - } } diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 2b62af23..096e6c2c 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -187,7 +187,7 @@ export class InstanceController { this.chatwootService.initInstanceChatwoot( instance, - instance.instanceName, + instance.instanceName.split('-cwId-')[0], `${urlServer}/chatwoot/webhook/${instance.instanceName}`, qrcode, number, @@ -234,6 +234,10 @@ export class InstanceController { this.logger.verbose('state: ' + state); + if (!state) { + throw new BadRequestException('The "' + instanceName + '" instance does not exist'); + } + if (state == 'open') { return await this.connectionState({ instanceName }); } diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 23efc66f..f78f1c36 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -5,7 +5,7 @@ import { createReadStream, readFileSync, unlinkSync, writeFileSync } from 'fs'; import mimeTypes from 'mime-types'; import path from 'path'; -import { ConfigService, HttpServer } from '../../config/env.config'; +import { ConfigService } from '../../config/env.config'; import { Logger } from '../../config/logger.config'; import { ROOT_DIR } from '../../config/path.config'; import { ChatwootDto } from '../dto/chatwoot.dto'; @@ -578,7 +578,7 @@ export class ChatwootService { } this.logger.verbose('find inbox by name'); - const findByName = inbox.payload.find((inbox) => inbox.name === instance.instanceName); + const findByName = inbox.payload.find((inbox) => inbox.name === instance.instanceName.split('-cwId-')[0]); if (!findByName) { this.logger.warn('inbox not found'); @@ -996,39 +996,6 @@ export class ChatwootService { await waInstance?.client?.logout('Log out instance: ' + instance.instanceName); await waInstance?.client?.ws?.close(); } - - if (command.includes('new_instance')) { - const urlServer = this.configService.get('SERVER').URL; - const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY; - - const data = { - instanceName: command.split(':')[1], - qrcode: true, - chatwoot_account_id: this.provider.account_id, - chatwoot_token: this.provider.token, - chatwoot_url: this.provider.url, - chatwoot_sign_msg: this.provider.sign_msg, - chatwoot_reopen_conversation: this.provider.reopen_conversation, - chatwoot_conversation_pending: this.provider.conversation_pending, - }; - - if (command.split(':')[2]) { - data['number'] = command.split(':')[2]; - } - - const config = { - method: 'post', - maxBodyLength: Infinity, - url: `${urlServer}/instance/create`, - headers: { - 'Content-Type': 'application/json', - apikey: apiKey, - }, - data: data, - }; - - await axios.request(config); - } } if (body.message_type === 'outgoing' && body?.conversation?.messages?.length && chatId !== '123456') { @@ -1342,7 +1309,7 @@ export class ChatwootService { if (!body.key.fromMe) { this.logger.verbose('message is not from me'); - content = `**${participantName}**\n\n${bodyMessage}`; + content = `**${participantName}:**\n\n${bodyMessage}`; } else { this.logger.verbose('message is from me'); content = `${bodyMessage}`; @@ -1515,50 +1482,4 @@ export class ChatwootService { this.logger.error(error); } } - - public async newInstance(data: any) { - try { - const instanceName = data.instanceName; - const qrcode = true; - const number = data.number; - const accountId = data.accountId; - const chatwootToken = data.token; - const chatwootUrl = data.url; - const signMsg = true; - const urlServer = this.configService.get('SERVER').URL; - const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY; - - const requestData = { - instanceName, - qrcode, - chatwoot_account_id: accountId, - chatwoot_token: chatwootToken, - chatwoot_url: chatwootUrl, - chatwoot_sign_msg: signMsg, - }; - - if (number) { - requestData['number'] = number; - } - - // eslint-disable-next-line - const config = { - method: 'post', - maxBodyLength: Infinity, - url: `${urlServer}/instance/create`, - headers: { - 'Content-Type': 'application/json', - apikey: apiKey, - }, - data: requestData, - }; - - // await axios.request(config); - - return true; - } catch (error) { - this.logger.error(error); - return null; - } - } } diff --git a/src/whatsapp/services/monitor.service.ts b/src/whatsapp/services/monitor.service.ts index 50f671d9..c0b9b610 100644 --- a/src/whatsapp/services/monitor.service.ts +++ b/src/whatsapp/services/monitor.service.ts @@ -335,11 +335,14 @@ export class WAMonitoringService { this.logger.verbose('checking instances without connection'); this.eventEmitter.on('no.connection', async (instanceName) => { try { - this.logger.verbose('instance: ' + instanceName + ' - removing from memory'); - this.waInstances[instanceName] = undefined; + this.logger.verbose('logging out instance: ' + instanceName); + await this.waInstances[instanceName]?.client?.logout('Log out instance: ' + instanceName); - this.logger.verbose('request cleaning up instance: ' + instanceName); - this.cleaningUp(instanceName); + this.logger.verbose('close connection instance: ' + instanceName); + this.waInstances[instanceName]?.client?.ws?.close(); + + this.waInstances[instanceName].instance.qrcode = { count: 0 }; + this.waInstances[instanceName].stateConnection.state = 'close'; } catch (error) { this.logger.error({ localError: 'noConnection', diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 6de7d847..46b909a0 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -135,12 +135,12 @@ export class WAStartupService { } private readonly logger = new Logger(WAStartupService.name); - private readonly instance: wa.Instance = {}; + public readonly instance: wa.Instance = {}; public client: WASocket; private readonly localWebhook: wa.LocalWebHook = {}; private readonly localChatwoot: wa.LocalChatwoot = {}; private readonly localSettings: wa.LocalSettings = {}; - private stateConnection: wa.StateConnection = { state: 'close' }; + public stateConnection: wa.StateConnection = { state: 'close' }; public readonly storePath = join(ROOT_DIR, 'store'); private readonly msgRetryCounterCache: CacheStore = new NodeCache(); private readonly userDevicesCache: CacheStore = new NodeCache(); @@ -558,6 +558,7 @@ export class WAStartupService { this.logger.verbose('Connection update'); if (qr) { this.logger.verbose('QR code found'); + console.log('this.instance.qrcode', this.instance.qrcode); if (this.instance.qrcode.count === this.configService.get('QRCODE').LIMIT) { this.logger.verbose('QR code limit reached');