From 10386cf408d3c1e6f76ae093be9aed959ede7cb6 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Sat, 24 Jan 2026 12:41:55 -0500 Subject: [PATCH] up --- api/send_message.php | 33 ++++++++++++++++++++++++++++++--- assets/js/app_simple.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/api/send_message.php b/api/send_message.php index 21e1929..e13ab18 100644 --- a/api/send_message.php +++ b/api/send_message.php @@ -48,9 +48,21 @@ try { } if (!$input) { - http_response_code(400); - echo json_encode(['error' => 'JSON inválido o vacío', 'raw_input' => $rawInput]); - exit; + // Si no viene JSON válido, intentar leer datos from form-encoded POST + if (!empty($_POST)) { + $input = $_POST; + // Si el formulario incluye template_params como JSON string, decodificarlo + if (isset($input['template_params']) && is_string($input['template_params'])) { + $decodedTp = json_decode($input['template_params'], true); + if ($decodedTp !== null) { + $input['template_params'] = $decodedTp; + } + } + } else { + http_response_code(400); + echo json_encode(['error' => 'JSON inválido o vacío', 'raw_input' => $rawInput]); + exit; + } } $db = Database::getInstance(); @@ -180,6 +192,21 @@ try { } $language = $input['language'] ?? 'es'; $parameters = $input['parameters'] ?? []; + // Also accept template_params (from form or hidden input) + if ((empty($parameters) || $parameters === []) && isset($input['template_params'])) { + $tp = $input['template_params']; + if (is_string($tp)) { + $decoded = json_decode($tp, true); + if ($decoded !== null) { + $parameters = $decoded; + } else { + // fallback: treat string as a single named parameter value (name) + $parameters = ['name' => $tp]; + } + } elseif (is_array($tp)) { + $parameters = $tp; + } + } $componentsSimple = $input['components'] ?? null; // Verificar plantilla en base de datos (opcional) diff --git a/assets/js/app_simple.js b/assets/js/app_simple.js index 97fdc2f..43ee14c 100644 --- a/assets/js/app_simple.js +++ b/assets/js/app_simple.js @@ -1367,10 +1367,38 @@ class SimpleWhatsAppManager { return; } + // Prepare template payload; support named parameters (e.g. { name: 'Juan' }) + const tplName = templateSelect.value; + let tplParams = null; + + // If there's a hidden input with JSON parameters (index.php), use it + const paramsInput = document.getElementById('template-params'); + if (paramsInput && paramsInput.value) { + try { + tplParams = JSON.parse(paramsInput.value); + } catch (e) { + console.warn('Invalid JSON in #template-params, ignoring', e); + tplParams = null; + } + } + + // If no params provided and template is contacto_nuevo, prompt operator for name + if ((!tplParams || (typeof tplParams === 'object' && Object.keys(tplParams).length === 0)) && tplName === 'contacto_nuevo') { + const defaultName = ''; + const inputName = prompt('Ingrese el nombre para la plantilla contacto_nuevo (dejar vacío para usar el número):', defaultName); + if (inputName === null) { + // user cancelled + return; + } + const clean = (typeof inputName === 'string') ? inputName.trim() : ''; + tplParams = { name: clean || recipient }; + } + messageData = { recipient: recipient, type: 'template', - template_name: templateSelect.value + template_name: tplName, + parameters: tplParams || {} }; }