badges on the right, evolution api sender

This commit is contained in:
Lucas Tettamanti
2026-01-26 01:21:08 -03:00
parent e85afab3e6
commit 53293ce9b3
13 changed files with 422 additions and 51 deletions

View File

@@ -5,10 +5,51 @@
import { ConversationState } from "../fsm.js";
import { createEmptyOrder, formatCartForDisplay } from "../orderModel.js";
/**
* Detecta si el usuario pregunta por horarios/días de entrega
*/
function isDeliveryInfoQuestion(text) {
const t = String(text || "").toLowerCase();
const patterns = [
/cu[aá]ndo\s+(entrega|llega|env[ií])/i,
/qu[eé]\s+d[ií]as?\s+(entrega|env[ií]|repart)/i,
/d[ií]as?\s+de\s+(entrega|env[ií]|reparto)/i,
/horario\s+de\s+(entrega|env[ií]|reparto)/i,
/qu[eé]\s+horarios?/i,
/a\s+qu[eé]\s+hora/i,
/en\s+qu[eé]\s+horario/i,
/cuando\s+(me\s+)?lo\s+traen/i,
/d[ií]a\s+y\s+hora/i,
];
return patterns.some(p => p.test(t));
}
/**
* Formatea los días de entrega para mostrar
*/
function formatDeliveryDays(daysStr) {
if (!daysStr) return null;
const dayMap = {
"lun": "Lunes", "mar": "Martes", "mie": "Miércoles", "mié": "Miércoles",
"jue": "Jueves", "vie": "Viernes", "sab": "Sábado", "sáb": "Sábado", "dom": "Domingo",
};
const days = daysStr.split(",").map(d => d.trim().toLowerCase());
const formatted = days.map(d => dayMap[d] || d).filter(Boolean);
if (formatted.length === 0) return null;
if (formatted.length === 1) return formatted[0];
// "Lunes, Martes, Miércoles y Jueves"
const last = formatted.pop();
return `${formatted.join(", ")} y ${last}`;
}
/**
* Maneja el estado WAITING_WEBHOOKS (esperando confirmación de pago)
*/
export async function handleWaitingState({ tenantId, text, nlu, order, audit }) {
export async function handleWaitingState({ tenantId, text, nlu, order, audit, storeConfig = {} }) {
const intent = nlu?.intent || "other";
const currentOrder = order || createEmptyOrder();
@@ -28,6 +69,37 @@ export async function handleWaitingState({ tenantId, text, nlu, order, audit })
};
}
// Preguntas sobre horarios/días de entrega
if (isDeliveryInfoQuestion(text)) {
const deliveryDays = formatDeliveryDays(storeConfig.delivery_days);
const startHour = storeConfig.delivery_hours_start?.slice(0, 5);
const endHour = storeConfig.delivery_hours_end?.slice(0, 5);
let reply = "";
if (deliveryDays) {
reply = `Hacemos entregas los días ${deliveryDays}`;
if (startHour && endHour) {
reply += ` de ${startHour} a ${endHour}`;
}
reply += ". ";
} else {
reply = "Todavía no tengo configurado los días de entrega. ";
}
reply += "Tu pedido ya está en proceso, avisame cualquier cosa.";
return {
plan: {
reply,
next_state: ConversationState.WAITING_WEBHOOKS,
intent: "delivery_info",
missing_fields: [],
order_action: "none",
},
decision: { actions: [], order: currentOrder, audit },
};
}
// Default
const reply = currentOrder.payment_type === "link"
? "Tu pedido está en proceso. Avisame si necesitás algo más o esperá la confirmación de pago."