06-sep: [fix] menu usa estado vivo (__main__) en vez de copia vacia
This commit is contained in:
@@ -8,6 +8,7 @@ exactamente igual. Se importa pi_bot de forma perezosa para evitar ciclos.
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
|
||||
API = "https://api.telegram.org/bot"
|
||||
@@ -17,7 +18,7 @@ PREFIJO = "m:"
|
||||
|
||||
|
||||
def _bot_token():
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
return pi_bot.TOKEN
|
||||
|
||||
|
||||
@@ -25,8 +26,19 @@ def _api(method):
|
||||
return f"{API}{_bot_token()}/{method}"
|
||||
|
||||
|
||||
def _log(linea):
|
||||
def _pi():
|
||||
"""Devuelve el modulo vivo de pi_bot. El bot corre como __main__, asi
|
||||
que `import pi_bot` cargaria una SEGUNDA copia con estado vacio:
|
||||
hay que usar sys.modules['__main__'] cuando sea el bot."""
|
||||
main = sys.modules.get("__main__")
|
||||
if main is not None and hasattr(main, "ultima_titulos"):
|
||||
return main
|
||||
import pi_bot
|
||||
return pi_bot
|
||||
|
||||
|
||||
def _log(linea):
|
||||
pi_bot = _pi()
|
||||
try:
|
||||
pi_bot.log(f"menu: {linea}")
|
||||
except Exception:
|
||||
@@ -35,7 +47,7 @@ def _log(linea):
|
||||
|
||||
def _txt(codigo, chat_id):
|
||||
"""Recupera el estado de la ultima busqueda de pi_bot para este chat."""
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
try:
|
||||
if codigo == "ultimos_titulos":
|
||||
estado = pi_bot.ultima_titulos.get(chat_id) or {}
|
||||
@@ -309,7 +321,7 @@ def teclado_dispositivos(lista):
|
||||
|
||||
|
||||
def _mostrar_dispositivos(chat_id, message_id):
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
try:
|
||||
from asus_api_control import listar_dispositivos
|
||||
lista = listar_dispositivos()
|
||||
@@ -386,7 +398,7 @@ def _cerrar_menu(chat_id, message_id):
|
||||
|
||||
def _disp(chat_id, message_id, comando, categoria=None):
|
||||
"""Ejecuta un comando, cierra el menu viejo y devuelve al menu."""
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
pi_bot.procesar(comando, chat_id)
|
||||
_cerrar_menu(chat_id, message_id)
|
||||
_reenviar_menu(chat_id, categoria)
|
||||
@@ -396,7 +408,7 @@ def _fin_accion(chat_id, categoria=None):
|
||||
"""Tras una accion terminal: deja ver el resultado, limpia toda la
|
||||
pantalla y devuelve al menu de la categoria."""
|
||||
import time as _time
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
_time.sleep(4)
|
||||
try:
|
||||
pi_bot.limpiar_pantalla(chat_id)
|
||||
@@ -409,7 +421,7 @@ def _reabrir(chat_id, message_id, categoria, comando):
|
||||
"""Para consultas (estado, listar): ejecuta, borra los mensajes viejos
|
||||
dejando solo el ultimo resultado y muestra de nuevo el menu de la
|
||||
categoria (con su tecla de volver operativa)."""
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
pi_bot.procesar(comando, chat_id)
|
||||
try:
|
||||
hist = pi_bot.historial_chat.get(chat_id, []) or []
|
||||
@@ -464,7 +476,14 @@ def _record_cat(chat_id, categoria):
|
||||
|
||||
def _reenviar_menu(chat_id, categoria=None):
|
||||
"""Envia de nuevo el menu de la categoria (con su volver operativo)."""
|
||||
pi_bot = _pi()
|
||||
cat = categoria or menu_cat.get(chat_id)
|
||||
try:
|
||||
nt = len((pi_bot.ultima_titulos.get(chat_id) or {}).get("results", []))
|
||||
nb = len((pi_bot.ultimos_torrents.get(chat_id) or {}).get("torrents", []))
|
||||
_log(f"reenviar cat={cat} chat={chat_id} titulos={nt} torrents={nb}")
|
||||
except Exception:
|
||||
pass
|
||||
if not cat:
|
||||
_enviar_nuevo(chat_id, "🧭 <b>Menú IDD</b>\nElige una categoría:",
|
||||
teclado_principal())
|
||||
@@ -526,7 +545,7 @@ def _abrir_categoria(chat_id, message_id, categoria):
|
||||
if categoria == "videoclub":
|
||||
# Al volver atras se olvida la eleccion de torrents (se queda Torrents).
|
||||
try:
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
pi_bot.ultimos_torrents.pop(chat_id, None)
|
||||
except Exception:
|
||||
pass
|
||||
@@ -612,7 +631,7 @@ def procesar_callback(data, chat_id, message_id):
|
||||
_cerrar_menu(chat_id, message_id)
|
||||
return
|
||||
minutos = partes[1]
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
pi_bot.procesar(f"parental pausa {nombre} {minutos}", chat_id)
|
||||
_cerrar_menu(chat_id, message_id)
|
||||
_fin_accion(chat_id, "parental")
|
||||
@@ -660,14 +679,14 @@ def _ejecutar_parental(chat_id, message_id, accion, nombre):
|
||||
teclado_minutos_pausa(nombre))
|
||||
return
|
||||
comando = f"parental {'desactivar' if accion == 'desactivar' else 'activar'} {nombre}"
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
pi_bot.procesar(comando, chat_id)
|
||||
_cerrar_menu(chat_id, message_id)
|
||||
_fin_accion(chat_id, "parental")
|
||||
|
||||
|
||||
def _ejecutar_accion(chat_id, message_id, partes):
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
accion = partes[0]
|
||||
sub = partes[1] if len(partes) > 1 else ""
|
||||
directos = {
|
||||
@@ -793,7 +812,7 @@ def _ejecutar_accion(chat_id, message_id, partes):
|
||||
# --------------------------------------------- pendiente de texto libre ----
|
||||
|
||||
def _pend(chat_id):
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
return pi_bot.menu_pendiente
|
||||
|
||||
|
||||
@@ -854,24 +873,24 @@ def resolver_pausa_minutos(chat_id, texto):
|
||||
_limpiar_pendiente(chat_id)
|
||||
if not nombre:
|
||||
return True
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
pi_bot.procesar(f"parental pausa {nombre} {minutos}", chat_id)
|
||||
_reenviar_menu(chat_id, "parental")
|
||||
return True
|
||||
|
||||
|
||||
def pausa_estado(chat_id):
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
return pi_bot.menu_pausa_nombre.get(chat_id)
|
||||
|
||||
|
||||
def _set_pausa_nombre(chat_id, nombre):
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
pi_bot.menu_pausa_nombre[chat_id] = nombre
|
||||
|
||||
|
||||
def _get_pausa_nombre(chat_id):
|
||||
import pi_bot
|
||||
pi_bot = _pi()
|
||||
return pi_bot.menu_pausa_nombre.get(chat_id)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user