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 json
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
API = "https://api.telegram.org/bot"
|
API = "https://api.telegram.org/bot"
|
||||||
@@ -17,7 +18,7 @@ PREFIJO = "m:"
|
|||||||
|
|
||||||
|
|
||||||
def _bot_token():
|
def _bot_token():
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
return pi_bot.TOKEN
|
return pi_bot.TOKEN
|
||||||
|
|
||||||
|
|
||||||
@@ -25,8 +26,19 @@ def _api(method):
|
|||||||
return f"{API}{_bot_token()}/{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
|
import pi_bot
|
||||||
|
return pi_bot
|
||||||
|
|
||||||
|
|
||||||
|
def _log(linea):
|
||||||
|
pi_bot = _pi()
|
||||||
try:
|
try:
|
||||||
pi_bot.log(f"menu: {linea}")
|
pi_bot.log(f"menu: {linea}")
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -35,7 +47,7 @@ def _log(linea):
|
|||||||
|
|
||||||
def _txt(codigo, chat_id):
|
def _txt(codigo, chat_id):
|
||||||
"""Recupera el estado de la ultima busqueda de pi_bot para este chat."""
|
"""Recupera el estado de la ultima busqueda de pi_bot para este chat."""
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
try:
|
try:
|
||||||
if codigo == "ultimos_titulos":
|
if codigo == "ultimos_titulos":
|
||||||
estado = pi_bot.ultima_titulos.get(chat_id) or {}
|
estado = pi_bot.ultima_titulos.get(chat_id) or {}
|
||||||
@@ -309,7 +321,7 @@ def teclado_dispositivos(lista):
|
|||||||
|
|
||||||
|
|
||||||
def _mostrar_dispositivos(chat_id, message_id):
|
def _mostrar_dispositivos(chat_id, message_id):
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
try:
|
try:
|
||||||
from asus_api_control import listar_dispositivos
|
from asus_api_control import listar_dispositivos
|
||||||
lista = 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):
|
def _disp(chat_id, message_id, comando, categoria=None):
|
||||||
"""Ejecuta un comando, cierra el menu viejo y devuelve al menu."""
|
"""Ejecuta un comando, cierra el menu viejo y devuelve al menu."""
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
pi_bot.procesar(comando, chat_id)
|
pi_bot.procesar(comando, chat_id)
|
||||||
_cerrar_menu(chat_id, message_id)
|
_cerrar_menu(chat_id, message_id)
|
||||||
_reenviar_menu(chat_id, categoria)
|
_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
|
"""Tras una accion terminal: deja ver el resultado, limpia toda la
|
||||||
pantalla y devuelve al menu de la categoria."""
|
pantalla y devuelve al menu de la categoria."""
|
||||||
import time as _time
|
import time as _time
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
_time.sleep(4)
|
_time.sleep(4)
|
||||||
try:
|
try:
|
||||||
pi_bot.limpiar_pantalla(chat_id)
|
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
|
"""Para consultas (estado, listar): ejecuta, borra los mensajes viejos
|
||||||
dejando solo el ultimo resultado y muestra de nuevo el menu de la
|
dejando solo el ultimo resultado y muestra de nuevo el menu de la
|
||||||
categoria (con su tecla de volver operativa)."""
|
categoria (con su tecla de volver operativa)."""
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
pi_bot.procesar(comando, chat_id)
|
pi_bot.procesar(comando, chat_id)
|
||||||
try:
|
try:
|
||||||
hist = pi_bot.historial_chat.get(chat_id, []) or []
|
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):
|
def _reenviar_menu(chat_id, categoria=None):
|
||||||
"""Envia de nuevo el menu de la categoria (con su volver operativo)."""
|
"""Envia de nuevo el menu de la categoria (con su volver operativo)."""
|
||||||
|
pi_bot = _pi()
|
||||||
cat = categoria or menu_cat.get(chat_id)
|
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:
|
if not cat:
|
||||||
_enviar_nuevo(chat_id, "🧭 <b>Menú IDD</b>\nElige una categoría:",
|
_enviar_nuevo(chat_id, "🧭 <b>Menú IDD</b>\nElige una categoría:",
|
||||||
teclado_principal())
|
teclado_principal())
|
||||||
@@ -526,7 +545,7 @@ def _abrir_categoria(chat_id, message_id, categoria):
|
|||||||
if categoria == "videoclub":
|
if categoria == "videoclub":
|
||||||
# Al volver atras se olvida la eleccion de torrents (se queda Torrents).
|
# Al volver atras se olvida la eleccion de torrents (se queda Torrents).
|
||||||
try:
|
try:
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
pi_bot.ultimos_torrents.pop(chat_id, None)
|
pi_bot.ultimos_torrents.pop(chat_id, None)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -612,7 +631,7 @@ def procesar_callback(data, chat_id, message_id):
|
|||||||
_cerrar_menu(chat_id, message_id)
|
_cerrar_menu(chat_id, message_id)
|
||||||
return
|
return
|
||||||
minutos = partes[1]
|
minutos = partes[1]
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
pi_bot.procesar(f"parental pausa {nombre} {minutos}", chat_id)
|
pi_bot.procesar(f"parental pausa {nombre} {minutos}", chat_id)
|
||||||
_cerrar_menu(chat_id, message_id)
|
_cerrar_menu(chat_id, message_id)
|
||||||
_fin_accion(chat_id, "parental")
|
_fin_accion(chat_id, "parental")
|
||||||
@@ -660,14 +679,14 @@ def _ejecutar_parental(chat_id, message_id, accion, nombre):
|
|||||||
teclado_minutos_pausa(nombre))
|
teclado_minutos_pausa(nombre))
|
||||||
return
|
return
|
||||||
comando = f"parental {'desactivar' if accion == 'desactivar' else 'activar'} {nombre}"
|
comando = f"parental {'desactivar' if accion == 'desactivar' else 'activar'} {nombre}"
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
pi_bot.procesar(comando, chat_id)
|
pi_bot.procesar(comando, chat_id)
|
||||||
_cerrar_menu(chat_id, message_id)
|
_cerrar_menu(chat_id, message_id)
|
||||||
_fin_accion(chat_id, "parental")
|
_fin_accion(chat_id, "parental")
|
||||||
|
|
||||||
|
|
||||||
def _ejecutar_accion(chat_id, message_id, partes):
|
def _ejecutar_accion(chat_id, message_id, partes):
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
accion = partes[0]
|
accion = partes[0]
|
||||||
sub = partes[1] if len(partes) > 1 else ""
|
sub = partes[1] if len(partes) > 1 else ""
|
||||||
directos = {
|
directos = {
|
||||||
@@ -793,7 +812,7 @@ def _ejecutar_accion(chat_id, message_id, partes):
|
|||||||
# --------------------------------------------- pendiente de texto libre ----
|
# --------------------------------------------- pendiente de texto libre ----
|
||||||
|
|
||||||
def _pend(chat_id):
|
def _pend(chat_id):
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
return pi_bot.menu_pendiente
|
return pi_bot.menu_pendiente
|
||||||
|
|
||||||
|
|
||||||
@@ -854,24 +873,24 @@ def resolver_pausa_minutos(chat_id, texto):
|
|||||||
_limpiar_pendiente(chat_id)
|
_limpiar_pendiente(chat_id)
|
||||||
if not nombre:
|
if not nombre:
|
||||||
return True
|
return True
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
pi_bot.procesar(f"parental pausa {nombre} {minutos}", chat_id)
|
pi_bot.procesar(f"parental pausa {nombre} {minutos}", chat_id)
|
||||||
_reenviar_menu(chat_id, "parental")
|
_reenviar_menu(chat_id, "parental")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def pausa_estado(chat_id):
|
def pausa_estado(chat_id):
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
return pi_bot.menu_pausa_nombre.get(chat_id)
|
return pi_bot.menu_pausa_nombre.get(chat_id)
|
||||||
|
|
||||||
|
|
||||||
def _set_pausa_nombre(chat_id, nombre):
|
def _set_pausa_nombre(chat_id, nombre):
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
pi_bot.menu_pausa_nombre[chat_id] = nombre
|
pi_bot.menu_pausa_nombre[chat_id] = nombre
|
||||||
|
|
||||||
|
|
||||||
def _get_pausa_nombre(chat_id):
|
def _get_pausa_nombre(chat_id):
|
||||||
import pi_bot
|
pi_bot = _pi()
|
||||||
return pi_bot.menu_pausa_nombre.get(chat_id)
|
return pi_bot.menu_pausa_nombre.get(chat_id)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user