mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-26 18:38:39 -06:00
Split messages on double line breaks (\n\n) and implement typing delay in WhatsApp
This change modifies the Evolution Bot integration to: - Split messages on double newline characters (\n\n) and send each part as a separate message in WhatsApp. - Add a typing delay before sending messages to simulate a more natural, human-like typing experience. These updates enhance user interaction by breaking long messages into manageable parts and making bot responses feel more conversational.
This commit is contained in:
parent
c29d20ea40
commit
e47567b819
@ -190,14 +190,44 @@ export class EvolutionBotService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (textBuffer.trim()) {
|
if (textBuffer.trim()) {
|
||||||
|
|
||||||
|
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<void>((resolve) => {
|
||||||
|
setTimeout(async () => {
|
||||||
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: message,
|
||||||
},
|
},
|
||||||
false,
|
false
|
||||||
);
|
);
|
||||||
|
resolve();
|
||||||
|
}, delay);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||||
|
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendTelemetry('/message/sendText');
|
sendTelemetry('/message/sendText');
|
||||||
|
Loading…
Reference in New Issue
Block a user