mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-20 04:12:23 -06:00
refactor(chatbot): centralize split logic and ensure linkPreview consistency
- Centralize double-line-break message splitting logic into dedicated helper methods - Add targeted debug logs for better observability without clutter - Ensure linkPreview parameter is consistently passed across all chatbot services - Extract splitMessageByDoubleLineBreaks() and sendSingleMessage() helpers - Update all chatbot services to explicitly pass linkPreview: true - Improve code testability and maintainability Services updated: - BaseChatbotService: Refactored split logic and added debug logs - TypebotService: Added linkPreview parameter to all sendMessageWhatsApp calls - OpenAIService: Added linkPreview parameter to all sendMessageWhatsApp calls - N8nService: Added linkPreview parameter to sendMessageWhatsApp call - FlowiseService: Added linkPreview parameter to sendMessageWhatsApp call - EvoaiService: Added linkPreview parameter to sendMessageWhatsApp call - DifyService: Added linkPreview parameter to all sendMessageWhatsApp calls
This commit is contained in:
@@ -257,6 +257,55 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Split message by double line breaks and return array of message parts
|
||||
*/
|
||||
private splitMessageByDoubleLineBreaks(message: string): string[] {
|
||||
return message.split('\n\n').filter((part) => part.trim().length > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a single message with proper typing indicators and delays
|
||||
*/
|
||||
private async sendSingleMessage(
|
||||
instance: any,
|
||||
remoteJid: string,
|
||||
message: string,
|
||||
settings: any,
|
||||
linkPreview: boolean = true,
|
||||
): Promise<void> {
|
||||
const timePerChar = settings?.timePerChar ?? 0;
|
||||
const minDelay = 1000;
|
||||
const maxDelay = 20000;
|
||||
const delay = Math.min(Math.max(message.length * timePerChar, minDelay), maxDelay);
|
||||
|
||||
this.logger.debug(`[BaseChatbot] Sending single message with linkPreview: ${linkPreview}`);
|
||||
|
||||
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(
|
||||
{
|
||||
number: remoteJid.split('@')[0],
|
||||
delay: settings?.delayMessage || 1000,
|
||||
text: message,
|
||||
linkPreview,
|
||||
},
|
||||
false,
|
||||
);
|
||||
resolve();
|
||||
}, delay);
|
||||
});
|
||||
|
||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to send formatted text with proper typing indicators and delays
|
||||
*/
|
||||
@@ -268,68 +317,22 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
|
||||
splitMessages: boolean,
|
||||
linkPreview: boolean = true,
|
||||
): Promise<void> {
|
||||
const timePerChar = settings?.timePerChar ?? 0;
|
||||
const minDelay = 1000;
|
||||
const maxDelay = 20000;
|
||||
|
||||
if (splitMessages) {
|
||||
const multipleMessages = text.split('\n\n');
|
||||
for (let index = 0; index < multipleMessages.length; index++) {
|
||||
const message = multipleMessages[index];
|
||||
if (!message.trim()) continue;
|
||||
const messageParts = this.splitMessageByDoubleLineBreaks(text);
|
||||
|
||||
const delay = Math.min(Math.max(message.length * timePerChar, minDelay), maxDelay);
|
||||
this.logger.debug(`[BaseChatbot] Splitting message into ${messageParts.length} parts`);
|
||||
|
||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||
await instance.client.presenceSubscribe(remoteJid);
|
||||
await instance.client.sendPresenceUpdate('composing', remoteJid);
|
||||
}
|
||||
for (let index = 0; index < messageParts.length; index++) {
|
||||
const message = messageParts[index];
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
setTimeout(async () => {
|
||||
await instance.textMessage(
|
||||
{
|
||||
number: remoteJid.split('@')[0],
|
||||
delay: settings?.delayMessage || 1000,
|
||||
text: message,
|
||||
linkPreview,
|
||||
},
|
||||
false,
|
||||
);
|
||||
resolve();
|
||||
}, delay);
|
||||
});
|
||||
|
||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||
}
|
||||
this.logger.debug(`[BaseChatbot] Sending message part ${index + 1}/${messageParts.length}`);
|
||||
await this.sendSingleMessage(instance, remoteJid, message, settings, linkPreview);
|
||||
}
|
||||
|
||||
this.logger.debug(`[BaseChatbot] All message parts sent successfully`);
|
||||
} else {
|
||||
const delay = Math.min(Math.max(text.length * timePerChar, 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(
|
||||
{
|
||||
number: remoteJid.split('@')[0],
|
||||
delay: settings?.delayMessage || 1000,
|
||||
text: text,
|
||||
linkPreview,
|
||||
},
|
||||
false,
|
||||
);
|
||||
resolve();
|
||||
}, delay);
|
||||
});
|
||||
|
||||
if (instance.integration === Integration.WHATSAPP_BAILEYS) {
|
||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||
}
|
||||
this.logger.debug(`[BaseChatbot] Sending single message`);
|
||||
await this.sendSingleMessage(instance, remoteJid, text, settings, linkPreview);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
|
||||
const conversationId = response?.data?.conversation_id;
|
||||
|
||||
if (message) {
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||
}
|
||||
|
||||
await this.prismaRepository.integrationSession.update({
|
||||
@@ -169,7 +169,7 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
|
||||
const conversationId = response?.data?.conversation_id;
|
||||
|
||||
if (message) {
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||
}
|
||||
|
||||
await this.prismaRepository.integrationSession.update({
|
||||
@@ -246,7 +246,7 @@ export class DifyService extends BaseChatbotService<Dify, DifySetting> {
|
||||
await instance.client.sendPresenceUpdate('paused', remoteJid);
|
||||
|
||||
if (answer) {
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, answer, settings);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, answer, settings, true);
|
||||
}
|
||||
|
||||
await this.prismaRepository.integrationSession.update({
|
||||
|
||||
@@ -174,7 +174,7 @@ export class EvoaiService extends BaseChatbotService<Evoai, EvoaiSetting> {
|
||||
this.logger.debug(`[EvoAI] Extracted message to send: ${message}`);
|
||||
|
||||
if (message) {
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
|
||||
@@ -130,7 +130,7 @@ export class FlowiseService extends BaseChatbotService<FlowiseModel> {
|
||||
|
||||
if (message) {
|
||||
// Use the base class method to send the message to WhatsApp
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ export class N8nService extends BaseChatbotService<N8n, N8nSetting> {
|
||||
const message = response?.data?.output || response?.data?.answer;
|
||||
|
||||
// Use base class method instead of custom implementation
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||
|
||||
await this.prismaRepository.integrationSession.update({
|
||||
where: {
|
||||
|
||||
@@ -85,6 +85,7 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
||||
remoteJid,
|
||||
"Sorry, I couldn't transcribe your audio message. Could you please type your message instead?",
|
||||
settings,
|
||||
true,
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -233,7 +234,7 @@ export class OpenaiService extends BaseChatbotService<OpenaiBot, OpenaiSetting>
|
||||
// Send the response
|
||||
if (message) {
|
||||
this.logger.log('Sending message to WhatsApp');
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
|
||||
await this.sendMessageWhatsApp(instance, remoteJid, message, settings, true);
|
||||
} else {
|
||||
this.logger.error('No message to send to WhatsApp');
|
||||
}
|
||||
|
||||
@@ -318,7 +318,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
|
||||
} else if (formattedText.includes('[buttons]')) {
|
||||
await this.processButtonMessage(instance, formattedText, session.remoteJid);
|
||||
} else {
|
||||
await this.sendMessageWhatsApp(instance, session.remoteJid, formattedText, settings);
|
||||
await this.sendMessageWhatsApp(instance, session.remoteJid, formattedText, settings, true);
|
||||
}
|
||||
|
||||
sendTelemetry('/message/sendText');
|
||||
@@ -393,7 +393,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
|
||||
} else if (formattedText.includes('[buttons]')) {
|
||||
await this.processButtonMessage(instance, formattedText, session.remoteJid);
|
||||
} else {
|
||||
await this.sendMessageWhatsApp(instance, session.remoteJid, formattedText, settings);
|
||||
await this.sendMessageWhatsApp(instance, session.remoteJid, formattedText, settings, true);
|
||||
}
|
||||
|
||||
sendTelemetry('/message/sendText');
|
||||
@@ -642,15 +642,21 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
|
||||
|
||||
if (!content) {
|
||||
if (unknownMessage) {
|
||||
await this.sendMessageWhatsApp(waInstance, remoteJid, unknownMessage, {
|
||||
delayMessage,
|
||||
expire,
|
||||
keywordFinish,
|
||||
listeningFromMe,
|
||||
stopBotFromMe,
|
||||
keepOpen,
|
||||
await this.sendMessageWhatsApp(
|
||||
waInstance,
|
||||
remoteJid,
|
||||
unknownMessage,
|
||||
});
|
||||
{
|
||||
delayMessage,
|
||||
expire,
|
||||
keywordFinish,
|
||||
listeningFromMe,
|
||||
stopBotFromMe,
|
||||
keepOpen,
|
||||
unknownMessage,
|
||||
},
|
||||
true,
|
||||
);
|
||||
sendTelemetry('/message/sendText');
|
||||
}
|
||||
return;
|
||||
@@ -801,15 +807,21 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
|
||||
if (!data?.messages || data.messages.length === 0) {
|
||||
if (!content) {
|
||||
if (unknownMessage) {
|
||||
await this.sendMessageWhatsApp(waInstance, remoteJid, unknownMessage, {
|
||||
delayMessage,
|
||||
expire,
|
||||
keywordFinish,
|
||||
listeningFromMe,
|
||||
stopBotFromMe,
|
||||
keepOpen,
|
||||
await this.sendMessageWhatsApp(
|
||||
waInstance,
|
||||
remoteJid,
|
||||
unknownMessage,
|
||||
});
|
||||
{
|
||||
delayMessage,
|
||||
expire,
|
||||
keywordFinish,
|
||||
listeningFromMe,
|
||||
stopBotFromMe,
|
||||
keepOpen,
|
||||
unknownMessage,
|
||||
},
|
||||
true,
|
||||
);
|
||||
sendTelemetry('/message/sendText');
|
||||
}
|
||||
return;
|
||||
@@ -903,15 +915,21 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
|
||||
|
||||
if (!content) {
|
||||
if (unknownMessage) {
|
||||
await this.sendMessageWhatsApp(waInstance, remoteJid, unknownMessage, {
|
||||
delayMessage,
|
||||
expire,
|
||||
keywordFinish,
|
||||
listeningFromMe,
|
||||
stopBotFromMe,
|
||||
keepOpen,
|
||||
await this.sendMessageWhatsApp(
|
||||
waInstance,
|
||||
remoteJid,
|
||||
unknownMessage,
|
||||
});
|
||||
{
|
||||
delayMessage,
|
||||
expire,
|
||||
keywordFinish,
|
||||
listeningFromMe,
|
||||
stopBotFromMe,
|
||||
keepOpen,
|
||||
unknownMessage,
|
||||
},
|
||||
true,
|
||||
);
|
||||
sendTelemetry('/message/sendText');
|
||||
}
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user