feat(whatsapp): notificacion con Titular, Imagen destacada, Resumen y Contenido sin etiquetas

This commit is contained in:
AI Bot 2026-08-24 12:46:58 -05:00
parent 7ff2dd41fb
commit f356cfbadd

View File

@ -23,6 +23,27 @@ function truncateString(str: string, length: number): string {
return str.length > length ? str.substring(0, length - 3) + '...' : str;
}
function htmlToWhatsAppText(html: string): string {
if (!html) return '';
return html
.replace(/<strong[^>]*>(.*?)<\/strong>/gi, '*$1*')
.replace(/<b[^>]*>(.*?)<\/b>/gi, '*$1*')
.replace(/<em[^>]*>(.*?)<\/em>/gi, '_$1_')
.replace(/<i[^>]*>(.*?)<\/i>/gi, '_$1_')
.replace(/<li[^>]*>(.*?)<\/li>/gi, '• $1\n')
.replace(/<\/?(?:p|div|h[1-6]|ul|ol|table|tr|td)[^>]*>/gi, '\n')
.replace(/<br\s*\/?>/gi, '\n')
.replace(/<[^>]+>/g, '') // Eliminar cualquier etiqueta HTML remanente
.replace(/&nbsp;/g, ' ')
.replace(/&amp;/g, '&')
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/\n{3,}/g, '\n\n')
.trim();
}
export async function POST(req: Request) {
try {
const payload = await req.json();
@ -294,22 +315,24 @@ export async function POST(req: Request) {
}
);
// Notificar al grupo de WhatsApp pidiendo aprobación explícita
let imageNotice = '';
// Formatear contenido limpio para WhatsApp (sin etiquetas HTML)
const cleanContent = htmlToWhatsAppText(parsed.contenido);
// Estado de la imagen destacada
let imageNotice = 'No se adjuntó imagen';
if (candidateMediaList.length > 1) {
const chosenNum = (parsed.selected_image_index ?? 0) + 1;
imageNotice = `🖼️ *Imagen Destacada:* Foto #${chosenNum} de ${candidateMediaList.length} analizadas`
+ (parsed.image_selection_reason ? ` (${parsed.image_selection_reason})\n` : '\n');
imageNotice = `Foto #${chosenNum} de ${candidateMediaList.length} analizadas`
+ (parsed.image_selection_reason ? ` (${parsed.image_selection_reason})` : '');
} else if (candidateMediaList.length === 1) {
imageNotice = `🖼️ *Imagen Destacada:* 1 foto procesada (${isSquare ? 'Formato 640x640' : 'Formato 640x360'})\n`;
imageNotice = `1 foto procesada (${isSquare ? '640x640' : '640x360'})`;
}
const requestText = `🔔 *SOLICITUD DE APROBACIÓN DE PUBLICACIÓN* 🔔\n`
+ `_Almeira Web • Asistente Editorial con IA_\n\n`
+ `📌 *Título:* ${parsed.titulo}\n`
+ `📂 *Categoría:* ${parsed.tipo.toUpperCase()} (${isSquare ? 'Cuadrado 640x640' : 'Panorámico 640x360'})\n`
+ (imageNotice ? `${imageNotice}` : '')
+ `📝 *Resumen:* ${parsed.seo_descripcion}\n\n`
const requestText = `🔔 *SOLICITUD DE APROBACIÓN DE PUBLICACIÓN*\n\n`
+ `📌 *Titular:*\n${parsed.titulo}\n\n`
+ `🖼️ *Imagen Destacada:*\n${imageNotice}\n\n`
+ `📝 *Resumen:*\n${parsed.seo_descripcion}\n\n`
+ `📄 *Contenido:*\n${cleanContent}\n\n`
+ `━━━━━━━━━━━━━━━━━━━━━━\n`
+ `¿Autorizas la publicación en el portal?\n\n`
+ `👉 Responde a este mensaje con:\n`