This commit is contained in:
lizandrogd
2026-01-20 22:13:02 -05:00
parent f0a713e1da
commit 648a6c0291
19 changed files with 762 additions and 33 deletions
+50 -11
View File
@@ -4,7 +4,7 @@
* Fecha: 13 de noviembre de 2025
*/
require_once '../config/config.php';
require_once __DIR__ . '/../config/config_enhanced.php';
// Headers para API
header('Content-Type: application/json; charset=utf-8');
@@ -146,8 +146,12 @@ class WhatsAppWebhook {
'status' => 'received'
]);
// Procesar con bot
$this->botService->processMessage($user, $messageText, $messageType);
// Procesar con bot (protección contra excepciones externas)
try {
$this->botService->processMessage($user, $messageText, $messageType);
} catch (Exception $e) {
error_log("Bot processing failed: " . $e->getMessage());
}
}
// Procesar estados de mensajes (entregado, leído, etc.)
@@ -207,15 +211,50 @@ class WhatsAppWebhook {
]);
}
}
/**
* Procesar un payload (array) recibido manualmente (replay)
* Útil para reproducir entradas desde la UI o scripts.
*/
public function processPayload(array $data) {
error_log("processPayload START");
// Registrar como recibido (reproducción)
$this->logWebhook(json_encode($data), json_encode(['status' => 'replayed']), 200);
if (empty($data) || !isset($data['entry'])) {
error_log("processPayload: empty or missing entry");
return false;
}
foreach ($data['entry'] as $entry) {
if (isset($entry['changes'])) {
foreach ($entry['changes'] as $change) {
error_log("processPayload: processing change field=" . ($change['field'] ?? ''));
if (($change['field'] ?? '') === 'messages' && isset($change['value'])) {
try {
$this->processMessages($change['value']);
} catch (Exception $e) {
error_log("processPayload: processMessages failed: " . $e->getMessage());
}
}
}
}
}
error_log("processPayload END");
return true;
}
}
// Procesar la solicitud
try {
$webhook = new WhatsAppWebhook();
$webhook->handleRequest();
} catch (Exception $e) {
error_log("Fatal error in webhook: " . $e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'Error fatal del servidor']);
// Procesar la solicitud (solo si no estamos en CLI)
if (php_sapi_name() !== 'cli') {
try {
$webhook = new WhatsAppWebhook();
$webhook->handleRequest();
} catch (Exception $e) {
error_log("Fatal error in webhook: " . $e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'Error fatal del servidor']);
}
}
?>