This commit is contained in:
Lizandro Guarnizo
2026-01-22 14:10:03 -05:00
parent 74955aef32
commit 2aed2c428c
3 changed files with 62 additions and 16 deletions
+34 -9
View File
@@ -35,7 +35,7 @@ class WhatsAppService
/**
* Enviar mensaje de texto
*/
public function sendTextMessage($to, $message)
public function sendTextMessage($to, $message, $meta = null)
{
$data = [
'messaging_product' => 'whatsapp',
@@ -46,6 +46,11 @@ class WhatsAppService
]
];
if (!empty($meta) && is_array($meta)) {
// internal metadata used by our application (not sent to WhatsApp API)
$data['__app_meta'] = $meta;
}
return $this->sendMessage($data);
}
@@ -58,7 +63,8 @@ class WhatsAppService
$language = 'es',
$bodyParameters = [],
$headerParameters = [],
$rawComponents = null
$rawComponents = null,
$meta = null
) {
// Si se pasan components completos (por ejemplo flow/button/image), úsalos tal cual
if (is_array($rawComponents) && !empty($rawComponents)) {
@@ -140,6 +146,11 @@ class WhatsAppService
'template' => $template
];
if (!empty($meta) && is_array($meta)) {
// internal metadata used by our application (not sent to WhatsApp API)
$data['__app_meta'] = $meta;
}
return $this->sendMessage($data);
}
@@ -402,12 +413,18 @@ class WhatsAppService
// Construir URL correcto para enviar mensajes (use /messages)
$url = rtrim($this->apiUrl, '/') . '/' . $this->phoneNumberId . '/messages';
// Debug log
// Prepare payload (remove internal app meta before sending to WhatsApp API)
$payload = $data;
if (isset($payload['__app_meta'])) {
unset($payload['__app_meta']);
}
// Debug log (payload sent to WhatsApp)
error_log("WhatsApp API URL: " . $url);
error_log("WhatsApp Token length: " . strlen($this->token));
error_log("WhatsApp Data: " . json_encode($data));
error_log("WhatsApp Payload: " . json_encode($payload));
$response = $this->makeRequest('POST', $url, $data);
$response = $this->makeRequest('POST', $url, $payload);
// Guardar mensaje enviado en la base de datos (respuesta puede contener 'messages' o 'conversations')
$sentMessageId = null;
@@ -545,10 +562,14 @@ class WhatsAppService
$this->db->insert('conversations', $messageData);
// Si un asesor envía un mensaje, limpiar la solicitud de asesor (advisor_requested)
// Solo limpiar advisor_requested si el mensaje fue enviado por un asesor (operator)
try {
$this->db->update('users', ['advisor_requested' => 0, 'on_hold' => 0, 'bot_paused_until' => null], 'id = :id', ['id' => $user['id']]);
if (function_exists('writeLog')) writeLog('INFO', "Cleared advisor_requested for user {$user['id']} after outgoing message");
$cleared = false;
if (isset($data['__app_meta']) && is_array($data['__app_meta']) && !empty($data['__app_meta']['operator_id'])) {
$this->db->update('users', ['advisor_requested' => 0, 'on_hold' => 0, 'bot_paused_until' => null], 'id = :id', ['id' => $user['id']]);
$cleared = true;
}
if ($cleared && function_exists('writeLog')) writeLog('INFO', "Cleared advisor_requested for user {$user['id']} after outgoing message by operator");
} catch (Exception $e) {
error_log('Failed to clear advisor_requested after outgoing: ' . $e->getMessage());
}
@@ -610,7 +631,7 @@ class WhatsAppService
* @param bool $preview_url Incluir preview_url en body.text
* @param bool $dryRun Si true, no hace la petición y devuelve el payload
*/
public function sendTextReply($to, $messageId, $text, $preview_url = false, $dryRun = false)
public function sendTextReply($to, $messageId, $text, $preview_url = false, $dryRun = false, $meta = null)
{
$data = [
'messaging_product' => 'whatsapp',
@@ -625,6 +646,10 @@ class WhatsAppService
]
];
if (!empty($meta) && is_array($meta)) {
$data['__app_meta'] = $meta;
}
if ($dryRun) return $data;
return $this->sendMessage($data);
}