Spaces:
Sleeping
Sleeping
Download src/bot.py from codebydante/TwistedStreamingBrody: direct link, hf CLI and curl.
- Browser
- Download file 2.34 kB
-
https://huggingface.co/spaces/codebydante/TwistedStreamingBrody/resolve/main/src/bot.py
- Command line
-
hf download hf://spaces/codebydante/TwistedStreamingBrody/src/bot.py
-
curl -L -o bot.py https://huggingface.co/spaces/codebydante/TwistedStreamingBrody/resolve/main/src/bot.py
2.34 kB
| import os | |
| import sys | |
| import threading | |
| import asyncio | |
| # Asegurar que el directorio src esté en sys.path | |
| BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| SRC_DIR = os.path.join(BASE_DIR, "src") | |
| if SRC_DIR not in sys.path: | |
| sys.path.insert(0, SRC_DIR) | |
| from telethon import TelegramClient | |
| from telethon.sessions import StringSession | |
| from config import API_ID, API_HASH, SESSION_STRING, PORT | |
| from web.server import start_server, bot_status | |
| from plugins import load_all_plugins | |
| SESSION_FILE = os.path.join(BASE_DIR, "sessions", "userbot.session") | |
| async def start_userbot(): | |
| # 1. Prioridad: Archivo .session local si existe | |
| if os.path.exists(SESSION_FILE): | |
| session_target = os.path.join(BASE_DIR, "sessions", "userbot") | |
| print(f"[*] Usando archivo de sesión: {SESSION_FILE}") | |
| client = TelegramClient(session_target, API_ID, API_HASH) | |
| # 2. StringSession para Hugging Face Spaces o variables de entorno | |
| elif SESSION_STRING: | |
| print("[*] Usando SESSION_STRING desde entorno.") | |
| client = TelegramClient(StringSession(SESSION_STRING), API_ID, API_HASH) | |
| else: | |
| bot_status["status"] = "ERROR: No hay archivo en sessions/userbot.session ni SESSION_STRING." | |
| print("[ERROR] No se encontró sessions/userbot.session ni variable SESSION_STRING. Ejecuta scripts/login.bat primero.") | |
| return | |
| # Cargar todos los comandos modulares desde la carpeta plugins/ | |
| load_all_plugins(client) | |
| try: | |
| await client.start() | |
| me = await client.get_me() | |
| user_name = f"@{me.username}" if me.username else f"{me.first_name} ({me.id})" | |
| bot_status["status"] = "Conectado y Escuchando" | |
| bot_status["user"] = user_name | |
| print(f"[OK] UserBot conectado exitosamente como: {user_name}") | |
| await client.run_until_disconnected() | |
| except Exception as e: | |
| bot_status["status"] = f"Error: {e}" | |
| print(f"[ERROR] Telethon: {e}") | |
| def main(): | |
| print(f"[*] Iniciando servidor web fantasma en el puerto {PORT}...") | |
| web_thread = threading.Thread(target=start_server, args=(PORT,), daemon=True) | |
| web_thread.start() | |
| print("[*] Iniciando Telegram UserBot...") | |
| asyncio.run(start_userbot()) | |
| if __name__ == "__main__": | |
| main() | |