Spaces:
Paused
Paused
Download main.py from saske560/userbotsaskeds: direct link, hf CLI and curl.
- Browser
- Download file 5.16 kB
-
https://huggingface.co/spaces/saske560/userbotsaskeds/resolve/main/main.py
- Command line
-
hf download hf://spaces/saske560/userbotsaskeds/main.py
-
curl -L -o main.py https://huggingface.co/spaces/saske560/userbotsaskeds/resolve/main/main.py
5.16 kB
| """ | |
| main.py β Punto de entrada del UserBot. | |
| β’ Carga todos los plugins automΓ‘ticamente desde /plugins | |
| β’ Inicia un servidor HTTP mΓnimo de health-check en PORT (7860) | |
| para mantener el Space de Hugging Face vivo | |
| β’ Imprime tabla de comandos al iniciar | |
| """ | |
| import asyncio | |
| import logging | |
| import os | |
| import sys | |
| import time | |
| import importlib | |
| import pkgutil | |
| from pathlib import Path | |
| from aiohttp import web | |
| from pyrogram import idle | |
| from config import Config | |
| # ββ Logging βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s β %(levelname)-8s β %(name)s β %(message)s", | |
| datefmt="%H:%M:%S", | |
| ) | |
| log = logging.getLogger("userbot") | |
| # Silence noisy libs | |
| for lib in ("pyrogram", "asyncio", "aiohttp.access"): | |
| logging.getLogger(lib).setLevel(logging.WARNING) | |
| # ββ Sanity checks βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def _check_env(): | |
| missing = [k for k in ("API_ID", "API_HASH", "SESSION_STRING") | |
| if not os.environ.get(k)] | |
| if missing: | |
| log.error("β Variables de entorno faltantes: %s", ", ".join(missing)) | |
| log.error(" ConfigΓΊralas en HF Spaces β Settings β Secrets") | |
| sys.exit(1) | |
| # ββ Plugin loader βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def _load_plugins(): | |
| plugins_dir = Path(__file__).parent / "plugins" | |
| loaded = [] | |
| for _, name, _ in pkgutil.iter_modules([str(plugins_dir)]): | |
| try: | |
| importlib.import_module(f"plugins.{name}") | |
| loaded.append(name) | |
| log.info(" β Plugin cargado: %s", name) | |
| except Exception as e: | |
| log.error(" β Error cargando plugin '%s': %s", name, e) | |
| return loaded | |
| # ββ Minimal health-check HTTP server βββββββββββββββββββββββββββββββββββββββββ | |
| async def _start_web(): | |
| async def health(_): | |
| return web.Response( | |
| text='{"status":"ok","userbot":"running"}', | |
| content_type="application/json", | |
| ) | |
| async def index(_): | |
| return web.Response( | |
| text="<h2>π€ TG UserBot β activo</h2>", | |
| content_type="text/html", | |
| ) | |
| app_web = web.Application() | |
| app_web.router.add_get("/", index) | |
| app_web.router.add_get("/health", health) | |
| runner = web.AppRunner(app_web) | |
| await runner.setup() | |
| site = web.TCPSite(runner, "0.0.0.0", Config.PORT) | |
| await site.start() | |
| log.info("π Health server escuchando en 0.0.0.0:%d", Config.PORT) | |
| # ββ Banner ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| BANNER = r""" | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β π€ TG UserBot β by Pyrogram β | |
| β βββββββββββββββββββββββββββββββββββββββββββββββββββ£ | |
| β Prefix : {prefix:<6} Owner-only : β β | |
| β Plugins: {plugins:<6} Health port: {port:<5} β | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| """.strip() | |
| # ββ Main ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def main(): | |
| _check_env() | |
| # Create needed directories | |
| for d in ("plugins", "utils", "data", "temp"): | |
| Path(d).mkdir(exist_ok=True) | |
| log.info("π Cargando plugins...") | |
| from bot import app as tg_app # triggers pyrogram Client creation | |
| plugins = _load_plugins() | |
| log.info("π Conectando a Telegram...") | |
| await tg_app.start() | |
| me = await tg_app.get_me() | |
| log.info("β Conectado como: %s (@%s | ID: %s)", | |
| me.first_name, me.username or "sin_username", me.id) | |
| print("\n" + BANNER.format( | |
| prefix=Config.CMD_PREFIX, | |
| plugins=len(plugins), | |
| port=Config.PORT, | |
| ) + "\n") | |
| # Start web health server (keeps HF Space alive) | |
| await _start_web() | |
| log.info("π UserBot listo. Escuchando mensajes...") | |
| await idle() | |
| log.info("π Deteniendo...") | |
| await tg_app.stop() | |
| if __name__ == "__main__": | |
| try: | |
| asyncio.run(main()) | |
| except KeyboardInterrupt: | |
| log.info("Detenido por el usuario.") | |