mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 12:12:55 -06:00
fix: chatbot integrations send media and links
This commit is contained in:
parent
3527e662e1
commit
5ebebbf211
@ -63,7 +63,6 @@
|
|||||||
"dayjs": "^1.11.7",
|
"dayjs": "^1.11.7",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"eventemitter2": "^6.4.9",
|
"eventemitter2": "^6.4.9",
|
||||||
"evolution-manager-v2": "^0.0.2",
|
|
||||||
"exiftool-vendored": "^22.0.0",
|
"exiftool-vendored": "^22.0.0",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"express-async-errors": "^3.1.1",
|
"express-async-errors": "^3.1.1",
|
||||||
|
@ -739,19 +739,24 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const instance = { instanceName: this.instance.name, instanceId: this.instance.id };
|
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled) {
|
||||||
|
const instance = { instanceName: this.instance.name, instanceId: this.instance.id };
|
||||||
|
|
||||||
const findParticipant = await this.chatwootService.findContact(instance, contact.remoteJid.split('@')[0]);
|
const findParticipant = await this.chatwootService.findContact(
|
||||||
|
instance,
|
||||||
|
contact.remoteJid.split('@')[0],
|
||||||
|
);
|
||||||
|
|
||||||
if (!findParticipant) {
|
if (!findParticipant) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.chatwootService.updateContact(instance, findParticipant.id, {
|
||||||
|
name: contact.pushName,
|
||||||
|
avatar_url: contact.profilePicUrl,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.chatwootService.updateContact(instance, findParticipant.id, {
|
|
||||||
name: contact.pushName,
|
|
||||||
avatar_url: contact.profilePicUrl,
|
|
||||||
});
|
|
||||||
|
|
||||||
return update;
|
return update;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@ -14,7 +14,7 @@ export class DifyService {
|
|||||||
private readonly waMonitor: WAMonitoringService,
|
private readonly waMonitor: WAMonitoringService,
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly prismaRepository: PrismaRepository,
|
private readonly prismaRepository: PrismaRepository,
|
||||||
) {}
|
) { }
|
||||||
|
|
||||||
private readonly logger = new Logger('DifyService');
|
private readonly logger = new Logger('DifyService');
|
||||||
|
|
||||||
@ -349,51 +349,99 @@ export class DifyService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async sendMessageWhatsApp(instance: any, remoteJid: string, message: string, settings: DifySetting) {
|
private async sendMessageWhatsApp(
|
||||||
const regex = /!?\[(.*?)\]\((.*?)\)/g;
|
instance: any,
|
||||||
|
remoteJid: string,
|
||||||
|
message: string,
|
||||||
|
settings: DifySetting
|
||||||
|
) {
|
||||||
|
const linkRegex = /(!?)\[(.*?)\]\((.*?)\)/g;
|
||||||
|
|
||||||
const result = [];
|
let textBuffer = '';
|
||||||
let lastIndex = 0;
|
let lastIndex = 0;
|
||||||
|
|
||||||
let match;
|
let match: RegExpExecArray | null;
|
||||||
while ((match = regex.exec(message)) !== null) {
|
|
||||||
if (match.index > lastIndex) {
|
const getMediaType = (url: string): string | null => {
|
||||||
result.push({ text: message.slice(lastIndex, match.index).trim() });
|
const extension = url.split('.').pop()?.toLowerCase();
|
||||||
|
const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
|
||||||
|
const audioExtensions = ['mp3', 'wav', 'aac', 'ogg'];
|
||||||
|
const videoExtensions = ['mp4', 'avi', 'mkv', 'mov'];
|
||||||
|
const documentExtensions = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt'];
|
||||||
|
|
||||||
|
if (imageExtensions.includes(extension || '')) return 'image';
|
||||||
|
if (audioExtensions.includes(extension || '')) return 'audio';
|
||||||
|
if (videoExtensions.includes(extension || '')) return 'video';
|
||||||
|
if (documentExtensions.includes(extension || '')) return 'document';
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
while ((match = linkRegex.exec(message)) !== null) {
|
||||||
|
const [fullMatch, exclMark, altText, url] = match;
|
||||||
|
const mediaType = getMediaType(url);
|
||||||
|
|
||||||
|
const beforeText = message.slice(lastIndex, match.index);
|
||||||
|
if (beforeText) {
|
||||||
|
textBuffer += beforeText;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push({ caption: match[1], url: match[2] });
|
if (mediaType) {
|
||||||
|
if (textBuffer.trim()) {
|
||||||
|
await instance.textMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
text: textBuffer.trim(),
|
||||||
|
},
|
||||||
|
false
|
||||||
|
);
|
||||||
|
textBuffer = '';
|
||||||
|
}
|
||||||
|
|
||||||
lastIndex = regex.lastIndex;
|
if (mediaType === 'audio') {
|
||||||
|
await instance.audioWhatsapp(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
audio: url,
|
||||||
|
caption: altText,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await instance.mediaMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
mediatype: mediaType,
|
||||||
|
media: url,
|
||||||
|
caption: altText,
|
||||||
|
},
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
textBuffer += `[${altText}](${url})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastIndex = linkRegex.lastIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastIndex < message.length) {
|
if (lastIndex < message.length) {
|
||||||
result.push({ text: message.slice(lastIndex).trim() });
|
const remainingText = message.slice(lastIndex);
|
||||||
|
if (remainingText.trim()) {
|
||||||
|
textBuffer += remainingText;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const item of result) {
|
if (textBuffer.trim()) {
|
||||||
if (item.text) {
|
await instance.textMessage(
|
||||||
await instance.textMessage(
|
{
|
||||||
{
|
number: remoteJid.split('@')[0],
|
||||||
number: remoteJid.split('@')[0],
|
delay: settings?.delayMessage || 1000,
|
||||||
delay: settings?.delayMessage || 1000,
|
text: textBuffer.trim(),
|
||||||
text: item.text,
|
},
|
||||||
},
|
false
|
||||||
false,
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.url) {
|
|
||||||
await instance.mediaMessage(
|
|
||||||
{
|
|
||||||
number: remoteJid.split('@')[0],
|
|
||||||
delay: settings?.delayMessage || 1000,
|
|
||||||
mediatype: 'image',
|
|
||||||
media: item.url,
|
|
||||||
caption: item.caption,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendTelemetry('/message/sendText');
|
sendTelemetry('/message/sendText');
|
||||||
|
@ -13,7 +13,7 @@ export class EvolutionBotService {
|
|||||||
private readonly waMonitor: WAMonitoringService,
|
private readonly waMonitor: WAMonitoringService,
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly prismaRepository: PrismaRepository,
|
private readonly prismaRepository: PrismaRepository,
|
||||||
) {}
|
) { }
|
||||||
|
|
||||||
private readonly logger = new Logger('EvolutionBotService');
|
private readonly logger = new Logger('EvolutionBotService');
|
||||||
|
|
||||||
@ -112,52 +112,97 @@ export class EvolutionBotService {
|
|||||||
settings: EvolutionBotSetting,
|
settings: EvolutionBotSetting,
|
||||||
message: string,
|
message: string,
|
||||||
) {
|
) {
|
||||||
const regex = /!?\[(.*?)\]\((.*?)\)/g;
|
const linkRegex = /(!?)\[(.*?)\]\((.*?)\)/g;
|
||||||
|
|
||||||
const result = [];
|
let textBuffer = '';
|
||||||
let lastIndex = 0;
|
let lastIndex = 0;
|
||||||
|
|
||||||
let match;
|
let match: RegExpExecArray | null;
|
||||||
while ((match = regex.exec(message)) !== null) {
|
|
||||||
if (match.index > lastIndex) {
|
const getMediaType = (url: string): string | null => {
|
||||||
result.push({ text: message.slice(lastIndex, match.index).trim() });
|
const extension = url.split('.').pop()?.toLowerCase();
|
||||||
|
const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
|
||||||
|
const audioExtensions = ['mp3', 'wav', 'aac', 'ogg'];
|
||||||
|
const videoExtensions = ['mp4', 'avi', 'mkv', 'mov'];
|
||||||
|
const documentExtensions = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt'];
|
||||||
|
|
||||||
|
if (imageExtensions.includes(extension || '')) return 'image';
|
||||||
|
if (audioExtensions.includes(extension || '')) return 'audio';
|
||||||
|
if (videoExtensions.includes(extension || '')) return 'video';
|
||||||
|
if (documentExtensions.includes(extension || '')) return 'document';
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
while ((match = linkRegex.exec(message)) !== null) {
|
||||||
|
const [fullMatch, exclMark, altText, url] = match;
|
||||||
|
const mediaType = getMediaType(url);
|
||||||
|
|
||||||
|
const beforeText = message.slice(lastIndex, match.index);
|
||||||
|
if (beforeText) {
|
||||||
|
textBuffer += beforeText;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push({ caption: match[1], url: match[2] });
|
if (mediaType) {
|
||||||
|
if (textBuffer.trim()) {
|
||||||
|
await instance.textMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
text: textBuffer.trim(),
|
||||||
|
},
|
||||||
|
false
|
||||||
|
);
|
||||||
|
textBuffer = '';
|
||||||
|
}
|
||||||
|
|
||||||
lastIndex = regex.lastIndex;
|
if (mediaType === 'audio') {
|
||||||
|
await instance.audioWhatsapp(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
audio: url,
|
||||||
|
caption: altText,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await instance.mediaMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
mediatype: mediaType,
|
||||||
|
media: url,
|
||||||
|
caption: altText,
|
||||||
|
},
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
textBuffer += `[${altText}](${url})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastIndex = linkRegex.lastIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastIndex < message.length) {
|
if (lastIndex < message.length) {
|
||||||
result.push({ text: message.slice(lastIndex).trim() });
|
const remainingText = message.slice(lastIndex);
|
||||||
}
|
if (remainingText.trim()) {
|
||||||
|
textBuffer += remainingText;
|
||||||
for (const item of result) {
|
|
||||||
if (item.text) {
|
|
||||||
await instance.textMessage(
|
|
||||||
{
|
|
||||||
number: remoteJid.split('@')[0],
|
|
||||||
delay: settings?.delayMessage || 1000,
|
|
||||||
text: item.text,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.url) {
|
|
||||||
await instance.mediaMessage(
|
|
||||||
{
|
|
||||||
number: remoteJid.split('@')[0],
|
|
||||||
delay: settings?.delayMessage || 1000,
|
|
||||||
mediatype: 'image',
|
|
||||||
media: item.url,
|
|
||||||
caption: item.caption,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (textBuffer.trim()) {
|
||||||
|
await instance.textMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
text: textBuffer.trim(),
|
||||||
|
},
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendTelemetry('/message/sendText');
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
where: {
|
where: {
|
||||||
id: session.id,
|
id: session.id,
|
||||||
@ -167,12 +212,9 @@ export class EvolutionBotService {
|
|||||||
awaitUser: true,
|
awaitUser: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
sendTelemetry('/message/sendText');
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private async initNewSession(
|
private async initNewSession(
|
||||||
instance: any,
|
instance: any,
|
||||||
remoteJid: string,
|
remoteJid: string,
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||||
import { InstanceDto } from '@api/dto/instance.dto';
|
import { InstanceDto } from '@api/dto/instance.dto';
|
||||||
import { PrismaRepository } from '@api/repository/repository.service';
|
import { PrismaRepository } from '@api/repository/repository.service';
|
||||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||||
@ -111,52 +112,95 @@ export class FlowiseService {
|
|||||||
settings: FlowiseSetting,
|
settings: FlowiseSetting,
|
||||||
message: string,
|
message: string,
|
||||||
) {
|
) {
|
||||||
const regex = /!?\[(.*?)\]\((.*?)\)/g;
|
const linkRegex = /(!?)\[(.*?)\]\((.*?)\)/g;
|
||||||
|
|
||||||
const result = [];
|
let textBuffer = '';
|
||||||
let lastIndex = 0;
|
let lastIndex = 0;
|
||||||
|
|
||||||
let match;
|
let match: RegExpExecArray | null;
|
||||||
while ((match = regex.exec(message)) !== null) {
|
|
||||||
if (match.index > lastIndex) {
|
const getMediaType = (url: string): string | null => {
|
||||||
result.push({ text: message.slice(lastIndex, match.index).trim() });
|
const extension = url.split('.').pop()?.toLowerCase();
|
||||||
|
const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
|
||||||
|
const audioExtensions = ['mp3', 'wav', 'aac', 'ogg'];
|
||||||
|
const videoExtensions = ['mp4', 'avi', 'mkv', 'mov'];
|
||||||
|
const documentExtensions = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt'];
|
||||||
|
|
||||||
|
if (imageExtensions.includes(extension || '')) return 'image';
|
||||||
|
if (audioExtensions.includes(extension || '')) return 'audio';
|
||||||
|
if (videoExtensions.includes(extension || '')) return 'video';
|
||||||
|
if (documentExtensions.includes(extension || '')) return 'document';
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
while ((match = linkRegex.exec(message)) !== null) {
|
||||||
|
const [fullMatch, exclMark, altText, url] = match;
|
||||||
|
const mediaType = getMediaType(url);
|
||||||
|
|
||||||
|
const beforeText = message.slice(lastIndex, match.index);
|
||||||
|
if (beforeText) {
|
||||||
|
textBuffer += beforeText;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push({ caption: match[1], url: match[2] });
|
if (mediaType) {
|
||||||
|
if (textBuffer.trim()) {
|
||||||
|
await instance.textMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
text: textBuffer.trim(),
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
textBuffer = '';
|
||||||
|
}
|
||||||
|
|
||||||
lastIndex = regex.lastIndex;
|
if (mediaType === 'audio') {
|
||||||
|
await instance.audioWhatsapp({
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
audio: url,
|
||||||
|
caption: altText,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await instance.mediaMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
mediatype: mediaType,
|
||||||
|
media: url,
|
||||||
|
caption: altText,
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
textBuffer += `[${altText}](${url})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastIndex = linkRegex.lastIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastIndex < message.length) {
|
if (lastIndex < message.length) {
|
||||||
result.push({ text: message.slice(lastIndex).trim() });
|
const remainingText = message.slice(lastIndex);
|
||||||
}
|
if (remainingText.trim()) {
|
||||||
|
textBuffer += remainingText;
|
||||||
for (const item of result) {
|
|
||||||
if (item.text) {
|
|
||||||
await instance.textMessage(
|
|
||||||
{
|
|
||||||
number: remoteJid.split('@')[0],
|
|
||||||
delay: settings?.delayMessage || 1000,
|
|
||||||
text: item.text,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.url) {
|
|
||||||
await instance.mediaMessage(
|
|
||||||
{
|
|
||||||
number: remoteJid.split('@')[0],
|
|
||||||
delay: settings?.delayMessage || 1000,
|
|
||||||
mediatype: 'image',
|
|
||||||
media: item.url,
|
|
||||||
caption: item.caption,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (textBuffer.trim()) {
|
||||||
|
await instance.textMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
text: textBuffer.trim(),
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendTelemetry('/message/sendText');
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
where: {
|
where: {
|
||||||
id: session.id,
|
id: session.id,
|
||||||
@ -167,8 +211,6 @@ export class FlowiseService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
sendTelemetry('/message/sendText');
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||||
import { InstanceDto } from '@api/dto/instance.dto';
|
import { InstanceDto } from '@api/dto/instance.dto';
|
||||||
import { PrismaRepository } from '@api/repository/repository.service';
|
import { PrismaRepository } from '@api/repository/repository.service';
|
||||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||||
@ -156,52 +157,95 @@ export class OpenaiService {
|
|||||||
settings: OpenaiSetting,
|
settings: OpenaiSetting,
|
||||||
message: string,
|
message: string,
|
||||||
) {
|
) {
|
||||||
const regex = /!?\[(.*?)\]\((.*?)\)/g;
|
const linkRegex = /(!?)\[(.*?)\]\((.*?)\)/g;
|
||||||
|
|
||||||
const result = [];
|
let textBuffer = '';
|
||||||
let lastIndex = 0;
|
let lastIndex = 0;
|
||||||
|
|
||||||
let match;
|
let match: RegExpExecArray | null;
|
||||||
while ((match = regex.exec(message)) !== null) {
|
|
||||||
if (match.index > lastIndex) {
|
const getMediaType = (url: string): string | null => {
|
||||||
result.push({ text: message.slice(lastIndex, match.index).trim() });
|
const extension = url.split('.').pop()?.toLowerCase();
|
||||||
|
const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
|
||||||
|
const audioExtensions = ['mp3', 'wav', 'aac', 'ogg'];
|
||||||
|
const videoExtensions = ['mp4', 'avi', 'mkv', 'mov'];
|
||||||
|
const documentExtensions = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt'];
|
||||||
|
|
||||||
|
if (imageExtensions.includes(extension || '')) return 'image';
|
||||||
|
if (audioExtensions.includes(extension || '')) return 'audio';
|
||||||
|
if (videoExtensions.includes(extension || '')) return 'video';
|
||||||
|
if (documentExtensions.includes(extension || '')) return 'document';
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
while ((match = linkRegex.exec(message)) !== null) {
|
||||||
|
const [fullMatch, exclMark, altText, url] = match;
|
||||||
|
const mediaType = getMediaType(url);
|
||||||
|
|
||||||
|
const beforeText = message.slice(lastIndex, match.index);
|
||||||
|
if (beforeText) {
|
||||||
|
textBuffer += beforeText;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push({ caption: match[1], url: match[2] });
|
if (mediaType) {
|
||||||
|
if (textBuffer.trim()) {
|
||||||
|
await instance.textMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
text: textBuffer.trim(),
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
textBuffer = '';
|
||||||
|
}
|
||||||
|
|
||||||
lastIndex = regex.lastIndex;
|
if (mediaType === 'audio') {
|
||||||
|
await instance.audioWhatsapp({
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
audio: url,
|
||||||
|
caption: altText,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await instance.mediaMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
mediatype: mediaType,
|
||||||
|
media: url,
|
||||||
|
caption: altText,
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
textBuffer += `[${altText}](${url})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastIndex = linkRegex.lastIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastIndex < message.length) {
|
if (lastIndex < message.length) {
|
||||||
result.push({ text: message.slice(lastIndex).trim() });
|
const remainingText = message.slice(lastIndex);
|
||||||
}
|
if (remainingText.trim()) {
|
||||||
|
textBuffer += remainingText;
|
||||||
for (const item of result) {
|
|
||||||
if (item.text) {
|
|
||||||
await instance.textMessage(
|
|
||||||
{
|
|
||||||
number: remoteJid.split('@')[0],
|
|
||||||
delay: settings?.delayMessage || 1000,
|
|
||||||
text: item.text,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.url) {
|
|
||||||
await instance.mediaMessage(
|
|
||||||
{
|
|
||||||
number: remoteJid.split('@')[0],
|
|
||||||
delay: settings?.delayMessage || 1000,
|
|
||||||
mediatype: 'image',
|
|
||||||
media: item.url,
|
|
||||||
caption: item.caption,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (textBuffer.trim()) {
|
||||||
|
await instance.textMessage(
|
||||||
|
{
|
||||||
|
number: remoteJid.split('@')[0],
|
||||||
|
delay: settings?.delayMessage || 1000,
|
||||||
|
text: textBuffer.trim(),
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendTelemetry('/message/sendText');
|
||||||
|
|
||||||
await this.prismaRepository.integrationSession.update({
|
await this.prismaRepository.integrationSession.update({
|
||||||
where: {
|
where: {
|
||||||
id: session.id,
|
id: session.id,
|
||||||
@ -211,8 +255,6 @@ export class OpenaiService {
|
|||||||
awaitUser: true,
|
awaitUser: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
sendTelemetry('/message/sendText');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createAssistantNewSession(instance: InstanceDto, data: any) {
|
public async createAssistantNewSession(instance: InstanceDto, data: any) {
|
||||||
|
Loading…
Reference in New Issue
Block a user