Skip to content

Commit 633dbb8

Browse files
committed
Merge branch 'release/1.7.4'
2 parents e071f56 + 95907b3 commit 633dbb8

25 files changed

+127
-212
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
# 1.7.4 (develop)
1+
# 1.7.4 (2024-04-28 09:46)
22

33
### Fixed
44
* Adjusts in proxy on fetchAgent
55
* Recovering messages lost with redis cache
66
* Log when init redis cache service
7+
* Recovering messages lost with redis cache
8+
* Chatwoot inbox name
9+
* Update Baileys version
710

811
# 1.7.3 (2024-04-18 12:07)
912

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM node:20.7.0-alpine AS builder
22

3-
LABEL version="1.7.3" description="Api to control whatsapp features through http requests."
3+
LABEL version="1.7.4" description="Api to control whatsapp features through http requests."
44
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
55
LABEL contact="[email protected]"
66

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "evolution-api",
3-
"version": "1.7.3",
3+
"version": "1.7.4",
44
"description": "Rest api for communication with WhatsApp",
55
"main": "./dist/src/main.js",
66
"scripts": {
@@ -46,10 +46,10 @@
4646
"@figuro/chatwoot-sdk": "^1.1.16",
4747
"@hapi/boom": "^10.0.1",
4848
"@sentry/node": "^7.59.2",
49-
"@whiskeysockets/baileys": "github:AtendAI/Baileys",
5049
"amqplib": "^0.10.3",
5150
"aws-sdk": "^2.1499.0",
5251
"axios": "^1.6.5",
52+
"baileys": "^6.7.0",
5353
"class-validator": "^0.14.1",
5454
"compression": "^1.7.4",
5555
"cors": "^2.8.5",

src/api/controllers/instance.controller.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { delay } from '@whiskeysockets/baileys';
1+
import { delay } from 'baileys';
22
import { isURL } from 'class-validator';
33
import EventEmitter2 from 'eventemitter2';
44
import { v4 } from 'uuid';
@@ -15,12 +15,12 @@ import { WebsocketService } from '../integrations/websocket/services/websocket.s
1515
import { RepositoryBroker } from '../repository/repository.manager';
1616
import { AuthService, OldToken } from '../services/auth.service';
1717
import { CacheService } from '../services/cache.service';
18+
import { BaileysStartupService } from '../services/channels/whatsapp.baileys.service';
19+
import { BusinessStartupService } from '../services/channels/whatsapp.business.service';
1820
import { IntegrationService } from '../services/integration.service';
1921
import { WAMonitoringService } from '../services/monitor.service';
2022
import { SettingsService } from '../services/settings.service';
2123
import { WebhookService } from '../services/webhook.service';
22-
import { BaileysStartupService } from '../services/whatsapp/whatsapp.baileys.service';
23-
import { BusinessStartupService } from '../services/whatsapp/whatsapp.business.service';
2424
import { Events, Integration, wa } from '../types/wa.types';
2525
import { ProxyController } from './proxy.controller';
2626

@@ -65,6 +65,7 @@ export class InstanceController {
6565
chatwoot_reopen_conversation,
6666
chatwoot_conversation_pending,
6767
chatwoot_import_contacts,
68+
chatwoot_name_inbox,
6869
chatwoot_import_messages,
6970
chatwoot_days_limit_import_messages,
7071
reject_call,
@@ -513,7 +514,7 @@ export class InstanceController {
513514
token: chatwoot_token,
514515
url: chatwoot_url,
515516
sign_msg: chatwoot_sign_msg || false,
516-
name_inbox: instance.instanceName.split('-cwId-')[0],
517+
name_inbox: chatwoot_name_inbox ?? instance.instanceName.split('-cwId-')[0],
517518
number,
518519
reopen_conversation: chatwoot_reopen_conversation || false,
519520
conversation_pending: chatwoot_conversation_pending || false,
@@ -577,7 +578,7 @@ export class InstanceController {
577578
import_messages: chatwoot_import_messages ?? true,
578579
days_limit_import_messages: chatwoot_days_limit_import_messages || 60,
579580
number,
580-
name_inbox: instance.instanceName,
581+
name_inbox: chatwoot_name_inbox ?? instance.instanceName,
581582
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(instance.instanceName)}`,
582583
},
583584
};

src/api/dto/chat.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { proto, WAPresence, WAPrivacyOnlineValue, WAPrivacyValue, WAReadReceiptsValue } from '@whiskeysockets/baileys';
1+
import { proto, WAPresence, WAPrivacyOnlineValue, WAPrivacyValue, WAReadReceiptsValue } from 'baileys';
22

33
export class OnWhatsAppDto {
44
constructor(

src/api/dto/instance.dto.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WAPresence } from '@whiskeysockets/baileys';
1+
import { WAPresence } from 'baileys';
22

33
import { ProxyDto } from './proxy.dto';
44

@@ -30,6 +30,7 @@ export class InstanceDto {
3030
chatwoot_import_contacts?: boolean;
3131
chatwoot_import_messages?: boolean;
3232
chatwoot_days_limit_import_messages?: number;
33+
chatwoot_name_inbox?: string;
3334
websocket_enabled?: boolean;
3435
websocket_events?: string[];
3536
rabbitmq_enabled?: boolean;

src/api/dto/sendMessage.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { proto, WAPresence } from '@whiskeysockets/baileys';
1+
import { proto, WAPresence } from 'baileys';
22

33
export class Quoted {
44
key: proto.IMessageKey;

src/api/integrations/chatwoot/controllers/chatwoot.controller.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ export class ChatwootController {
5555
data.import_messages = false;
5656
data.days_limit_import_messages = 0;
5757
data.auto_create = false;
58+
data.name_inbox = '';
5859
}
5960

60-
data.name_inbox = instance.instanceName;
61+
if (!data.name_inbox || data.name_inbox === '') {
62+
data.name_inbox = instance.instanceName;
63+
}
6164

6265
const result = await this.chatwootService.create(instance, data);
6366

src/api/integrations/chatwoot/services/chatwoot.service.ts

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ChatwootClient, {
99
} from '@figuro/chatwoot-sdk';
1010
import { request as chatwootRequest } from '@figuro/chatwoot-sdk/dist/core/request';
1111
import axios from 'axios';
12+
import { proto } from 'baileys';
1213
import FormData from 'form-data';
1314
import { createReadStream, unlinkSync, writeFileSync } from 'fs';
1415
import Jimp from 'jimp';
@@ -85,12 +86,13 @@ export class ChatwootService {
8586
return client;
8687
}
8788

88-
public getClientCwConfig(): ChatwootAPIConfig {
89+
public getClientCwConfig(): ChatwootAPIConfig & { name_inbox: string } {
8990
return {
9091
basePath: this.provider.url,
9192
with_credentials: true,
9293
credentials: 'include',
9394
token: this.provider.token,
95+
name_inbox: this.provider.name_inbox,
9496
};
9597
}
9698

@@ -110,7 +112,7 @@ export class ChatwootService {
110112

111113
await this.initInstanceChatwoot(
112114
instance,
113-
instance.instanceName.split('-cwId-')[0],
115+
data.name_inbox ?? instance.instanceName.split('-cwId-')[0],
114116
`${urlServer}/chatwoot/webhook/${encodeURIComponent(instance.instanceName)}`,
115117
true,
116118
data.number,
@@ -628,7 +630,7 @@ export class ChatwootService {
628630
id: contactId,
629631
})) as any;
630632

631-
if (contactConversations) {
633+
if (contactConversations?.payload?.length) {
632634
let conversation: any;
633635
if (this.provider.reopen_conversation) {
634636
conversation = contactConversations.payload.find((conversation) => conversation.inbox_id == filterInbox.id);
@@ -710,7 +712,7 @@ export class ChatwootService {
710712
}
711713

712714
this.logger.verbose('find inbox by name');
713-
const findByName = inbox.payload.find((inbox) => inbox.name === instance.instanceName.split('-cwId-')[0]);
715+
const findByName = inbox.payload.find((inbox) => inbox.name === this.getClientCwConfig().name_inbox);
714716

715717
if (!findByName) {
716718
this.logger.warn('inbox not found');
@@ -1106,6 +1108,26 @@ export class ChatwootService {
11061108
}
11071109
}
11081110

1111+
public async onSendMessageError(instance: InstanceDto, conversation: number, error?: string) {
1112+
const client = await this.clientCw(instance);
1113+
1114+
if (!client) {
1115+
return;
1116+
}
1117+
1118+
client.messages.create({
1119+
accountId: this.provider.account_id,
1120+
conversationId: conversation,
1121+
data: {
1122+
content: i18next.t('cw.message.notsent', {
1123+
error: error?.length > 0 ? `_${error}_` : '',
1124+
}),
1125+
message_type: 'outgoing',
1126+
private: true,
1127+
},
1128+
});
1129+
}
1130+
11091131
public async receiveWebhook(instance: InstanceDto, body: any) {
11101132
try {
11111133
await new Promise((resolve) => setTimeout(resolve, 500));
@@ -1274,6 +1296,11 @@ export class ChatwootService {
12741296
return { message: 'bot' };
12751297
}
12761298

1299+
if (!waInstance && body.conversation?.id) {
1300+
this.onSendMessageError(instance, body.conversation?.id, 'Instance not found');
1301+
return { message: 'bot' };
1302+
}
1303+
12771304
this.logger.verbose('Format message to send');
12781305
let formatText: string;
12791306
if (senderName === null || senderName === undefined) {
@@ -1310,6 +1337,9 @@ export class ChatwootService {
13101337
formatText,
13111338
options,
13121339
);
1340+
if (!messageSent && body.conversation?.id) {
1341+
this.onSendMessageError(instance, body.conversation?.id);
1342+
}
13131343

13141344
this.updateChatwootMessageId(
13151345
{
@@ -1343,23 +1373,34 @@ export class ChatwootService {
13431373
},
13441374
};
13451375

1346-
const messageSent = await waInstance?.textMessage(data, true);
1376+
let messageSent: MessageRaw | proto.WebMessageInfo;
1377+
try {
1378+
messageSent = await waInstance?.textMessage(data, true);
1379+
if (!messageSent) {
1380+
throw new Error('Message not sent');
1381+
}
13471382

1348-
this.updateChatwootMessageId(
1349-
{
1350-
...messageSent,
1351-
owner: instance.instanceName,
1352-
},
1353-
{
1354-
messageId: body.id,
1355-
inboxId: body.inbox?.id,
1356-
conversationId: body.conversation?.id,
1357-
contactInbox: {
1358-
sourceId: body.conversation?.contact_inbox?.source_id,
1383+
this.updateChatwootMessageId(
1384+
{
1385+
...messageSent,
1386+
owner: instance.instanceName,
13591387
},
1360-
},
1361-
instance,
1362-
);
1388+
{
1389+
messageId: body.id,
1390+
inboxId: body.inbox?.id,
1391+
conversationId: body.conversation?.id,
1392+
contactInbox: {
1393+
sourceId: body.conversation?.contact_inbox?.source_id,
1394+
},
1395+
},
1396+
instance,
1397+
);
1398+
} catch (error) {
1399+
if (!messageSent && body.conversation?.id) {
1400+
this.onSendMessageError(instance, body.conversation?.id, error.toString());
1401+
}
1402+
throw error;
1403+
}
13631404
}
13641405
}
13651406

src/api/integrations/chatwoot/utils/chatwoot-import-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { inbox } from '@figuro/chatwoot-sdk';
2-
import { proto } from '@whiskeysockets/baileys';
2+
import { proto } from 'baileys';
33

44
import { InstanceDto } from '../../../../api/dto/instance.dto';
55
import { ChatwootRaw, ContactRaw, MessageRaw } from '../../../../api/models';

src/api/integrations/chatwoot/validate/chatwoot.schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const chatwootSchema: JSONSchema7 = {
3030
url: { type: 'string' },
3131
sign_msg: { type: 'boolean', enum: [true, false] },
3232
sign_delimiter: { type: ['string', 'null'] },
33+
name_inbox: { type: ['string', 'null'] },
3334
reopen_conversation: { type: 'boolean', enum: [true, false] },
3435
conversation_pending: { type: 'boolean', enum: [true, false] },
3536
auto_create: { type: 'boolean', enum: [true, false] },

src/api/services/cache.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BufferJSON } from '@whiskeysockets/baileys';
1+
import { BufferJSON } from 'baileys';
22

33
import { Logger } from '../../config/logger.config';
44
import { ICache } from '../abstract/abstract.cache';

0 commit comments

Comments
 (0)