Actualizar server.js
This commit is contained in:
55
server.js
55
server.js
@@ -852,6 +852,45 @@ app.post("/whatsapp/settings", authMiddleware, async (req, res) => {
|
|||||||
} catch (e) { res.status(500).json({ ok: false }); }
|
} catch (e) { res.status(500).json({ ok: false }); }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// 💬 NÚCLEO DE ENVÍO WHATSAPP (EVOLUTION API)
|
||||||
|
// ==========================================
|
||||||
|
async function sendWhatsAppAuto(phone, message, instanceName, useDelay = false) {
|
||||||
|
if (!EVOLUTION_BASE_URL || !EVOLUTION_API_KEY) {
|
||||||
|
console.error("⚠️ WhatsApp omitido: Servidor Evolution no configurado.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const baseUrl = EVOLUTION_BASE_URL.replace(/\/$/, "");
|
||||||
|
const url = `${baseUrl}/message/sendText/${instanceName}`;
|
||||||
|
|
||||||
|
const response = await fetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"apikey": EVOLUTION_API_KEY.trim()
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
number: phone,
|
||||||
|
text: message,
|
||||||
|
delay: useDelay ? 2000 : 0
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error("❌ Fallo enviando WhatsApp. Estado Evolution:", response.status);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("❌ Error de red conectando con Evolution API:", error.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// 🔔 DISPARADOR DE EVENTOS DE WHATSAPP
|
||||||
|
// ==========================================
|
||||||
async function triggerWhatsAppEvent(ownerId, serviceId, eventType) {
|
async function triggerWhatsAppEvent(ownerId, serviceId, eventType) {
|
||||||
try {
|
try {
|
||||||
const userQ = await pool.query("SELECT wa_settings FROM users WHERE id=$1", [ownerId]);
|
const userQ = await pool.query("SELECT wa_settings FROM users WHERE id=$1", [ownerId]);
|
||||||
@@ -913,21 +952,19 @@ async function triggerWhatsAppEvent(ownerId, serviceId, eventType) {
|
|||||||
text = text.replace(/{{FECHA}}/g, fechaLimpia);
|
text = text.replace(/{{FECHA}}/g, fechaLimpia);
|
||||||
text = text.replace(/{{HORA}}/g, raw["scheduled_time"] || "la hora acordada");
|
text = text.replace(/{{HORA}}/g, raw["scheduled_time"] || "la hora acordada");
|
||||||
text = text.replace(/{{COMPANIA}}/g, raw["Compañía"] || raw["COMPAÑIA"] || "su Aseguradora");
|
text = text.replace(/{{COMPANIA}}/g, raw["Compañía"] || raw["COMPAÑIA"] || "su Aseguradora");
|
||||||
text = text.replace(/{{REFERENCIA}}/g, s.service_ref || raw["Referencia"] || raw["Nº Siniestro"] || id); text = text.replace(/{{ENLACE}}/g, linkMagico);
|
text = text.replace(/{{REFERENCIA}}/g, s.service_ref || raw["Referencia"] || raw["Nº Siniestro"] || serviceId);
|
||||||
|
text = text.replace(/{{ENLACE}}/g, linkMagico);
|
||||||
|
|
||||||
const useDelay = settings.wa_delay_enabled !== false;
|
const useDelay = settings.wa_delay_enabled !== false;
|
||||||
|
|
||||||
// RETORNAMOS EL ÉXITO O FRACASO DEL ENVÍO
|
// RETORNAMOS EL ÉXITO O FRACASO DEL ENVÍO
|
||||||
return await sendWhatsAppAuto(phone, text, `cliente_${ownerId}`, useDelay);
|
return await sendWhatsAppAuto(phone, text, `cliente_${ownerId}`, useDelay);
|
||||||
|
|
||||||
} catch (e) { console.error("Error Motor WA:", e.message); return false; }
|
} catch (e) {
|
||||||
|
console.error("Error Motor WA:", e.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get("/providers/credentials", authMiddleware, async (req, res) => {
|
|
||||||
try {
|
|
||||||
const q = await pool.query("SELECT provider, username, last_sync, status FROM provider_credentials WHERE owner_id=$1", [req.user.accountId]);
|
|
||||||
res.json({ ok: true, credentials: q.rows });
|
|
||||||
} catch (e) { res.status(500).json({ ok: false }); }
|
|
||||||
});
|
|
||||||
|
|
||||||
app.post("/providers/credentials", authMiddleware, async (req, res) => {
|
app.post("/providers/credentials", authMiddleware, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user