mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-18 19:32:21 -06:00
fix: chatbot integrations send media and links
This commit is contained in:
@@ -13,7 +13,7 @@ export class EvolutionBotService {
|
||||
private readonly waMonitor: WAMonitoringService,
|
||||
private readonly configService: ConfigService,
|
||||
private readonly prismaRepository: PrismaRepository,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
private readonly logger = new Logger('EvolutionBotService');
|
||||
|
||||
@@ -112,52 +112,97 @@ export class EvolutionBotService {
|
||||
settings: EvolutionBotSetting,
|
||||
message: string,
|
||||
) {
|
||||
const regex = /!?\[(.*?)\]\((.*?)\)/g;
|
||||
const linkRegex = /(!?)\[(.*?)\]\((.*?)\)/g;
|
||||
|
||||
const result = [];
|
||||
let textBuffer = '';
|
||||
let lastIndex = 0;
|
||||
|
||||
let match;
|
||||
while ((match = regex.exec(message)) !== null) {
|
||||
if (match.index > lastIndex) {
|
||||
result.push({ text: message.slice(lastIndex, match.index).trim() });
|
||||
let match: RegExpExecArray | null;
|
||||
|
||||
const getMediaType = (url: string): string | null => {
|
||||
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) {
|
||||
result.push({ text: message.slice(lastIndex).trim() });
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
const remainingText = message.slice(lastIndex);
|
||||
if (remainingText.trim()) {
|
||||
textBuffer += remainingText;
|
||||
}
|
||||
}
|
||||
|
||||
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({
|
||||
where: {
|
||||
id: session.id,
|
||||
@@ -167,12 +212,9 @@ export class EvolutionBotService {
|
||||
awaitUser: true,
|
||||
},
|
||||
});
|
||||
|
||||
sendTelemetry('/message/sendText');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
private async initNewSession(
|
||||
instance: any,
|
||||
remoteJid: string,
|
||||
|
||||
Reference in New Issue
Block a user