p
This commit is contained in:
+69
-4
@@ -790,21 +790,68 @@ class BotService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Mostrar menú principal
|
||||
* Mostrar menú principal.
|
||||
* Si no hay asesores disponibles (advisor_available=0) y existe un menú de tipo
|
||||
* 'no_advisor' activo, lo muestra primero en lugar del menú raíz.
|
||||
*/
|
||||
private function showMainMenu($phoneNumber) {
|
||||
try { error_log("[BotService] showMainMenu - phone={$phoneNumber} fetching root menu"); } catch (Throwable $t) {}
|
||||
|
||||
// Verificar disponibilidad de asesores
|
||||
$advisorAvailable = true;
|
||||
try {
|
||||
$cfg = $this->db->fetch(
|
||||
"SELECT config_value FROM system_config WHERE config_key = 'advisor_available' LIMIT 1"
|
||||
);
|
||||
if ($cfg !== null && $cfg !== false) {
|
||||
$advisorAvailable = (bool)(int)$cfg['config_value'];
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log('[BotService] showMainMenu - failed to read advisor_available: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
// Si no hay asesor disponible, intentar mostrar el menú secundario primero
|
||||
if (!$advisorAvailable) {
|
||||
try {
|
||||
// Verificar que la columna menu_type ya existe (puede no existir si la migración aún no corrió).
|
||||
// Se cachea en static para no hacer SHOW COLUMNS en cada mensaje.
|
||||
static $menuTypeColChecked = null;
|
||||
if ($menuTypeColChecked === null) {
|
||||
$colExists = $this->db->fetchAll("SHOW COLUMNS FROM menus LIKE 'menu_type'");
|
||||
$menuTypeColChecked = !empty($colExists);
|
||||
if (!$menuTypeColChecked) {
|
||||
error_log('[BotService] showMainMenu - column menu_type not yet migrated, run run_20260527_menu_type.php');
|
||||
}
|
||||
}
|
||||
|
||||
if ($menuTypeColChecked) {
|
||||
$noAdvisorMenu = $this->db->fetch(
|
||||
"SELECT * FROM menus WHERE menu_type = 'no_advisor' AND is_active = 1 ORDER BY order_position, id LIMIT 1"
|
||||
);
|
||||
if ($noAdvisorMenu) {
|
||||
try { error_log("[BotService] showMainMenu - no advisor available, showing no_advisor menu id={$noAdvisorMenu['id']} phone={$phoneNumber}"); } catch (Throwable $t) {}
|
||||
$this->showMenu($phoneNumber, $noAdvisorMenu['id']);
|
||||
return;
|
||||
}
|
||||
error_log('[BotService] showMainMenu - advisor absent but no no_advisor menu configured, falling back to root');
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log('[BotService] showMainMenu - error fetching no_advisor menu: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Menú raíz principal (comportamiento normal)
|
||||
$mainMenu = $this->db->fetch(
|
||||
"SELECT * FROM menus WHERE is_root = 1 AND is_active = 1 ORDER BY order_position LIMIT 1"
|
||||
);
|
||||
try { error_log("[BotService] showMainMenu - fetched root menu=" . json_encode($mainMenu)); } catch (Throwable $t) {}
|
||||
|
||||
|
||||
if ($mainMenu) {
|
||||
$this->showMenu($phoneNumber, $mainMenu['id']);
|
||||
} else {
|
||||
try { error_log("[BotService] showMainMenu - no root menu found for phone={$phoneNumber}"); } catch (Throwable $t) {}
|
||||
$this->whatsappService->sendTextMessage(
|
||||
$phoneNumber,
|
||||
$phoneNumber,
|
||||
"❌ No hay menús configurados. Contacte con soporte."
|
||||
);
|
||||
}
|
||||
@@ -1224,9 +1271,27 @@ class BotService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviar mensaje por defecto cuando no hay coincidencias
|
||||
* Enviar mensaje por defecto cuando no hay coincidencias.
|
||||
* Si el asesor está marcado como ausente (advisor_available=0), muestra el menú
|
||||
* secundario en lugar del mensaje genérico, para que todos los clientes que escriban
|
||||
* vean el menú de asesor ausente hasta que se reactive la disponibilidad.
|
||||
*/
|
||||
private function sendDefaultNoMatch($phoneNumber) {
|
||||
// Si el asesor está ausente, redirigir al menú secundario
|
||||
try {
|
||||
$cfg = $this->db->fetch(
|
||||
"SELECT config_value FROM system_config WHERE config_key = 'advisor_available' LIMIT 1"
|
||||
);
|
||||
$advisorAbsent = ($cfg !== null && $cfg !== false) && !(bool)(int)$cfg['config_value'];
|
||||
if ($advisorAbsent) {
|
||||
error_log("[BotService] sendDefaultNoMatch - advisor absent, redirecting to showMainMenu for phone={$phoneNumber}");
|
||||
$this->showMainMenu($phoneNumber);
|
||||
return;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log('[BotService] sendDefaultNoMatch - failed to read advisor_available: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$defaultMessage = "🤖 No entendí su mensaje. Escriba *MENU* para ver las opciones disponibles o *ASESOR* para obtener ayuda.";
|
||||
$this->whatsappService->sendTextMessage($phoneNumber, $defaultMessage);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user