fix: resolve TG message character limit overflow
This commit is contained in:
@@ -1,39 +1,62 @@
|
|||||||
import emailUtils from '../utils/email-utils';
|
import emailUtils from '../utils/email-utils';
|
||||||
|
|
||||||
|
const TELEGRAM_MESSAGE_LIMIT = 3500;
|
||||||
|
const TRUNCATED_SUFFIX = '...';
|
||||||
|
|
||||||
|
function escapeHtml(text = '') {
|
||||||
|
return text
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>');
|
||||||
|
}
|
||||||
|
|
||||||
|
function truncateText(text, maxLength) {
|
||||||
|
if (!text || text.length <= maxLength) {
|
||||||
|
return text || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maxLength <= TRUNCATED_SUFFIX.length) {
|
||||||
|
return TRUNCATED_SUFFIX.slice(0, maxLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
return text.slice(0, maxLength - TRUNCATED_SUFFIX.length) + TRUNCATED_SUFFIX;
|
||||||
|
}
|
||||||
|
|
||||||
export default function emailMsgTemplate(email, tgMsgTo, tgMsgFrom, tgMsgText) {
|
export default function emailMsgTemplate(email, tgMsgTo, tgMsgFrom, tgMsgText) {
|
||||||
|
|
||||||
let template = `<b>${email.subject}</b>`
|
let template = `<b>${escapeHtml(email.subject || '')}</b>`
|
||||||
|
|
||||||
if (tgMsgFrom === 'only-name') {
|
if (tgMsgFrom === 'only-name') {
|
||||||
template += `
|
template += `
|
||||||
|
|
||||||
From\u200B:${email.name}`
|
From\u200B:${escapeHtml(email.name || '')}`
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tgMsgFrom === 'show') {
|
if (tgMsgFrom === 'show') {
|
||||||
template += `
|
template += `
|
||||||
|
|
||||||
From\u200B:${email.name} <${email.sendEmail}>`
|
From\u200B:${escapeHtml(email.name || '')} <${escapeHtml(email.sendEmail || '')}>`
|
||||||
}
|
}
|
||||||
|
|
||||||
if(tgMsgTo === 'show' && tgMsgFrom === 'hide') {
|
if(tgMsgTo === 'show' && tgMsgFrom === 'hide') {
|
||||||
template += `
|
template += `
|
||||||
|
|
||||||
To:\u200B${email.toEmail}`
|
To:\u200B${escapeHtml(email.toEmail || '')}`
|
||||||
|
|
||||||
} else if(tgMsgTo === 'show') {
|
} else if(tgMsgTo === 'show') {
|
||||||
template += `
|
template += `
|
||||||
To:\u200B${email.toEmail}`
|
To:\u200B${escapeHtml(email.toEmail || '')}`
|
||||||
}
|
}
|
||||||
|
|
||||||
const text = (emailUtils.formatText(email.text) || emailUtils.htmlToText(email.content))
|
const text = escapeHtml(emailUtils.formatText(email.text) || emailUtils.htmlToText(email.content));
|
||||||
.replace(/</g, '<')
|
|
||||||
.replace(/>/g, '>');
|
|
||||||
|
|
||||||
if(tgMsgText === 'show') {
|
if(tgMsgText === 'show') {
|
||||||
|
const prefix = `${template}
|
||||||
|
|
||||||
|
`;
|
||||||
|
const maxTextLength = Math.max(0, TELEGRAM_MESSAGE_LIMIT - prefix.length);
|
||||||
template += `
|
template += `
|
||||||
|
|
||||||
${text}`
|
${truncateText(text, maxTextLength)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
return template;
|
return template;
|
||||||
|
|||||||
Reference in New Issue
Block a user