diff --git a/services/NormalBot.php b/services/NormalBot.php index 6f70044..cfbcd78 100644 --- a/services/NormalBot.php +++ b/services/NormalBot.php @@ -1084,6 +1084,20 @@ class NormalBot }; } + // Reset manteniendo grupos de sesión (finca, etc.) para que el usuario + // no tenga que re-seleccionar su finca después de cada reporte. + private static function preservingReset(int $ctxId, array $meta): void + { + $keep = []; + foreach (['finca'] as $group) { + if (isset($meta[$group])) $keep[$group] = $meta[$group]; + } + ConversationContext::reset($ctxId); + if (!empty($keep)) { + ConversationContext::updateMetadata($ctxId, $keep); + } + } + // ── Resolves fixed-mode var values ─────────────────────────────────────── private static function resolveFixedVar(string $value): string { @@ -1141,7 +1155,7 @@ class NormalBot $nav = "\n\nEscribe *menu* para ver más opciones o *salir* para terminar."; if ($endpointKey === '') { - ConversationContext::reset($ctxId); + self::preservingReset($ctxId, ConversationContext::getMetadata($ctxId)); return self::sendText('Error: endpoint no configurado.' . $nav, $context['from'], $company); } @@ -1151,7 +1165,7 @@ class NormalBot if (!$ep || empty($ep['url'])) { self::log("API report: endpoint_key '{$endpointKey}' no configurado para company {$company['id']}"); - ConversationContext::reset($ctxId); + self::preservingReset($ctxId, ConversationContext::getMetadata($ctxId)); return self::sendText('El informe solicitado no está configurado. Contacta al administrador.' . $nav, $context['from'], $company); } @@ -1261,13 +1275,13 @@ class NormalBot if ($error) { self::log("API report error [{$endpointKey}]: {$error}"); - ConversationContext::reset($ctxId); + self::preservingReset($ctxId, $meta); return self::sendText('Error al obtener el reporte. Intenta de nuevo.' . $nav, $context['from'], $company); } if ($httpCode >= 400) { self::log("API report HTTP {$httpCode} [{$endpointKey}]: " . mb_substr($content, 0, 200)); - ConversationContext::reset($ctxId); + self::preservingReset($ctxId, $meta); return self::sendText('Error al obtener el reporte. Intenta de nuevo.' . $nav, $context['from'], $company); } @@ -1288,7 +1302,7 @@ class NormalBot unset($nextParams['next_endpoint_key'], $nextParams['next_caption'], $nextParams['next_filename']); return self::executeApiReport($nextParams, $context, $company, $ctxId); } - ConversationContext::reset($ctxId); + self::preservingReset($ctxId, $meta); return self::sendText('ℹ️ ' . $msg . $nav, $context['from'], $company); } @@ -1321,7 +1335,7 @@ class NormalBot if (!$upload['success'] || !$upload['media_id']) { self::log("API report upload failed [{$endpointKey}]: " . ($upload['error'] ?? 'unknown')); - ConversationContext::reset($ctxId); + self::preservingReset($ctxId, $meta); return self::sendText('Error al enviar el reporte. Intenta de nuevo.' . $nav, $context['from'], $company); } @@ -1329,7 +1343,7 @@ class NormalBot $caption = $params['caption'] ?? 'Aquí tienes el reporte solicitado.'; WhatsAppSender::sendDocument($context['from'], $upload['media_id'], $phoneNumberId, $caption, $reportName); - ConversationContext::reset($ctxId); + self::preservingReset($ctxId, $meta); return self::sendText('✅ Reporte generado.' . $nav, $context['from'], $company); }