fix: correction in typebot text formatting

This commit is contained in:
Davidson Gomes 2023-12-28 18:07:24 -03:00
parent 2aadd1cac5
commit 6983f385fc

View File

@ -389,6 +389,7 @@ export class TypebotService {
input,
clientSideActions,
this.eventEmitter,
applyFormatting,
).catch((err) => {
console.error('Erro ao processar mensagens:', err);
});
@ -404,72 +405,64 @@ export class TypebotService {
return null;
}
async function processMessages(instance, messages, input, clientSideActions, eventEmitter) {
function applyFormatting(element) {
let text = '';
if (element.text) {
text += element.text;
}
if (element.type === 'p' || element.type === 'inline-variable') {
for (const child of element.children) {
text += applyFormatting(child);
}
}
let formats = '';
if (element.bold) {
formats += '*';
}
if (element.italic) {
formats += '_';
}
if (element.underline) {
formats += '~';
}
let formattedText = `${formats}${text}${formats.split('').reverse().join('')}`;
if (element.url) {
const linkText = element.children[0]?.text || '';
formattedText = `[${linkText}](${element.url})`;
}
return formattedText;
}
async function processMessages(instance, messages, input, clientSideActions, eventEmitter, applyFormatting) {
for (const message of messages) {
const wait = findItemAndGetSecondsToWait(clientSideActions, message.id);
if (message.type === 'text') {
let formattedText = '';
let linkPreview = false;
for (const richText of message.content.richText) {
if (richText.type === 'variable') {
for (const child of richText.children) {
for (const grandChild of child.children) {
formattedText += grandChild.text;
}
}
} else {
for (const element of richText.children) {
let text = '';
if (element.type === 'inline-variable') {
for (const child of element.children) {
for (const grandChild of child.children) {
text += grandChild.text;
}
}
} else if (element.text) {
text = element.text;
}
// if (element.text) {
// text = element.text;
// }
if (element.bold) {
text = `*${text}*`;
}
if (element.italic) {
text = `_${text}_`;
}
if (element.underline) {
text = `*${text}*`;
}
if (element.url) {
const linkText = element.children[0].text;
text = `[${linkText}](${element.url})`;
linkPreview = true;
}
formattedText += text;
}
for (const element of richText.children) {
formattedText += applyFormatting(element);
}
formattedText += '\n';
}
formattedText = formattedText.replace(/\n$/, '');
formattedText = formattedText.replace(/\*\*/g, '').replace(/__/, '').replace(/~~/, '').replace(/\n$/, '');
await instance.textMessage({
number: remoteJid.split('@')[0],
options: {
delay: wait ? wait * 1000 : instance.localTypebot.delay_message || 1000,
presence: 'composing',
linkPreview: linkPreview,
},
textMessage: {
text: formattedText,
@ -537,7 +530,6 @@ export class TypebotService {
options: {
delay: 1200,
presence: 'composing',
linkPreview: false,
},
textMessage: {
text: formattedText,