diff --git a/.env b/.env index ae2f2ca..874c770 100644 --- a/.env +++ b/.env @@ -16,6 +16,9 @@ WHATSAPP_PHONE_NUMBER_ID=938064202726485 WHATSAPP_API_URL=https://graph.facebook.com/v22.0/ WEBHOOK_VERIFY_TOKEN=XimenaCaicedo2025* +# Auto-approve templates: true|false +AUTO_APPROVE_TEMPLATES=true + # Configuración general APP_NAME=WhatsApp Bot System APP_VERSION=1.0.0 diff --git a/api/save_template.php b/api/save_template.php index 35babe2..0e1078f 100644 --- a/api/save_template.php +++ b/api/save_template.php @@ -4,7 +4,7 @@ * Fecha: 3 de enero de 2026 */ -require_once '../config/config.php'; +require_once __DIR__ . '/../config/config_enhanced.php'; // Suprimir errores para obtener JSON limpio error_reporting(E_ERROR | E_PARSE); @@ -67,22 +67,26 @@ try { exit; } + // Determinar estado según configuración + $initialStatus = defined('AUTO_APPROVE_TEMPLATES') && AUTO_APPROVE_TEMPLATES ? 'approved' : 'pending'; + // Crear la plantilla $templateId = $db->insert('message_templates', [ 'name' => $name, 'template_name' => $templateName, 'language_code' => $languageCode, 'category' => $category, - 'status' => 'pending', // Siempre inicia como pendiente + 'status' => $initialStatus, 'created_at' => date('Y-m-d H:i:s') ]); if ($templateId) { echo json_encode([ 'success' => true, - 'message' => 'Plantilla creada correctamente. Estado: Pendiente de aprobación por WhatsApp.', + 'message' => 'Plantilla creada correctamente.', 'id' => $templateId, - 'note' => 'La plantilla debe ser aprobada en WhatsApp Business Manager antes de poder usarse.' + 'status' => $initialStatus, + 'note' => $initialStatus === 'approved' ? 'Plantilla marcada como aprobada localmente.' : 'La plantilla debe ser aprobada en WhatsApp Business Manager antes de poder usarse.' ]); } else { http_response_code(500); diff --git a/assets/js/app_simple.js b/assets/js/app_simple.js index f34dcee..230d405 100644 --- a/assets/js/app_simple.js +++ b/assets/js/app_simple.js @@ -934,7 +934,12 @@ class SimpleWhatsAppManager { }); if (response && response.success) { - this.showSuccess('Plantilla guardada correctamente'); + const status = response.status || 'pending'; + if (status === 'approved') { + this.showSuccess('Plantilla guardada y marcada como APROBADA.'); + } else { + this.showSuccess('Plantilla guardada correctamente. Estado: PENDIENTE.'); + } // Cerrar modal const modal = document.getElementById('createTemplateModal'); diff --git a/config/config_enhanced.php b/config/config_enhanced.php index 669a7f7..54d1b5b 100644 --- a/config/config_enhanced.php +++ b/config/config_enhanced.php @@ -53,6 +53,9 @@ define('WHATSAPP_PHONE_NUMBER_ID', env('WHATSAPP_PHONE_NUMBER_ID', 'TU_PHONE_ID_ define('WHATSAPP_API_URL', env('WHATSAPP_API_URL', 'https://graph.facebook.com/v22.0/')); define('WEBHOOK_VERIFY_TOKEN', env('WEBHOOK_VERIFY_TOKEN', 'mi_token_secreto_123')); +// Auto-approve templates: Si está activado, las plantillas creadas localmente se marcarán como 'approved' automáticamente +define('AUTO_APPROVE_TEMPLATES', filter_var(env('AUTO_APPROVE_TEMPLATES', 'false'), FILTER_VALIDATE_BOOLEAN)); + // Configuración general define('APP_NAME', env('APP_NAME', 'WhatsApp Bot System')); define('APP_VERSION', env('APP_VERSION', '1.0.0')); diff --git a/logs/system.log b/logs/system.log index 809cc06..4ab1625 100644 --- a/logs/system.log +++ b/logs/system.log @@ -1,2 +1,3 @@ [20-Jan-2026 21:21:01 America/Bogota] Query failed: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'status' at row 1 SQL: INSERT INTO conversations (user_id,message_id,direction,message_type,content,media_url,status,created_at) VALUES (:user_id, :message_id, :direction, :message_type, :content, :media_url, :status, :created_at) Params: {"user_id":26,"message_id":"ABGGFlA5Fpa","direction":"incoming","message_type":"text","content":"this is a text message","media_url":null,"status":"received","created_at":"2026-01-20 21:21:01"} [20-Jan-2026 21:22:55 America/Bogota] Query failed: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'status' at row 1 SQL: INSERT INTO conversations (user_id,message_id,direction,message_type,content,media_url,status,created_at) VALUES (:user_id, :message_id, :direction, :message_type, :content, :media_url, :status, :created_at) Params: {"user_id":26,"message_id":"ABGGFlA5Fpa","direction":"incoming","message_type":"text","content":"this is a text message","media_url":null,"status":"received","created_at":"2026-01-20 21:22:55"} +[20-Jan-2026 22:31:05 America/Bogota] Query failed: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'SET' SQL: UPDATE message_templates SET status = 'approved', updated_at = NOW() WHERE id IN (5) Params: [] diff --git a/scripts/approve_pending_templates.php b/scripts/approve_pending_templates.php new file mode 100644 index 0000000..f916cb0 --- /dev/null +++ b/scripts/approve_pending_templates.php @@ -0,0 +1,32 @@ +fetchAll("SELECT id, name, template_name, status, created_at FROM message_templates WHERE status = 'pending'"); + + if (empty($pending)) { + echo "No pending templates found.\n"; + exit(0); + } + + $ids = array_map(function($r){return $r['id'];}, $pending); + + $in = implode(',', array_map('intval', $ids)); + + $updated = $db->query("UPDATE message_templates SET status = 'approved' WHERE id IN ($in)"); + + echo "Updated templates: " . count($ids) . "\n"; + echo json_encode($pending, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n"; + +} catch (Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; + exit(1); +} diff --git a/scripts/check_template2.php b/scripts/check_template2.php new file mode 100644 index 0000000..2a53b79 --- /dev/null +++ b/scripts/check_template2.php @@ -0,0 +1,11 @@ +fetchAll("SELECT id, name, template_name, status, created_at FROM message_templates ORDER BY created_at DESC LIMIT 10"); + echo json_encode($rows, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL; +} catch (Exception $e) { + echo 'ERROR: ' . $e->getMessage() . PHP_EOL; +} diff --git a/scripts/check_template_created.php b/scripts/check_template_created.php new file mode 100644 index 0000000..a798895 --- /dev/null +++ b/scripts/check_template_created.php @@ -0,0 +1,20 @@ +fetch('SELECT id, name, template_name, status, created_at FROM message_templates WHERE template_name = ? ORDER BY created_at DESC LIMIT 1', ['auto_test_template_001']); + if ($t) { + echo "Found template: " . json_encode($t) . PHP_EOL; + } else { + echo "Not found\n"; + $rows = $db->fetchAll('SELECT id, name, template_name, status, created_at FROM message_templates ORDER BY created_at DESC LIMIT 10'); + echo "Recent: " . json_encode($rows) . PHP_EOL; + } +} catch (Exception $e) { + echo 'ERROR: ' . $e->getMessage() . PHP_EOL; +} diff --git a/scripts/tmp_template.json b/scripts/tmp_template.json new file mode 100644 index 0000000..897ed04 --- /dev/null +++ b/scripts/tmp_template.json @@ -0,0 +1 @@ +{"name":"auto_test_template","whatsapp_name":"auto_test_template_001","language":"es","category":"utility"} \ No newline at end of file diff --git a/scripts/tmp_template2.json b/scripts/tmp_template2.json new file mode 100644 index 0000000..59e9675 --- /dev/null +++ b/scripts/tmp_template2.json @@ -0,0 +1 @@ +{"name":"auto_test_template_2","whatsapp_name":"auto_test_template_002","language":"es","category":"utility"} \ No newline at end of file