From cd2a68775166949274f8c1de99f88f4e33ae0660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9lio=20Matheus?= Date: Thu, 10 Oct 2024 21:51:02 -0300 Subject: [PATCH] feat: add support for splitMessages and timePerChar in EvolutionBotService for WhatsApp message handling --- .../services/evolutionBot.service.ts | 78 ++++++++++--------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/src/api/integrations/chatbot/evolutionBot/services/evolutionBot.service.ts b/src/api/integrations/chatbot/evolutionBot/services/evolutionBot.service.ts index 10903165..8eebcf68 100644 --- a/src/api/integrations/chatbot/evolutionBot/services/evolutionBot.service.ts +++ b/src/api/integrations/chatbot/evolutionBot/services/evolutionBot.service.ts @@ -119,6 +119,7 @@ export class EvolutionBotService { let match: RegExpExecArray | null; + const getMediaType = (url: string): string | null => { const extension = url.split('.').pop()?.toLowerCase(); const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']; @@ -189,45 +190,52 @@ export class EvolutionBotService { } } - if (textBuffer.trim()) { + const splitMessages = settings.splitMessages ?? false; + const timePerChar = settings.timePerChar ?? 0; + const minDelay = 1000; + const maxDelay = 20000; + if (splitMessages) { const multipleMessages = textBuffer.trim().split("\n\n"); - - if (multipleMessages) { - for (let index = 0; index < multipleMessages.length; index++) { - const message = multipleMessages[index]; - - const delayPerChar = 200 - const minDelay = 1000 - const maxDelay = 20000 - const delay = Math.min(Math.max(message.length * delayPerChar, minDelay), maxDelay); - - - - if (instance.integration === Integration.WHATSAPP_BAILEYS) { - await instance.client.presenceSubscribe(remoteJid); - await instance.client.sendPresenceUpdate('composing', remoteJid); - } - - await new Promise((resolve) => { - setTimeout(async () => { - await instance.textMessage( - { - number: remoteJid.split('@')[0], - delay: settings?.delayMessage || 1000, - text: message, - }, - false - ); - resolve(); - }, delay); - }); - - if (instance.integration === Integration.WHATSAPP_BAILEYS) { - await instance.client.sendPresenceUpdate('paused', remoteJid); - } + + for (let index = 0; index < multipleMessages.length; index++) { + const message = multipleMessages[index]; + + const delay = Math.min(Math.max(message.length * timePerChar, minDelay), maxDelay); + + if (instance.integration === Integration.WHATSAPP_BAILEYS) { + await instance.client.presenceSubscribe(remoteJid); + await instance.client.sendPresenceUpdate('composing', remoteJid); + } + + await new Promise((resolve) => { + setTimeout(async () => { + await instance.textMessage( + { + number: remoteJid.split('@')[0], + delay: settings?.delayMessage || 1000, + text: message, + }, + false + ); + resolve(); + }, delay); + }); + + if (instance.integration === Integration.WHATSAPP_BAILEYS) { + await instance.client.sendPresenceUpdate('paused', remoteJid); } } + } else { + + await instance.textMessage( + { + number: remoteJid.split('@')[0], + delay: settings?.delayMessage || 1000, + text: textBuffer.trim(), + }, + false + ); } sendTelemetry('/message/sendText');