From eddfe4d16c60eb15ea31307cc8858902fc845996 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Fri, 3 Jul 2026 10:02:34 -0500 Subject: [PATCH] debug_turno_wa: corregir tabla system_config para token/phone WA Co-Authored-By: Claude Sonnet 4.6 --- debug_turno_wa.php | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/debug_turno_wa.php b/debug_turno_wa.php index d92c0c0..192c2c6 100644 --- a/debug_turno_wa.php +++ b/debug_turno_wa.php @@ -17,12 +17,19 @@ echo "════════════════════════ // ── 1. Config WA turnero ────────────────────────────────────── echo "1. CONFIGURACIÓN WHATSAPP\n"; echo "─────────────────────────\n"; -$cfgKeys = ['whatsapp_token','whatsapp_phone_number_id','whatsapp_phone_number_id_turnero', - 'turnero_wa_template','turnero_wa_lang','turnero_wa_kiosko_enabled']; -$cfg = $pdo->query( - "SELECT clave, valor FROM lab_config WHERE clave IN ('" . implode("','", $cfgKeys) . "')" +// Token y phone IDs viven en system_config; resto en lab_config +$sysCfg = $pdo->query( + "SELECT config_key AS k, config_value AS v FROM system_config + WHERE config_key IN ('whatsapp_token','whatsapp_phone_number_id','whatsapp_phone_number_id_turnero')" )->fetchAll(PDO::FETCH_KEY_PAIR); +$labCfg = $pdo->query( + "SELECT clave AS k, valor AS v FROM lab_config + WHERE clave IN ('turnero_wa_template','turnero_wa_lang','turnero_wa_kiosko_enabled')" +)->fetchAll(PDO::FETCH_KEY_PAIR); + +$cfg = array_merge($sysCfg, $labCfg); + $tokenLen = strlen($cfg['whatsapp_token'] ?? ''); echo "Token WA: " . ($tokenLen > 0 ? "✅ presente ({$tokenLen} chars)" : "❌ VACÍO") . "\n"; echo "Phone ID principal: " . ($cfg['whatsapp_phone_number_id'] ?? '❌ no configurado') . "\n"; @@ -102,15 +109,21 @@ if (isset($_GET['test_cel'])) { $testLang = $cfg['turnero_wa_lang'] ?? 'es_CO'; require_once __DIR__ . '/services/WhatsAppService.php'; + // Detectar cuántas variables tiene la plantilla en el body + $bodyText = $tplRow['body_text'] ?? ''; + preg_match_all('/\{\{(\d+)\}\}/', $bodyText, $m); + $numVars = count(array_unique($m[1] ?? [])); + $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; + $testLink = $baseUrl . '/ver_formulario_enviado.php?token=TEST123'; + + // Armar params según cuántas variables haya + $bodyParams = array_slice(['Paciente Prueba', 'Consentimiento Informado', 'A001'], 0, max($numVars, 1)); + echo "Variables detectadas en body: {$numVars} → params enviados: " . implode(', ', $bodyParams) . "\n"; + echo "URL: {$testLink}\n\n"; + try { $wa = new WhatsAppService('turnero'); - $wa->sendTemplateMessage( - $testCel, - $testTpl, - $testLang, - ['Paciente Prueba', 'Consentimiento Informado', 'A001'], - [($_SERVER['HTTPS'] ?? '') === 'on' ? 'https' : 'http' . '://' . $_SERVER['HTTP_HOST'] . '/ver_formulario_enviado.php?token=TEST'] - ); + $wa->sendTemplateMessage($testCel, $testTpl, $testLang, $bodyParams, [$testLink]); echo "✅ Mensaje enviado a {$testCel}\n"; } catch (\Throwable $e) { echo "❌ Error: " . $e->getMessage() . "\n";