diff --git a/api/lab/save_config.php b/api/lab/save_config.php index 42fb5c7..aed766b 100644 --- a/api/lab/save_config.php +++ b/api/lab/save_config.php @@ -18,6 +18,9 @@ $permitidas = [ 'doc_color', 'doc_logo_base64', 'doc_pie_pagina', 'gemini_api_key', 'ia_activa', + 'sms_url', + 'sms_api_key', + 'sms_activo', ]; $guardadas = 0; diff --git a/api/lab/test_sms.php b/api/lab/test_sms.php new file mode 100644 index 0000000..58ed902 --- /dev/null +++ b/api/lab/test_sms.php @@ -0,0 +1,59 @@ + $numero, + 'mensaje' => $mensaje, +], JSON_UNESCAPED_UNICODE); + +$endpoint = $baseUrl . '/api/sms/send'; + +$ch = curl_init($endpoint); +curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $payload, + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', + 'Authorization: Bearer ' . $key, + ], + CURLOPT_TIMEOUT => 15, + CURLOPT_SSL_VERIFYPEER => true, +]); +$raw = curl_exec($ch); +$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); +$err = curl_error($ch); +curl_close($ch); + +if ($err) jsonError('Error de conexión: ' . $err, 502); +if (!$raw) jsonError('Sin respuesta del servidor SMS.', 502); + +$resp = json_decode($raw, true); +if ($code >= 200 && $code < 300 && !empty($resp['ok'])) { + jsonOk(['id' => $resp['id'] ?? null]); +} + +$msg = $resp['error'] ?? "Error HTTP {$code}"; +jsonError($msg, 502); diff --git a/lab_configuracion.php b/lab_configuracion.php index c84bb6c..6e70b72 100644 --- a/lab_configuracion.php +++ b/lab_configuracion.php @@ -199,6 +199,58 @@ require_once __DIR__ . '/shared/components/sidebar.php'; + +
+
+
Integración WEBSms
+
+ > + +
+
+

+ Permite enviar SMS a pacientes desde el sistema. Requiere una cuenta en WEBSms. +

+ +
+ + +
Sin barra final. El sistema agrega /api/sms/send.
+
+ +
+ +
+ + +
+
+ +
+ +
+ + + + +
+
+
+
+ @@ -322,6 +374,9 @@ async function guardar() { doc_pie_pagina: $('c-doc-pie').value.trim(), gemini_api_key: $('c-gemini-key').value.trim(), ia_activa: $('c-ia-activa').checked ? '1' : '0', + sms_url: $('c-sms-url').value.trim().replace(/\/$/, ''), + sms_api_key: $('c-sms-key').value.trim(), + sms_activo: $('c-sms-activo').checked ? '1' : '0', }; if (!payload.empresa_nombre) { @@ -354,6 +409,42 @@ async function guardar() { } } +async function probarSms() { + const url = $('c-sms-url').value.trim().replace(/\/$/, ''); + const key = $('c-sms-key').value.trim(); + const num = $('c-sms-test-num').value.trim(); + const msg = $('c-sms-test-msg').value.trim(); + const res = document.getElementById('sms-test-result'); + const btn = document.getElementById('btn-probar-sms'); + + if (!url) { res.innerHTML = 'Ingresa la URL base primero.'; return; } + if (!key) { res.innerHTML = 'Ingresa la API key primero.'; return; } + if (!num) { res.innerHTML = 'Ingresa un número de destino.'; return; } + + btn.disabled = true; + btn.innerHTML = ''; + res.innerHTML = ''; + + try { + const r = await fetch('api/lab/test_sms.php', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ url, key, numero: num, mensaje: msg || 'Prueba de conexión WEBSms' }), + }); + const d = await r.json(); + if (d.ok) { + res.innerHTML = `SMS enviado — ID: ${d.id || '—'}`; + } else { + res.innerHTML = `${d.error || 'Error desconocido'}`; + } + } catch(_) { + res.innerHTML = 'No se pudo conectar.'; + } finally { + btn.disabled = false; + btn.innerHTML = 'Enviar'; + } +} + async function probarIA() { const key = $('c-gemini-key').value.trim(); const res = document.getElementById('ia-test-result');