From 3bf975d90f145d80b77607a8793fa4383c54c984 Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Sun, 21 Apr 2024 16:33:20 -0300 Subject: [PATCH 01/15] Set chatwoot custom inbox name --- src/api/controllers/instance.controller.ts | 5 +++-- src/api/dto/instance.dto.ts | 1 + .../integrations/chatwoot/controllers/chatwoot.controller.ts | 5 ++++- src/api/integrations/chatwoot/services/chatwoot.service.ts | 5 +++-- src/api/integrations/chatwoot/validate/chatwoot.schema.ts | 1 + 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index abe1dd3b3..8adf2d572 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -65,6 +65,7 @@ export class InstanceController { chatwoot_reopen_conversation, chatwoot_conversation_pending, chatwoot_import_contacts, + chatwoot_name_inbox, chatwoot_import_messages, chatwoot_days_limit_import_messages, reject_call, @@ -513,7 +514,7 @@ export class InstanceController { token: chatwoot_token, url: chatwoot_url, sign_msg: chatwoot_sign_msg || false, - name_inbox: instance.instanceName.split('-cwId-')[0], + name_inbox: chatwoot_name_inbox ?? instance.instanceName.split('-cwId-')[0], number, reopen_conversation: chatwoot_reopen_conversation || false, conversation_pending: chatwoot_conversation_pending || false, @@ -577,7 +578,7 @@ export class InstanceController { import_messages: chatwoot_import_messages ?? true, days_limit_import_messages: chatwoot_days_limit_import_messages || 60, number, - name_inbox: instance.instanceName, + name_inbox: chatwoot_name_inbox ?? instance.instanceName, webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(instance.instanceName)}`, }, }; diff --git a/src/api/dto/instance.dto.ts b/src/api/dto/instance.dto.ts index b703b9da9..59c00a7d0 100644 --- a/src/api/dto/instance.dto.ts +++ b/src/api/dto/instance.dto.ts @@ -30,6 +30,7 @@ export class InstanceDto { chatwoot_import_contacts?: boolean; chatwoot_import_messages?: boolean; chatwoot_days_limit_import_messages?: number; + chatwoot_name_inbox?: string; websocket_enabled?: boolean; websocket_events?: string[]; rabbitmq_enabled?: boolean; diff --git a/src/api/integrations/chatwoot/controllers/chatwoot.controller.ts b/src/api/integrations/chatwoot/controllers/chatwoot.controller.ts index 2621465e0..a70e70ff1 100644 --- a/src/api/integrations/chatwoot/controllers/chatwoot.controller.ts +++ b/src/api/integrations/chatwoot/controllers/chatwoot.controller.ts @@ -55,9 +55,12 @@ export class ChatwootController { data.import_messages = false; data.days_limit_import_messages = 0; data.auto_create = false; + data.name_inbox = ''; } - data.name_inbox = instance.instanceName; + if (!data.name_inbox || data.name_inbox === '') { + data.name_inbox = instance.instanceName; + } const result = await this.chatwootService.create(instance, data); diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index e484ec662..116c15a81 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -91,6 +91,7 @@ export class ChatwootService { with_credentials: true, credentials: 'include', token: this.provider.token, + name_inbox: this.provider.name_inbox, }; } @@ -110,7 +111,7 @@ export class ChatwootService { await this.initInstanceChatwoot( instance, - instance.instanceName.split('-cwId-')[0], + data.name_inbox ?? instance.instanceName.split('-cwId-')[0], `${urlServer}/chatwoot/webhook/${encodeURIComponent(instance.instanceName)}`, true, data.number, @@ -710,7 +711,7 @@ export class ChatwootService { } this.logger.verbose('find inbox by name'); - const findByName = inbox.payload.find((inbox) => inbox.name === instance.instanceName.split('-cwId-')[0]); + const findByName = inbox.payload.find((inbox) => inbox.name === this.getClientCwConfig().name_inbox); if (!findByName) { this.logger.warn('inbox not found'); diff --git a/src/api/integrations/chatwoot/validate/chatwoot.schema.ts b/src/api/integrations/chatwoot/validate/chatwoot.schema.ts index d2784daf0..33652ec33 100644 --- a/src/api/integrations/chatwoot/validate/chatwoot.schema.ts +++ b/src/api/integrations/chatwoot/validate/chatwoot.schema.ts @@ -30,6 +30,7 @@ export const chatwootSchema: JSONSchema7 = { url: { type: 'string' }, sign_msg: { type: 'boolean', enum: [true, false] }, sign_delimiter: { type: ['string', 'null'] }, + name_inbox: { type: ['string', 'null'] }, reopen_conversation: { type: 'boolean', enum: [true, false] }, conversation_pending: { type: 'boolean', enum: [true, false] }, auto_create: { type: 'boolean', enum: [true, false] }, From 1f128747bbc8af0987fbb3b5f45a00982a4237ca Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Sun, 21 Apr 2024 16:37:27 -0300 Subject: [PATCH 02/15] Fix inbox name on cw config --- src/api/integrations/chatwoot/services/chatwoot.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index 116c15a81..e28821bb8 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -85,7 +85,7 @@ export class ChatwootService { return client; } - public getClientCwConfig(): ChatwootAPIConfig { + public getClientCwConfig(): ChatwootAPIConfig & { name_inbox: string } { return { basePath: this.provider.url, with_credentials: true, From f44ab0f67872073d14b3b3db19907ee5a4c13d29 Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Sun, 21 Apr 2024 16:46:49 -0300 Subject: [PATCH 03/15] Update current inbox if exists --- .../chatwoot/services/chatwoot.service.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index e28821bb8..b57fee496 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -216,6 +216,19 @@ export class ChatwootService { } inboxId = inbox.id; + const data = { + type: 'api', + webhook_url: webhookUrl, + }; + await client.inboxes.update({ + id: inboxId, + accountId: this.provider.account_id, + data: { + enable_auto_assignment: false, + name: inboxName, + channel: data as any, + }, + }); } this.logger.verbose('find contact in chatwoot and create if not exists'); From 26ff0b634fc7df6141eb79c83b73be07227dc7ef Mon Sep 17 00:00:00 2001 From: Judson Cairo Date: Sun, 21 Apr 2024 16:49:15 -0300 Subject: [PATCH 04/15] Revert "Update current inbox if exists" This reverts commit f44ab0f67872073d14b3b3db19907ee5a4c13d29. --- .../chatwoot/services/chatwoot.service.ts | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index b57fee496..e28821bb8 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -216,19 +216,6 @@ export class ChatwootService { } inboxId = inbox.id; - const data = { - type: 'api', - webhook_url: webhookUrl, - }; - await client.inboxes.update({ - id: inboxId, - accountId: this.provider.account_id, - data: { - enable_auto_assignment: false, - name: inboxName, - channel: data as any, - }, - }); } this.logger.verbose('find contact in chatwoot and create if not exists'); From 0ee243f2843304a36822bd81a843bd1c43904786 Mon Sep 17 00:00:00 2001 From: jaison-x Date: Mon, 22 Apr 2024 13:37:52 -0300 Subject: [PATCH 05/15] fix(chatwoot): fix bug when chatwoot params reopen_conversation and conversation_pending are enabled --- src/api/integrations/chatwoot/services/chatwoot.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index e484ec662..a4f9e228d 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -628,7 +628,7 @@ export class ChatwootService { id: contactId, })) as any; - if (contactConversations) { + if (contactConversations?.payload?.length) { let conversation: any; if (this.provider.reopen_conversation) { conversation = contactConversations.payload.find((conversation) => conversation.inbox_id == filterInbox.id); From 53361682f4c06cb1d90baf2c2eaf26fa38653479 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Tue, 23 Apr 2024 08:01:59 -0300 Subject: [PATCH 06/15] Recovering messages --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bf5832ad2..f584674c1 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "@figuro/chatwoot-sdk": "^1.1.16", "@hapi/boom": "^10.0.1", "@sentry/node": "^7.59.2", - "@whiskeysockets/baileys": "github:AtendAI/Baileys", + "@whiskeysockets/baileys": "github:WhiskeySockets/Baileys#fix/missing-messages", "amqplib": "^0.10.3", "aws-sdk": "^2.1499.0", "axios": "^1.6.5", From 84ad8e0d6eab72b99ff976fda6b1f66b0c73bf5a Mon Sep 17 00:00:00 2001 From: jaison-x Date: Tue, 23 Apr 2024 15:06:06 -0300 Subject: [PATCH 07/15] feat(chatwoot): send private message on error message sent from chatwoot --- .../chatwoot/services/chatwoot.service.ts | 62 ++++++++++++++----- src/utils/translations/en.json | 1 + src/utils/translations/es.json | 1 + src/utils/translations/pt-BR.json | 1 + 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index e484ec662..319677933 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -8,6 +8,7 @@ import ChatwootClient, { inbox, } from '@figuro/chatwoot-sdk'; import { request as chatwootRequest } from '@figuro/chatwoot-sdk/dist/core/request'; +import { proto } from '@whiskeysockets/baileys'; import axios from 'axios'; import FormData from 'form-data'; import { createReadStream, unlinkSync, writeFileSync } from 'fs'; @@ -1106,6 +1107,26 @@ export class ChatwootService { } } + public async onSendMessageError(instance: InstanceDto, conversation: number, error?: string) { + const client = await this.clientCw(instance); + + if (!client) { + return; + } + + client.messages.create({ + accountId: this.provider.account_id, + conversationId: conversation, + data: { + content: i18next.t('cw.message.notsent', { + error: error.length > 0 ? `_${error}_` : '', + }), + message_type: 'outgoing', + private: true, + }, + }); + } + public async receiveWebhook(instance: InstanceDto, body: any) { try { await new Promise((resolve) => setTimeout(resolve, 500)); @@ -1310,6 +1331,9 @@ export class ChatwootService { formatText, options, ); + if (!messageSent && body.conversation?.id) { + this.onSendMessageError(instance, body.conversation?.id); + } this.updateChatwootMessageId( { @@ -1343,23 +1367,31 @@ export class ChatwootService { }, }; - const messageSent = await waInstance?.textMessage(data, true); + let messageSent: MessageRaw | proto.WebMessageInfo; + try { + messageSent = await waInstance?.textMessage(data, true); - this.updateChatwootMessageId( - { - ...messageSent, - owner: instance.instanceName, - }, - { - messageId: body.id, - inboxId: body.inbox?.id, - conversationId: body.conversation?.id, - contactInbox: { - sourceId: body.conversation?.contact_inbox?.source_id, + this.updateChatwootMessageId( + { + ...messageSent, + owner: instance.instanceName, }, - }, - instance, - ); + { + messageId: body.id, + inboxId: body.inbox?.id, + conversationId: body.conversation?.id, + contactInbox: { + sourceId: body.conversation?.contact_inbox?.source_id, + }, + }, + instance, + ); + } catch (error) { + if (!messageSent && body.conversation?.id) { + this.onSendMessageError(instance, body.conversation?.id, error.toString()); + } + throw error; + } } } diff --git a/src/utils/translations/en.json b/src/utils/translations/en.json index f0a64def3..1b10e7b8a 100644 --- a/src/utils/translations/en.json +++ b/src/utils/translations/en.json @@ -22,5 +22,6 @@ "cw.contactMessage.contact": "Contact", "cw.contactMessage.name": "Name", "cw.contactMessage.number": "Number", + "cw.message.notsent": "🚨 The message could not be sent. Please check your connection. {{error}}", "cw.message.edited": "Edited Message" } \ No newline at end of file diff --git a/src/utils/translations/es.json b/src/utils/translations/es.json index 287840639..13f0d4b78 100644 --- a/src/utils/translations/es.json +++ b/src/utils/translations/es.json @@ -22,5 +22,6 @@ "cw.contactMessage.contact": "Contacto", "cw.contactMessage.name": "Nombre", "cw.contactMessage.number": "Numero", + "cw.message.notsent": "🚨 El mensaje no se pudo enviar. Comprueba tu conexión. {{error}}", "cw.message.edited": "Mensaje editado" } \ No newline at end of file diff --git a/src/utils/translations/pt-BR.json b/src/utils/translations/pt-BR.json index a8d62417c..85ed293b0 100644 --- a/src/utils/translations/pt-BR.json +++ b/src/utils/translations/pt-BR.json @@ -22,5 +22,6 @@ "cw.contactMessage.contact": "Contato", "cw.contactMessage.name": "Nome", "cw.contactMessage.number": "Número", + "cw.message.notsent": "🚨 Não foi possível enviar a mensagem. Verifique sua conexão. {{error}}", "cw.message.edited": "Mensagem editada" } \ No newline at end of file From 96fdb210be74c5f0e88501cc298bcbae99a02e20 Mon Sep 17 00:00:00 2001 From: jaison-x Date: Tue, 23 Apr 2024 17:14:41 -0300 Subject: [PATCH 08/15] feat(chatwoot): send private message on error message sent from chatwoot --- .../integrations/chatwoot/services/chatwoot.service.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index 319677933..01afda542 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -1119,7 +1119,7 @@ export class ChatwootService { conversationId: conversation, data: { content: i18next.t('cw.message.notsent', { - error: error.length > 0 ? `_${error}_` : '', + error: error?.length > 0 ? `_${error}_` : '', }), message_type: 'outgoing', private: true, @@ -1295,6 +1295,11 @@ export class ChatwootService { return { message: 'bot' }; } + if (!waInstance && body.conversation?.id) { + this.onSendMessageError(instance, body.conversation?.id, 'Instance not found'); + return { message: 'bot' }; + } + this.logger.verbose('Format message to send'); let formatText: string; if (senderName === null || senderName === undefined) { @@ -1370,6 +1375,9 @@ export class ChatwootService { let messageSent: MessageRaw | proto.WebMessageInfo; try { messageSent = await waInstance?.textMessage(data, true); + if (!messageSent) { + throw new Error('Message not sent'); + } this.updateChatwootMessageId( { From 6c8ffd8ec6a021193513ab054709af6a0cd96152 Mon Sep 17 00:00:00 2001 From: jaison-x Date: Tue, 23 Apr 2024 17:26:22 -0300 Subject: [PATCH 09/15] feat(chatwoot): send private message on error message sent from chatwoot From e27818ecda3a8541d84ac7eef3b26d21f142a2a9 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Wed, 24 Apr 2024 18:55:38 -0300 Subject: [PATCH 10/15] adjusts in integration --- src/api/services/integration.service.ts | 3 ++- src/api/services/whatsapp.service.ts | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/api/services/integration.service.ts b/src/api/services/integration.service.ts index 6490a491d..c457fa933 100644 --- a/src/api/services/integration.service.ts +++ b/src/api/services/integration.service.ts @@ -22,7 +22,8 @@ export class IntegrationService { const result = await this.waMonitor.waInstances[instance.instanceName].findIntegration(); if (Object.keys(result).length === 0) { - throw new Error('Integration not found'); + this.create(instance, { integration: 'WHATSAPP-BAILEYS', number: '', token: '' }); + return { integration: 'WHATSAPP-BAILEYS', number: '', token: '' }; } return result; diff --git a/src/api/services/whatsapp.service.ts b/src/api/services/whatsapp.service.ts index ade84c4fd..097b90d0e 100644 --- a/src/api/services/whatsapp.service.ts +++ b/src/api/services/whatsapp.service.ts @@ -136,11 +136,13 @@ export class WAStartupService { public async findIntegration() { this.logger.verbose('Finding integration'); - const data = await this.repository.integration.find(this.instanceName); + let data: any; + + data = await this.repository.integration.find(this.instanceName); if (!data) { - this.logger.verbose('Integration not found'); - throw new NotFoundException('Integration not found'); + this.repository.integration.create({ integration: 'WHATSAPP-BAILEYS', number: '', token: '' }, this.instanceName); + data = { integration: 'WHATSAPP-BAILEYS', number: '', token: '' }; } this.logger.verbose(`Integration: ${data.integration}`); From d45b7af3b69c8926f776f5b3c62a7ef0e710306a Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sat, 27 Apr 2024 13:11:33 -0300 Subject: [PATCH 11/15] fix: recovering messages --- src/api/services/whatsapp/whatsapp.baileys.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/services/whatsapp/whatsapp.baileys.service.ts b/src/api/services/whatsapp/whatsapp.baileys.service.ts index d8d856a16..9821d9aee 100644 --- a/src/api/services/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/services/whatsapp/whatsapp.baileys.service.ts @@ -148,17 +148,17 @@ export class BaileysStartupService extends WAStartupService { public mobile: boolean; private async recoveringMessages() { + this.logger.info('Recovering messages lost'); const cacheConf = this.configService.get('CACHE'); if ((cacheConf?.REDIS?.ENABLED && cacheConf?.REDIS?.URI !== '') || cacheConf?.LOCAL?.ENABLED) { setInterval(async () => { - this.logger.info('Recovering messages'); this.messagesLostCache.keys().then((keys) => { keys.forEach(async (key) => { const message = await this.messagesLostCache.get(key.split(':')[2]); if (message.messageStubParameters && message.messageStubParameters[0] === 'Message absent from node') { - this.logger.verbose('Message absent from node, retrying to send, key: ' + key.split(':')[2]); + this.logger.info('Message absent from node, retrying to send, key: ' + key.split(':')[2]); await this.client.sendMessageAck(JSON.parse(message.messageStubParameters[1], BufferJSON.reviver)); } }); From d95791cc18715ea705527557aa478e4516e69e60 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sat, 27 Apr 2024 13:12:29 -0300 Subject: [PATCH 12/15] changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc11ef67c..372d8e849 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,10 @@ -# 1.7.4 (develop) +# 1.8.0 (develop) ### Fixed * Adjusts in proxy on fetchAgent * Recovering messages lost with redis cache * Log when init redis cache service +* Recovering messages lost with redis cache # 1.7.3 (2024-04-18 12:07) From 00e7fcc46b447d680c581049e474f93a63efed2e Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sat, 27 Apr 2024 13:13:43 -0300 Subject: [PATCH 13/15] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 372d8e849..a55f61d29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Recovering messages lost with redis cache * Log when init redis cache service * Recovering messages lost with redis cache +* Chatwoot inbox name # 1.7.3 (2024-04-18 12:07) From 14c210c771707dfc6ac408f7fa9bd3fa4b085196 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sun, 28 Apr 2024 09:41:22 -0300 Subject: [PATCH 14/15] fix: new version baileys --- CHANGELOG.md | 3 ++- package.json | 2 +- src/api/controllers/instance.controller.ts | 6 ++--- src/api/dto/chat.dto.ts | 2 +- src/api/dto/instance.dto.ts | 2 +- src/api/dto/sendMessage.dto.ts | 2 +- .../chatwoot/services/chatwoot.service.ts | 2 +- .../chatwoot/utils/chatwoot-import-helper.ts | 2 +- src/api/services/cache.service.ts | 2 +- ...whatsapp.service.ts => channel.service.ts} | 26 +++++++++---------- .../whatsapp.baileys.service.ts | 12 ++++----- .../whatsapp.business.service.ts | 4 +-- src/api/services/monitor.service.ts | 4 +-- src/api/types/wa.types.ts | 2 +- src/cache/rediscache.ts | 2 +- src/utils/use-multi-file-auth-state-db.ts | 9 +------ .../use-multi-file-auth-state-redis-db.ts | 8 +----- 17 files changed, 39 insertions(+), 51 deletions(-) rename src/api/services/{whatsapp.service.ts => channel.service.ts} (97%) rename src/api/services/{whatsapp => channels}/whatsapp.baileys.service.ts (99%) rename src/api/services/{whatsapp => channels}/whatsapp.business.service.ts (99%) diff --git a/CHANGELOG.md b/CHANGELOG.md index a55f61d29..0c28267f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 1.8.0 (develop) +# 1.7.4 (develop) ### Fixed * Adjusts in proxy on fetchAgent @@ -6,6 +6,7 @@ * Log when init redis cache service * Recovering messages lost with redis cache * Chatwoot inbox name +* Update Baileys version # 1.7.3 (2024-04-18 12:07) diff --git a/package.json b/package.json index f584674c1..f0b252a71 100644 --- a/package.json +++ b/package.json @@ -46,10 +46,10 @@ "@figuro/chatwoot-sdk": "^1.1.16", "@hapi/boom": "^10.0.1", "@sentry/node": "^7.59.2", - "@whiskeysockets/baileys": "github:WhiskeySockets/Baileys#fix/missing-messages", "amqplib": "^0.10.3", "aws-sdk": "^2.1499.0", "axios": "^1.6.5", + "baileys": "^6.7.0", "class-validator": "^0.14.1", "compression": "^1.7.4", "cors": "^2.8.5", diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index 8adf2d572..648e549f1 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -1,4 +1,4 @@ -import { delay } from '@whiskeysockets/baileys'; +import { delay } from 'baileys'; import { isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; import { v4 } from 'uuid'; @@ -15,12 +15,12 @@ import { WebsocketService } from '../integrations/websocket/services/websocket.s import { RepositoryBroker } from '../repository/repository.manager'; import { AuthService, OldToken } from '../services/auth.service'; import { CacheService } from '../services/cache.service'; +import { BaileysStartupService } from '../services/channels/whatsapp.baileys.service'; +import { BusinessStartupService } from '../services/channels/whatsapp.business.service'; import { IntegrationService } from '../services/integration.service'; import { WAMonitoringService } from '../services/monitor.service'; import { SettingsService } from '../services/settings.service'; import { WebhookService } from '../services/webhook.service'; -import { BaileysStartupService } from '../services/whatsapp/whatsapp.baileys.service'; -import { BusinessStartupService } from '../services/whatsapp/whatsapp.business.service'; import { Events, Integration, wa } from '../types/wa.types'; import { ProxyController } from './proxy.controller'; diff --git a/src/api/dto/chat.dto.ts b/src/api/dto/chat.dto.ts index 24f048473..0178de2a1 100644 --- a/src/api/dto/chat.dto.ts +++ b/src/api/dto/chat.dto.ts @@ -1,4 +1,4 @@ -import { proto, WAPresence, WAPrivacyOnlineValue, WAPrivacyValue, WAReadReceiptsValue } from '@whiskeysockets/baileys'; +import { proto, WAPresence, WAPrivacyOnlineValue, WAPrivacyValue, WAReadReceiptsValue } from 'baileys'; export class OnWhatsAppDto { constructor( diff --git a/src/api/dto/instance.dto.ts b/src/api/dto/instance.dto.ts index 59c00a7d0..f3329e3e1 100644 --- a/src/api/dto/instance.dto.ts +++ b/src/api/dto/instance.dto.ts @@ -1,4 +1,4 @@ -import { WAPresence } from '@whiskeysockets/baileys'; +import { WAPresence } from 'baileys'; import { ProxyDto } from './proxy.dto'; diff --git a/src/api/dto/sendMessage.dto.ts b/src/api/dto/sendMessage.dto.ts index 7bb33074b..5c197d446 100644 --- a/src/api/dto/sendMessage.dto.ts +++ b/src/api/dto/sendMessage.dto.ts @@ -1,4 +1,4 @@ -import { proto, WAPresence } from '@whiskeysockets/baileys'; +import { proto, WAPresence } from 'baileys'; export class Quoted { key: proto.IMessageKey; diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index e52a03d65..cf122fb10 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -8,8 +8,8 @@ import ChatwootClient, { inbox, } from '@figuro/chatwoot-sdk'; import { request as chatwootRequest } from '@figuro/chatwoot-sdk/dist/core/request'; -import { proto } from '@whiskeysockets/baileys'; import axios from 'axios'; +import { proto } from 'baileys'; import FormData from 'form-data'; import { createReadStream, unlinkSync, writeFileSync } from 'fs'; import Jimp from 'jimp'; diff --git a/src/api/integrations/chatwoot/utils/chatwoot-import-helper.ts b/src/api/integrations/chatwoot/utils/chatwoot-import-helper.ts index dd0bb23a4..e102aa57f 100644 --- a/src/api/integrations/chatwoot/utils/chatwoot-import-helper.ts +++ b/src/api/integrations/chatwoot/utils/chatwoot-import-helper.ts @@ -1,5 +1,5 @@ import { inbox } from '@figuro/chatwoot-sdk'; -import { proto } from '@whiskeysockets/baileys'; +import { proto } from 'baileys'; import { InstanceDto } from '../../../../api/dto/instance.dto'; import { ChatwootRaw, ContactRaw, MessageRaw } from '../../../../api/models'; diff --git a/src/api/services/cache.service.ts b/src/api/services/cache.service.ts index caf3dbfae..e03b3eb56 100644 --- a/src/api/services/cache.service.ts +++ b/src/api/services/cache.service.ts @@ -1,4 +1,4 @@ -import { BufferJSON } from '@whiskeysockets/baileys'; +import { BufferJSON } from 'baileys'; import { Logger } from '../../config/logger.config'; import { ICache } from '../abstract/abstract.cache'; diff --git a/src/api/services/whatsapp.service.ts b/src/api/services/channel.service.ts similarity index 97% rename from src/api/services/whatsapp.service.ts rename to src/api/services/channel.service.ts index 097b90d0e..a6fef45bc 100644 --- a/src/api/services/whatsapp.service.ts +++ b/src/api/services/channel.service.ts @@ -1,5 +1,5 @@ -import { WASocket } from '@whiskeysockets/baileys'; import axios from 'axios'; +import { WASocket } from 'baileys'; import { execSync } from 'child_process'; import { isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; @@ -38,17 +38,17 @@ import { waMonitor } from '../server.module'; import { Events, wa } from '../types/wa.types'; import { CacheService } from './cache.service'; -export class WAStartupService { +export class ChannelStartupService { constructor( public readonly configService: ConfigService, public readonly eventEmitter: EventEmitter2, public readonly repository: RepositoryBroker, public readonly chatwootCache: CacheService, ) { - this.logger.verbose('WAStartupService initialized'); + this.logger.verbose('ChannelStartupService initialized'); } - public readonly logger = new Logger(WAStartupService.name); + public readonly logger = new Logger(ChannelStartupService.name); public client: WASocket; public readonly instance: wa.Instance = {}; @@ -742,7 +742,7 @@ export class WAStartupService { if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { const logData = { - local: WAStartupService.name + '.sendData-RabbitMQ', + local: ChannelStartupService.name + '.sendData-RabbitMQ', event, instance: this.instance.name, data, @@ -798,7 +798,7 @@ export class WAStartupService { sqs.sendMessage(params, (err, data) => { if (err) { this.logger.error({ - local: WAStartupService.name + '.sendData-SQS', + local: ChannelStartupService.name + '.sendData-SQS', message: err?.message, hostName: err?.hostname, code: err?.code, @@ -810,7 +810,7 @@ export class WAStartupService { } else { if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { const logData = { - local: WAStartupService.name + '.sendData-SQS', + local: ChannelStartupService.name + '.sendData-SQS', event, instance: this.instance.name, data, @@ -854,7 +854,7 @@ export class WAStartupService { if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { const logData = { - local: WAStartupService.name + '.sendData-WebsocketGlobal', + local: ChannelStartupService.name + '.sendData-WebsocketGlobal', event, instance: this.instance.name, data, @@ -884,7 +884,7 @@ export class WAStartupService { if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { const logData = { - local: WAStartupService.name + '.sendData-Websocket', + local: ChannelStartupService.name + '.sendData-Websocket', event, instance: this.instance.name, data, @@ -918,7 +918,7 @@ export class WAStartupService { if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { const logData = { - local: WAStartupService.name + '.sendDataWebhook-local', + local: ChannelStartupService.name + '.sendDataWebhook-local', url: baseURL, event, instance: this.instance.name, @@ -958,7 +958,7 @@ export class WAStartupService { } } catch (error) { this.logger.error({ - local: WAStartupService.name + '.sendDataWebhook-local', + local: ChannelStartupService.name + '.sendDataWebhook-local', message: error?.message, hostName: error?.hostname, syscall: error?.syscall, @@ -990,7 +990,7 @@ export class WAStartupService { if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { const logData = { - local: WAStartupService.name + '.sendDataWebhook-global', + local: ChannelStartupService.name + '.sendDataWebhook-global', url: globalURL, event, instance: this.instance.name, @@ -1029,7 +1029,7 @@ export class WAStartupService { } } catch (error) { this.logger.error({ - local: WAStartupService.name + '.sendDataWebhook-global', + local: ChannelStartupService.name + '.sendDataWebhook-global', message: error?.message, hostName: error?.hostname, syscall: error?.syscall, diff --git a/src/api/services/whatsapp/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts similarity index 99% rename from src/api/services/whatsapp/whatsapp.baileys.service.ts rename to src/api/services/channels/whatsapp.baileys.service.ts index 9821d9aee..b1f961a04 100644 --- a/src/api/services/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -1,5 +1,6 @@ import ffmpegPath from '@ffmpeg-installer/ffmpeg'; import { Boom } from '@hapi/boom'; +import axios from 'axios'; import makeWASocket, { AnyMessageContent, BufferedEventData, @@ -35,10 +36,9 @@ import makeWASocket, { WAMessageUpdate, WAPresence, WASocket, -} from '@whiskeysockets/baileys'; -import { Label } from '@whiskeysockets/baileys/lib/Types/Label'; -import { LabelAssociation } from '@whiskeysockets/baileys/lib/Types/LabelAssociation'; -import axios from 'axios'; +} from 'baileys'; +import { Label } from 'baileys/lib/Types/Label'; +import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation'; import { exec } from 'child_process'; import { arrayUnique, isBase64, isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; @@ -118,9 +118,9 @@ import { RepositoryBroker } from '../../repository/repository.manager'; import { waMonitor } from '../../server.module'; import { Events, MessageSubtype, TypeMediaMessage, wa } from '../../types/wa.types'; import { CacheService } from './../cache.service'; -import { WAStartupService } from './../whatsapp.service'; +import { ChannelStartupService } from './../channel.service'; -export class BaileysStartupService extends WAStartupService { +export class BaileysStartupService extends ChannelStartupService { constructor( public readonly configService: ConfigService, public readonly eventEmitter: EventEmitter2, diff --git a/src/api/services/whatsapp/whatsapp.business.service.ts b/src/api/services/channels/whatsapp.business.service.ts similarity index 99% rename from src/api/services/whatsapp/whatsapp.business.service.ts rename to src/api/services/channels/whatsapp.business.service.ts index 1ed2ddcdb..d55f72e8c 100644 --- a/src/api/services/whatsapp/whatsapp.business.service.ts +++ b/src/api/services/channels/whatsapp.business.service.ts @@ -26,9 +26,9 @@ import { ContactRaw, MessageRaw, MessageUpdateRaw, SettingsRaw } from '../../mod import { RepositoryBroker } from '../../repository/repository.manager'; import { Events, wa } from '../../types/wa.types'; import { CacheService } from './../cache.service'; -import { WAStartupService } from './../whatsapp.service'; +import { ChannelStartupService } from './../channel.service'; -export class BusinessStartupService extends WAStartupService { +export class BusinessStartupService extends ChannelStartupService { constructor( public readonly configService: ConfigService, public readonly eventEmitter: EventEmitter2, diff --git a/src/api/services/monitor.service.ts b/src/api/services/monitor.service.ts index df6b333ad..afeffa954 100644 --- a/src/api/services/monitor.service.ts +++ b/src/api/services/monitor.service.ts @@ -25,8 +25,8 @@ import { import { RepositoryBroker } from '../repository/repository.manager'; import { Integration } from '../types/wa.types'; import { CacheService } from './cache.service'; -import { BaileysStartupService } from './whatsapp/whatsapp.baileys.service'; -import { BusinessStartupService } from './whatsapp/whatsapp.business.service'; +import { BaileysStartupService } from './channels/whatsapp.baileys.service'; +import { BusinessStartupService } from './channels/whatsapp.business.service'; export class WAMonitoringService { constructor( diff --git a/src/api/types/wa.types.ts b/src/api/types/wa.types.ts index 066691e4a..0549f05f0 100644 --- a/src/api/types/wa.types.ts +++ b/src/api/types/wa.types.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-namespace */ -import { AuthenticationState, WAConnectionState } from '@whiskeysockets/baileys'; +import { AuthenticationState, WAConnectionState } from 'baileys'; export enum Events { APPLICATION_STARTUP = 'application.startup', diff --git a/src/cache/rediscache.ts b/src/cache/rediscache.ts index 6e209ef11..c4e98968d 100644 --- a/src/cache/rediscache.ts +++ b/src/cache/rediscache.ts @@ -1,4 +1,4 @@ -import { BufferJSON } from '@whiskeysockets/baileys'; +import { BufferJSON } from 'baileys'; import { RedisClientType } from 'redis'; import { ICache } from '../api/abstract/abstract.cache'; diff --git a/src/utils/use-multi-file-auth-state-db.ts b/src/utils/use-multi-file-auth-state-db.ts index 995ac92ad..f20db84f3 100644 --- a/src/utils/use-multi-file-auth-state-db.ts +++ b/src/utils/use-multi-file-auth-state-db.ts @@ -1,11 +1,4 @@ -import { - AuthenticationCreds, - AuthenticationState, - BufferJSON, - initAuthCreds, - proto, - SignalDataTypeMap, -} from '@whiskeysockets/baileys'; +import { AuthenticationCreds, AuthenticationState, BufferJSON, initAuthCreds, proto, SignalDataTypeMap } from 'baileys'; import { configService, Database } from '../config/env.config'; import { Logger } from '../config/logger.config'; diff --git a/src/utils/use-multi-file-auth-state-redis-db.ts b/src/utils/use-multi-file-auth-state-redis-db.ts index 66bb89ea3..d077b894e 100644 --- a/src/utils/use-multi-file-auth-state-redis-db.ts +++ b/src/utils/use-multi-file-auth-state-redis-db.ts @@ -1,10 +1,4 @@ -import { - AuthenticationCreds, - AuthenticationState, - initAuthCreds, - proto, - SignalDataTypeMap, -} from '@whiskeysockets/baileys'; +import { AuthenticationCreds, AuthenticationState, initAuthCreds, proto, SignalDataTypeMap } from 'baileys'; import { CacheService } from '../api/services/cache.service'; import { Logger } from '../config/logger.config'; From 95907b3cea582d6638d714dce4fe3379e12a3bfa Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sun, 28 Apr 2024 09:47:06 -0300 Subject: [PATCH 15/15] v 1.7.4 --- CHANGELOG.md | 2 +- Dockerfile | 2 +- package.json | 2 +- .../channels/whatsapp.baileys.service.ts | 134 +----------------- src/docs/swagger.yaml | 2 +- 5 files changed, 7 insertions(+), 135 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c28267f3..fc595045b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 1.7.4 (develop) +# 1.7.4 (2024-04-28 09:46) ### Fixed * Adjusts in proxy on fetchAgent diff --git a/Dockerfile b/Dockerfile index a5e5c8b20..51955b71c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM node:20.7.0-alpine AS builder -LABEL version="1.7.3" description="Api to control whatsapp features through http requests." +LABEL version="1.7.4" description="Api to control whatsapp features through http requests." LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes" LABEL contact="contato@agenciadgcode.com" diff --git a/package.json b/package.json index f0b252a71..03ce55e2b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "evolution-api", - "version": "1.7.3", + "version": "1.7.4", "description": "Rest api for communication with WhatsApp", "main": "./dist/src/main.js", "scripts": { diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index b1f961a04..7e2f8d881 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -40,7 +40,7 @@ import makeWASocket, { import { Label } from 'baileys/lib/Types/Label'; import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation'; import { exec } from 'child_process'; -import { arrayUnique, isBase64, isURL } from 'class-validator'; +import { isBase64, isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; // import ffmpeg from 'fluent-ffmpeg'; import fs, { existsSync, readFileSync } from 'fs'; @@ -97,7 +97,6 @@ import { MediaMessage, Options, SendAudioDto, - SendButtonDto, SendContactDto, SendListDto, SendLocationDto, @@ -2207,15 +2206,6 @@ export class BaileysStartupService extends ChannelStartupService { mimetype = response.headers['content-type']; } - // if (isURL(mediaMessage.media)) { - // const response = await axios.get(mediaMessage.media, { responseType: 'arraybuffer' }); - - // mimetype = response.headers['content-type']; - // console.log('mediaMessage.mimetype2', mimetype); - // } else { - // mimetype = getMIMEType(mediaMessage.fileName); - // console.log('mediaMessage.mimetype3', mimetype); - // } } this.logger.verbose('Mimetype: ' + mimetype); @@ -2330,82 +2320,6 @@ export class BaileysStartupService extends ChannelStartupService { return await this.sendMessageWithTyping(data.number, { ...generate.message }, data?.options, isChatwoot); } - // public async processAudio(audio: string, number: string) { - // this.logger.verbose('Processing audio'); - // let tempAudioPath: string; - // let outputAudio: string; - - // number = number.replace(/\D/g, ''); - // const hash = `${number}-${new Date().getTime()}`; - // this.logger.verbose('Hash to audio name: ' + hash); - - // if (isURL(audio)) { - // this.logger.verbose('Audio is url'); - - // outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`; - // tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`; - - // this.logger.verbose('Output audio path: ' + outputAudio); - // this.logger.verbose('Temp audio path: ' + tempAudioPath); - - // const timestamp = new Date().getTime(); - // const url = `${audio}?timestamp=${timestamp}`; - - // this.logger.verbose('Including timestamp in url: ' + url); - - // let config: any = { - // responseType: 'arraybuffer', - // }; - - // if (this.localProxy.enabled) { - // config = { - // ...config, - // httpsAgent: makeProxyAgent(this.localProxy.proxy), - // }; - // } - - // const response = await axios.get(url, config); - // this.logger.verbose('Getting audio from url'); - - // fs.writeFileSync(tempAudioPath, response.data); - // } else { - // this.logger.verbose('Audio is base64'); - - // outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`; - // tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`; - - // this.logger.verbose('Output audio path: ' + outputAudio); - // this.logger.verbose('Temp audio path: ' + tempAudioPath); - - // const audioBuffer = Buffer.from(audio, 'base64'); - // fs.writeFileSync(tempAudioPath, audioBuffer); - // this.logger.verbose('Temp audio created'); - // } - - // this.logger.verbose('Converting audio to mp4'); - // return new Promise((resolve, reject) => { - // // This fix was suggested by @PurpShell - // ffmpeg.setFfmpegPath(ffmpegPath.path); - - // ffmpeg() - // .input(tempAudioPath) - // .outputFormat('ogg') - // .noVideo() - // .audioCodec('libopus') - // .save(outputAudio) - // .on('error', function (error) { - // console.log('error', error); - // fs.unlinkSync(tempAudioPath); - // if (error) reject(error); - // }) - // .on('end', async function () { - // fs.unlinkSync(tempAudioPath); - // resolve(outputAudio); - // }) - // .run(); - // }); - // } - public async processAudio(audio: string, number: string) { this.logger.verbose('Processing audio'); let tempAudioPath: string; @@ -2506,50 +2420,8 @@ export class BaileysStartupService extends ChannelStartupService { ); } - public async buttonMessage(data: SendButtonDto) { - this.logger.verbose('Sending button message'); - const embeddedMedia: any = {}; - let mediatype = 'TEXT'; - - if (data.buttonMessage?.mediaMessage) { - mediatype = data.buttonMessage.mediaMessage?.mediatype.toUpperCase() ?? 'TEXT'; - embeddedMedia.mediaKey = mediatype.toLowerCase() + 'Message'; - const generate = await this.prepareMediaMessage(data.buttonMessage.mediaMessage); - embeddedMedia.message = generate.message[embeddedMedia.mediaKey]; - embeddedMedia.contentText = `*${data.buttonMessage.title}*\n\n${data.buttonMessage.description}`; - } - - const btnItems = { - text: data.buttonMessage.buttons.map((btn) => btn.buttonText), - ids: data.buttonMessage.buttons.map((btn) => btn.buttonId), - }; - - if (!arrayUnique(btnItems.text) || !arrayUnique(btnItems.ids)) { - throw new BadRequestException('Button texts cannot be repeated', 'Button IDs cannot be repeated.'); - } - - return await this.sendMessageWithTyping( - data.number, - { - buttonsMessage: { - text: !embeddedMedia?.mediaKey ? data.buttonMessage.title : undefined, - contentText: embeddedMedia?.contentText ?? data.buttonMessage.description, - footerText: data.buttonMessage?.footerText, - buttons: data.buttonMessage.buttons.map((button) => { - return { - buttonText: { - displayText: button.buttonText, - }, - buttonId: button.buttonId, - type: 1, - }; - }), - headerType: proto.Message.ButtonsMessage.HeaderType[mediatype], - [embeddedMedia?.mediaKey]: embeddedMedia?.message, - }, - }, - data?.options, - ); + public async buttonMessage() { + throw new BadRequestException('Method not available on WhatsApp Baileys'); } public async locationMessage(data: SendLocationDto) { diff --git a/src/docs/swagger.yaml b/src/docs/swagger.yaml index 3a7a2d15a..b4aa646cc 100644 --- a/src/docs/swagger.yaml +++ b/src/docs/swagger.yaml @@ -25,7 +25,7 @@ info: [![Run in Postman](https://run.pstmn.io/button.svg)](https://god.gw.postman.com/run-collection/26869335-5546d063-156b-4529-915f-909dd628c090?action=collection%2Ffork&source=rip_markdown&collection-url=entityId%3D26869335-5546d063-156b-4529-915f-909dd628c090%26entityType%3Dcollection%26workspaceId%3D339a4ee7-378b-45c9-b5b8-fd2c0a9c2442) - version: 1.7.3 + version: 1.7.4 contact: name: DavidsonGomes email: contato@agenciadgcode.com