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
+24 -5
View File
@@ -63,13 +63,31 @@ class BotService {
try {
// Support masculino/femenino, singular/plural: enviado, enviada, enviados, enviadas
if (preg_match('/\b(enviad[oa]s?)\b/iu', $messageText)) {
$ack = "📄 Informacion recibida, Estaremos procesando su solicitud y confirmaremos en unos minutos";
// NO enviar respuesta al usuario (para conservar el indicador de atención en la lista).
// En su lugar, marcar la conversación como que requiere atención y crear un mensaje 'system'
try {
$this->whatsappService->sendTextMessage($phoneNumber, $ack);
$this->db->update('users', ['advisor_requested' => 1], 'phone_number = :phone', ['phone' => $phoneNumber]);
$sys = [
'system' => 'attention',
'type' => 'user_sent_documents',
'text' => 'El usuario indicó que ha enviado documentos o información; requiere atención.'
];
$this->db->insert('conversations', [
'user_id' => $user['id'],
'direction' => 'incoming',
'message_type' => 'text',
'content' => json_encode($sys),
'status' => 'received',
'created_at' => date('Y-m-d H:i:s')
]);
} catch (Exception $e) {
// ignore send errors
error_log('[BotService] failed to mark advisor_requested on envio: ' . $e->getMessage());
}
error_log("[BotService] processMessage - sent 'enviados' ack to user {$user['id']}");
error_log("[BotService] processMessage - marked advisor_requested for user {$user['id']} (no outgoing message sent)");
return;
}
} catch (Throwable $t) {
@@ -491,7 +509,8 @@ class BotService {
try {
$pausedUntil = date('Y-m-d H:i:s', strtotime("+{$minutes} minutes"));
// Guardar bandera y tiempo de expiración (usamos bot_paused_until que ya existe)
$this->db->update('users', ['on_hold' => 1, 'bot_paused_until' => $pausedUntil, 'advisor_requested' => 0], 'phone_number = :phone', ['phone' => $phoneNumber]);
$this->db->update('users', ['on_hold' => 1, 'bot_paused_until' => $pausedUntil, 'advisor_requested' => 1], 'phone_number = :phone', ['phone' => $phoneNumber]);
// Enviar mensaje (bot) marcando la solicitud; no incluimos metadata de operador para evitar limpiar la marca
$this->whatsappService->sendTextMessage($phoneNumber, "🔔 Se ha solicitado un asesor. Te hemos puesto en espera por {$minutes} minutos. Si no hay respuesta, podrás volver a usar *menu*." );
if (function_exists('writeLog')) writeLog('INFO', "Advisor requested for $phoneNumber until $pausedUntil");
} catch (Exception $e) {