up
This commit is contained in:
+30
-3
@@ -48,9 +48,21 @@ try {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$input) {
|
if (!$input) {
|
||||||
http_response_code(400);
|
// Si no viene JSON válido, intentar leer datos from form-encoded POST
|
||||||
echo json_encode(['error' => 'JSON inválido o vacío', 'raw_input' => $rawInput]);
|
if (!empty($_POST)) {
|
||||||
exit;
|
$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();
|
$db = Database::getInstance();
|
||||||
@@ -180,6 +192,21 @@ try {
|
|||||||
}
|
}
|
||||||
$language = $input['language'] ?? 'es';
|
$language = $input['language'] ?? 'es';
|
||||||
$parameters = $input['parameters'] ?? [];
|
$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;
|
$componentsSimple = $input['components'] ?? null;
|
||||||
|
|
||||||
// Verificar plantilla en base de datos (opcional)
|
// Verificar plantilla en base de datos (opcional)
|
||||||
|
|||||||
+29
-1
@@ -1367,10 +1367,38 @@ class SimpleWhatsAppManager {
|
|||||||
return;
|
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 = {
|
messageData = {
|
||||||
recipient: recipient,
|
recipient: recipient,
|
||||||
type: 'template',
|
type: 'template',
|
||||||
template_name: templateSelect.value
|
template_name: tplName,
|
||||||
|
parameters: tplParams || {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user