perf(webhook): optimizar respuestas no bloqueantes y fallback de Notificaciones Almeira
This commit is contained in:
parent
98f13621ba
commit
6ef2c3dd19
@ -194,27 +194,21 @@ export async function POST(req: Request) {
|
||||
const destinationJid = adminNotificationJid || remoteJid;
|
||||
const isCentralizedAdmin = Boolean(adminNotificationJid && adminNotificationJid !== remoteJid);
|
||||
|
||||
// Notificación de Progreso 1: Recepción inmediata
|
||||
try {
|
||||
if (isCentralizedAdmin) {
|
||||
// Acuse discreto a la fuente/cliente
|
||||
await sendWhatsAppMessage(
|
||||
remoteJid,
|
||||
`📥 *Comunicado Recibido*\n` +
|
||||
`_Almeira Web • Redacción Editorial_\n\n` +
|
||||
`Tu información ha sido recibida y remitida a la mesa editorial para su revisión.`
|
||||
);
|
||||
} else {
|
||||
// Notificación directa en el mismo chat
|
||||
await sendWhatsAppMessage(
|
||||
remoteJid,
|
||||
`📥 *Información Recibida*\n` +
|
||||
`_Almeira Web • Asistente Editorial con IA_\n\n` +
|
||||
`🤖 Iniciando análisis con IA y procesamiento de contenido...`
|
||||
);
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.warn('[WhatsApp Webhook] No se pudo enviar notificación de inicio:', err.message);
|
||||
// Notificación de Progreso 1: Recepción inmediata (asíncrona / no bloqueante)
|
||||
if (isCentralizedAdmin) {
|
||||
sendWhatsAppMessage(
|
||||
remoteJid,
|
||||
`📥 *Comunicado Recibido*\n` +
|
||||
`_Almeira Web • Redacción Editorial_\n\n` +
|
||||
`Tu información ha sido recibida y remitida a la mesa editorial para su revisión.`
|
||||
).catch(e => console.warn('[WhatsApp Webhook] No se pudo enviar acuse a la fuente:', e.message));
|
||||
} else {
|
||||
sendWhatsAppMessage(
|
||||
remoteJid,
|
||||
`📥 *Información Recibida*\n` +
|
||||
`_Almeira Web • Asistente Editorial con IA_\n\n` +
|
||||
`🤖 Iniciando análisis con IA y procesamiento de contenido...`
|
||||
).catch(e => console.warn('[WhatsApp Webhook] No se pudo enviar notificación de inicio:', e.message));
|
||||
}
|
||||
|
||||
// Recolectamos todas las imágenes/multimedia candidatas (mensaje actual + lote reciente del mismo chat)
|
||||
@ -250,23 +244,19 @@ export async function POST(req: Request) {
|
||||
const isSquare = parsed.tipo === 'evento' || parsed.tipo === 'aviso';
|
||||
const imgHeight = isSquare ? 640 : 360;
|
||||
|
||||
// Notificación de Progreso 2: Estructuración completa y registro
|
||||
try {
|
||||
const mediaStatus = candidateMediaList.length > 0
|
||||
? `🖼️ *Imagen:* Optimizando foto destacada (${isSquare ? 'Formato 640x640' : 'Formato 640x360'})`
|
||||
: `📄 *Formato:* Publicación en texto`;
|
||||
// Notificación de Progreso 2: Estructuración completa y registro (asíncrona)
|
||||
const mediaStatus = candidateMediaList.length > 0
|
||||
? `🖼️ *Imagen:* Optimizando foto destacada (${isSquare ? 'Formato 640x640' : 'Formato 640x360'})`
|
||||
: `📄 *Formato:* Publicación en texto`;
|
||||
|
||||
const progressMsg = `⚙️ *Procesando Publicación*\n`
|
||||
+ (isCentralizedAdmin ? `📍 *Fuente:* ${filterResult.resolvedName}\n\n` : '\n')
|
||||
+ `📌 *Titular:* ${parsed.titulo}\n`
|
||||
+ `📂 *Categoría:* ${parsed.tipo.toUpperCase()}\n`
|
||||
+ `${mediaStatus}\n\n`
|
||||
+ `💾 Registrando en la biblioteca de medios y guardando borrador en la web...`;
|
||||
const progressMsg = `⚙️ *Procesando Publicación*\n`
|
||||
+ (isCentralizedAdmin ? `📍 *Fuente:* ${filterResult.resolvedName}\n\n` : '\n')
|
||||
+ `📌 *Titular:* ${parsed.titulo}\n`
|
||||
+ `📂 *Categoría:* ${parsed.tipo.toUpperCase()}\n`
|
||||
+ `${mediaStatus}\n\n`
|
||||
+ `💾 Registrando en la biblioteca de medios y guardando borrador en la web...`;
|
||||
|
||||
await sendWhatsAppMessage(destinationJid, progressMsg);
|
||||
} catch (err: any) {
|
||||
console.warn('[WhatsApp Webhook] No se pudo enviar notificación de progreso:', err.message);
|
||||
}
|
||||
sendWhatsAppMessage(destinationJid, progressMsg).catch(e => console.warn('[WhatsApp Webhook] No se pudo enviar notificación de progreso:', e.message));
|
||||
|
||||
let storageFileId = undefined;
|
||||
let featuredMediaDocId = undefined;
|
||||
|
||||
@ -164,12 +164,19 @@ export async function getWhatsAppGroupSubject(groupJid: string): Promise<string
|
||||
return null;
|
||||
}
|
||||
|
||||
let cachedNotificationGroupJid: string | null = '120363428012905103@g.us';
|
||||
|
||||
/**
|
||||
* Automatically discovers the dedicated Admin/Notification group JID if one exists
|
||||
*/
|
||||
export async function getNotificationGroupJid(): Promise<string | null> {
|
||||
if (cachedNotificationGroupJid) {
|
||||
return cachedNotificationGroupJid;
|
||||
}
|
||||
|
||||
if (process.env.WHATSAPP_ADMIN_NOTIFICATION_JID) {
|
||||
return process.env.WHATSAPP_ADMIN_NOTIFICATION_JID;
|
||||
cachedNotificationGroupJid = process.env.WHATSAPP_ADMIN_NOTIFICATION_JID;
|
||||
return cachedNotificationGroupJid;
|
||||
}
|
||||
|
||||
try {
|
||||
@ -183,11 +190,12 @@ export async function getNotificationGroupJid(): Promise<string | null> {
|
||||
});
|
||||
|
||||
if (found) {
|
||||
cachedNotificationGroupJid = found.id;
|
||||
return found.id;
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.warn('Could not discover notification group:', err?.message);
|
||||
}
|
||||
|
||||
return null;
|
||||
return '120363428012905103@g.us';
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user