Actualizar server.js
This commit is contained in:
31
server.js
31
server.js
@@ -340,24 +340,16 @@ async function sendWhatsAppCode(phone, code) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function sendWhatsAppAuto(originalPhone, text, instanceName, useDelay = true) {
|
async function sendWhatsAppAuto(originalPhone, text, instanceName, useDelay = true) {
|
||||||
if (!EVOLUTION_BASE_URL || !EVOLUTION_API_KEY || !instanceName) {
|
if (!EVOLUTION_BASE_URL || !EVOLUTION_API_KEY || !instanceName) return false;
|
||||||
console.error("❌ Faltan datos para enviar WhatsApp automático (Revisa URLs o instancia)");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==========================================
|
const TEST_PHONE = "34667248132"; // TU NÚMERO PROTEGIDO
|
||||||
// 🛑 MODO PRUEBAS (SANDBOX) ACTIVADO 🛑
|
|
||||||
// ==========================================
|
|
||||||
const TEST_PHONE = "34667248132"; // <--- TU NÚMERO PROTEGIDO
|
|
||||||
const phone = TEST_PHONE;
|
const phone = TEST_PHONE;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`\n📲 [MODO PRUEBA] El sistema quería enviar un WA a ${originalPhone} pero se ha redirigido a tu número: ${phone}`);
|
console.log(`\n📲 [MODO PRUEBA] WA a ${originalPhone} redirigido a -> ${phone}`);
|
||||||
|
|
||||||
let payloadConEscribiendo;
|
let payloadConEscribiendo;
|
||||||
const typingTimeMs = Math.min(Math.max(text.length * 30, 1500), 8000);
|
const typingTimeMs = Math.min(Math.max(text.length * 30, 1500), 8000);
|
||||||
|
|
||||||
// Añadimos una cabecera para que sepas a quién iba dirigido realmente el mensaje
|
|
||||||
const textWithNotice = `*(PRUEBA - Iba para: ${originalPhone})*\n\n` + text;
|
const textWithNotice = `*(PRUEBA - Iba para: ${originalPhone})*\n\n` + text;
|
||||||
|
|
||||||
if(useDelay) {
|
if(useDelay) {
|
||||||
@@ -377,25 +369,22 @@ async function sendWhatsAppAuto(originalPhone, text, instanceName, useDelay = tr
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok && useDelay) {
|
if (!res.ok && useDelay) {
|
||||||
const errCode = res.status;
|
|
||||||
console.warn(`⚠️ Evolution rechazó el modo "Escribiendo" (Código ${errCode}). Activando Plan B (Modo seguro instantáneo)...`);
|
|
||||||
|
|
||||||
const payloadSeguro = { number: phone.replace("+", ""), text: textWithNotice };
|
const payloadSeguro = { number: phone.replace("+", ""), text: textWithNotice };
|
||||||
const res2 = await fetch(`${EVOLUTION_BASE_URL.replace(/\/$/, "")}/message/sendText/${instanceName}`, {
|
const res2 = await fetch(`${EVOLUTION_BASE_URL.replace(/\/$/, "")}/message/sendText/${instanceName}`, {
|
||||||
method: "POST",
|
method: "POST", headers: { "Content-Type": "application/json", "apikey": EVOLUTION_API_KEY },
|
||||||
headers: { "Content-Type": "application/json", "apikey": EVOLUTION_API_KEY },
|
|
||||||
body: JSON.stringify(payloadSeguro)
|
body: JSON.stringify(payloadSeguro)
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!res2.ok) console.error("❌ Error definitivo en Evolution API:", await res2.text());
|
if (!res2.ok) return false; // Falló el plan B
|
||||||
else console.log("✅ WA de prueba enviado correctamente (Plan B).");
|
return true; // Éxito en plan B
|
||||||
} else if (res.ok) {
|
} else if (res.ok) {
|
||||||
console.log(`✅ WA de prueba enviado con éxito.`);
|
return true; // Éxito a la primera
|
||||||
} else {
|
} else {
|
||||||
console.error("❌ Error en Evolution API:", await res.text());
|
return false; // Falló y no había delay
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("❌ Error crítico en función WA:", e.message);
|
console.error("❌ Error crítico en WA:", e.message);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user