Skip to content

Commit 59fac8b

Browse files
authored
Merge branch 'EvolutionAPI:v2.0.0' into v2.0.0
2 parents 68b5d8d + 4ca141b commit 59fac8b

File tree

16 files changed

+594
-288
lines changed

16 files changed

+594
-288
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# 2.1.1 (develop)
1+
# 2.1.1 (2024-09-22 10:31)
22

33
### Features
44

55
* Define a global proxy to be used if the instance does not have one
66
* Save is on whatsapp on the database
77
* Add headers to the instance's webhook registration
88
* Debounce message break is now "\n" instead of white space
9+
* Single view messages are now supported in chatwoot
10+
* Chatbots can now send any type of media
911

1012
### Fixed
1113

@@ -19,6 +21,11 @@
1921
* It is now possible to send images via the Evolution Channel
2022
* Removed "version" from docker-compose as it is obsolete (https://dev.to/ajeetraina/do-we-still-use-version-in-compose-3inp)
2123
* Fixed typebot ignoreJids being used only from default settings
24+
* Fixed Chatwoot inbox creation on save
25+
* Changed axios timeout for manager requests for 30s
26+
* Update in Baileys version that fixes timeout when updating profile picture
27+
* Fixed issue when sending links in markdown by chatbots like Dify
28+
* Fixed issue with chatbots not respecting settings
2229

2330
# 2.1.0 (2024-08-26 15:33)
2431

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM node:20-alpine AS builder
33
RUN apk update && \
44
apk add git ffmpeg wget curl bash
55

6-
LABEL version="2.1.0" description="Api to control whatsapp features through http requests."
6+
LABEL version="2.1.1" description="Api to control whatsapp features through http requests."
77
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
88
LABEL contact="contato@atendai.com"
99

Lines changed: 74 additions & 74 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manager/dist/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<link rel="icon" type="image/png" href="/assets/images/evolution-logo.png" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Evolution Manager</title>
8-
<script type="module" crossorigin src="/assets/index-DOHK1pp9.js"></script>
8+
<script type="module" crossorigin src="/assets/index-Do1bGWiz.js"></script>
99
<link rel="stylesheet" crossorigin href="/assets/index-DNOCacL_.css">
1010
</head>
1111
<body>

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"dayjs": "^1.11.7",
6464
"dotenv": "^16.4.5",
6565
"eventemitter2": "^6.4.9",
66-
"evolution-manager-v2": "^0.0.2",
6766
"exiftool-vendored": "^22.0.0",
6867
"express": "^4.18.2",
6968
"express-async-errors": "^3.1.1",

src/api/integrations/channel/meta/whatsapp.business.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ export class BusinessStartupService extends ChannelStartupService {
333333
const mediaType = message.messages[0].document
334334
? 'document'
335335
: message.messages[0].image
336-
? 'image'
337-
: message.messages[0].audio
338-
? 'audio'
339-
: 'video';
336+
? 'image'
337+
: message.messages[0].audio
338+
? 'audio'
339+
: 'video';
340340

341341
const mimetype = result.headers['content-type'];
342342

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -736,18 +736,23 @@ export class BaileysStartupService extends ChannelStartupService {
736736
},
737737
});
738738

739-
const instance = { instanceName: this.instance.name, instanceId: this.instance.id };
739+
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled) {
740+
const instance = { instanceName: this.instance.name, instanceId: this.instance.id };
740741

741-
const findParticipant = await this.chatwootService.findContact(instance, contact.remoteJid.split('@')[0]);
742+
const findParticipant = await this.chatwootService.findContact(
743+
instance,
744+
contact.remoteJid.split('@')[0],
745+
);
742746

743-
if (!findParticipant) {
744-
return;
745-
}
747+
if (!findParticipant) {
748+
return;
749+
}
746750

747-
this.chatwootService.updateContact(instance, findParticipant.id, {
748-
name: contact.pushName,
749-
avatar_url: contact.profilePicUrl,
750-
});
751+
this.chatwootService.updateContact(instance, findParticipant.id, {
752+
name: contact.pushName,
753+
avatar_url: contact.profilePicUrl,
754+
});
755+
}
751756

752757
return update;
753758
}),

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export class ChatwootService {
310310
avatar_url: avatar_url,
311311
};
312312

313-
if (jid.includes('@')) {
313+
if ((jid && jid.includes('@')) || !jid) {
314314
data['phone_number'] = `+${phoneNumber}`;
315315
}
316316
} else {
@@ -369,6 +369,10 @@ export class ChatwootService {
369369

370370
public async addLabelToContact(nameInbox: string, contactId: number) {
371371
try {
372+
const uri = this.configService.get<Chatwoot>('CHATWOOT').IMPORT.DATABASE.CONNECTION.URI;
373+
374+
if (!uri) return false;
375+
372376
const sqlTags = `SELECT id FROM tags WHERE name = '${nameInbox}' LIMIT 1`;
373377

374378
const tagData = (await this.pgClient.query(sqlTags))?.rows[0];
@@ -1138,7 +1142,8 @@ export class ChatwootService {
11381142
return { message: 'bot' };
11391143
}
11401144

1141-
const chatId = body.conversation.meta.sender?.identifier;
1145+
const chatId =
1146+
body.conversation.meta.sender?.identifier || body.conversation.meta.sender?.phone_number.replace('+', '');
11421147
// Chatwoot to Whatsapp
11431148
const messageReceived = body.content
11441149
? body.content
@@ -1527,6 +1532,7 @@ export class ChatwootService {
15271532
'audioMessage',
15281533
'videoMessage',
15291534
'stickerMessage',
1535+
'viewOnceMessageV2'
15301536
];
15311537

15321538
const messageKeys = Object.keys(message);
@@ -1580,6 +1586,8 @@ export class ChatwootService {
15801586
liveLocationMessage: msg.liveLocationMessage,
15811587
listMessage: msg.listMessage,
15821588
listResponseMessage: msg.listResponseMessage,
1589+
viewOnceMessageV2: msg?.message?.viewOnceMessageV2?.message?.imageMessage?.url || msg?.message?.viewOnceMessageV2?.message?.videoMessage?.url || msg?.message?.viewOnceMessageV2?.message?.audioMessage?.url,
1590+
15831591
};
15841592

15851593
return types;

src/api/integrations/chatbot/dify/controllers/dify.controller.ts

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -736,17 +736,25 @@ export class DifyController extends ChatbotController implements ChatbotControll
736736

737737
if (!findBot) return;
738738

739-
let listeningFromMe = findBot.listeningFromMe;
740-
let stopBotFromMe = findBot.stopBotFromMe;
741-
let debounceTime = findBot.debounceTime;
742-
743-
if (!listeningFromMe || !stopBotFromMe || !debounceTime) {
744-
if (!listeningFromMe) listeningFromMe = settings.listeningFromMe;
745-
746-
if (!stopBotFromMe) stopBotFromMe = settings.stopBotFromMe;
747-
748-
if (!debounceTime) debounceTime = settings.debounceTime;
749-
}
739+
let expire = findBot?.expire;
740+
let keywordFinish = findBot?.keywordFinish;
741+
let delayMessage = findBot?.delayMessage;
742+
let unknownMessage = findBot?.unknownMessage;
743+
let listeningFromMe = findBot?.listeningFromMe;
744+
let stopBotFromMe = findBot?.stopBotFromMe;
745+
let keepOpen = findBot?.keepOpen;
746+
let debounceTime = findBot?.debounceTime;
747+
let ignoreJids = findBot?.ignoreJids;
748+
749+
if (!expire) expire = settings.expire;
750+
if (!keywordFinish) keywordFinish = settings.keywordFinish;
751+
if (!delayMessage) delayMessage = settings.delayMessage;
752+
if (!unknownMessage) unknownMessage = settings.unknownMessage;
753+
if (!listeningFromMe) listeningFromMe = settings.listeningFromMe;
754+
if (!stopBotFromMe) stopBotFromMe = settings.stopBotFromMe;
755+
if (!keepOpen) keepOpen = settings.keepOpen;
756+
if (!debounceTime) debounceTime = settings.debounceTime;
757+
if (!ignoreJids) ignoreJids = settings.ignoreJids;
750758

751759
const key = msg.key as {
752760
id: string;
@@ -782,7 +790,18 @@ export class DifyController extends ChatbotController implements ChatbotControll
782790
remoteJid,
783791
findBot,
784792
session,
785-
settings,
793+
{
794+
...settings,
795+
expire,
796+
keywordFinish,
797+
delayMessage,
798+
unknownMessage,
799+
listeningFromMe,
800+
stopBotFromMe,
801+
keepOpen,
802+
debounceTime,
803+
ignoreJids,
804+
},
786805
debouncedContent,
787806
msg?.pushName,
788807
);
@@ -793,7 +812,18 @@ export class DifyController extends ChatbotController implements ChatbotControll
793812
remoteJid,
794813
findBot,
795814
session,
796-
settings,
815+
{
816+
...settings,
817+
expire,
818+
keywordFinish,
819+
delayMessage,
820+
unknownMessage,
821+
listeningFromMe,
822+
stopBotFromMe,
823+
keepOpen,
824+
debounceTime,
825+
ignoreJids,
826+
},
797827
content,
798828
msg?.pushName,
799829
);

src/api/integrations/chatbot/dify/services/dify.service.ts

Lines changed: 78 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
12
import { InstanceDto } from '@api/dto/instance.dto';
23
import { PrismaRepository } from '@api/repository/repository.service';
34
import { WAMonitoringService } from '@api/services/monitor.service';
@@ -238,14 +239,14 @@ export class DifyService {
238239
if (data.trim() === '' || !data.startsWith('{')) {
239240
return;
240241
}
241-
242+
242243
try {
243244
const events = data.split('\n').filter((line) => line.trim() !== '');
244-
245+
245246
for (const eventString of events) {
246247
if (eventString.trim().startsWith('{')) {
247248
const event = JSON.parse(eventString);
248-
249+
249250
if (event?.event === 'agent_message') {
250251
console.log('event:', event);
251252
conversationId = conversationId ?? event?.conversation_id;
@@ -350,52 +351,93 @@ export class DifyService {
350351
}
351352

352353
private async sendMessageWhatsApp(instance: any, remoteJid: string, message: string, settings: DifySetting) {
353-
const regex = /!?\[(.*?)\]\((.*?)\)/g;
354+
const linkRegex = /(!?)\[(.*?)\]\((.*?)\)/g;
354355

355-
const result = [];
356+
let textBuffer = '';
356357
let lastIndex = 0;
357358

358-
let match;
359-
while ((match = regex.exec(message)) !== null) {
360-
if (match.index > lastIndex) {
361-
result.push({ text: message.slice(lastIndex, match.index).trim() });
359+
let match: RegExpExecArray | null;
360+
361+
const getMediaType = (url: string): string | null => {
362+
const extension = url.split('.').pop()?.toLowerCase();
363+
const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
364+
const audioExtensions = ['mp3', 'wav', 'aac', 'ogg'];
365+
const videoExtensions = ['mp4', 'avi', 'mkv', 'mov'];
366+
const documentExtensions = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt'];
367+
368+
if (imageExtensions.includes(extension || '')) return 'image';
369+
if (audioExtensions.includes(extension || '')) return 'audio';
370+
if (videoExtensions.includes(extension || '')) return 'video';
371+
if (documentExtensions.includes(extension || '')) return 'document';
372+
return null;
373+
};
374+
375+
while ((match = linkRegex.exec(message)) !== null) {
376+
const [fullMatch, exclMark, altText, url] = match;
377+
const mediaType = getMediaType(url);
378+
379+
const beforeText = message.slice(lastIndex, match.index);
380+
if (beforeText) {
381+
textBuffer += beforeText;
362382
}
363383

364-
result.push({ caption: match[1], url: match[2] });
365-
366-
lastIndex = regex.lastIndex;
367-
}
368-
369-
if (lastIndex < message.length) {
370-
result.push({ text: message.slice(lastIndex).trim() });
371-
}
384+
if (mediaType) {
385+
if (textBuffer.trim()) {
386+
await instance.textMessage(
387+
{
388+
number: remoteJid.split('@')[0],
389+
delay: settings?.delayMessage || 1000,
390+
text: textBuffer.trim(),
391+
},
392+
false,
393+
);
394+
textBuffer = '';
395+
}
372396

373-
for (const item of result) {
374-
if (item.text) {
375-
await instance.textMessage(
376-
{
397+
if (mediaType === 'audio') {
398+
await instance.audioWhatsapp({
377399
number: remoteJid.split('@')[0],
378400
delay: settings?.delayMessage || 1000,
379-
text: item.text,
380-
},
381-
false,
382-
);
401+
audio: url,
402+
caption: altText,
403+
});
404+
} else {
405+
await instance.mediaMessage(
406+
{
407+
number: remoteJid.split('@')[0],
408+
delay: settings?.delayMessage || 1000,
409+
mediatype: mediaType,
410+
media: url,
411+
caption: altText,
412+
},
413+
false,
414+
);
415+
}
416+
} else {
417+
textBuffer += `[${altText}](${url})`;
383418
}
384419

385-
if (item.url) {
386-
await instance.mediaMessage(
387-
{
388-
number: remoteJid.split('@')[0],
389-
delay: settings?.delayMessage || 1000,
390-
mediatype: 'image',
391-
media: item.url,
392-
caption: item.caption,
393-
},
394-
false,
395-
);
420+
lastIndex = linkRegex.lastIndex;
421+
}
422+
423+
if (lastIndex < message.length) {
424+
const remainingText = message.slice(lastIndex);
425+
if (remainingText.trim()) {
426+
textBuffer += remainingText;
396427
}
397428
}
398429

430+
if (textBuffer.trim()) {
431+
await instance.textMessage(
432+
{
433+
number: remoteJid.split('@')[0],
434+
delay: settings?.delayMessage || 1000,
435+
text: textBuffer.trim(),
436+
},
437+
false,
438+
);
439+
}
440+
399441
sendTelemetry('/message/sendText');
400442
}
401443

0 commit comments

Comments
 (0)