Spaces:
Sleeping
Sleeping
Download scripts/login.py from codebydante/TwistedStreamingBrody: direct link, hf CLI and curl.
- Browser
- Download file 2.27 kB
-
https://huggingface.co/spaces/codebydante/TwistedStreamingBrody/resolve/main/scripts/login.py
- Command line
-
hf download hf://spaces/codebydante/TwistedStreamingBrody/scripts/login.py
-
curl -L -o login.py https://huggingface.co/spaces/codebydante/TwistedStreamingBrody/resolve/main/scripts/login.py
2.27 kB
| import os | |
| import sys | |
| from telethon.sync import TelegramClient | |
| from telethon.sessions import StringSession | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Credenciales de API de Telegram (App Oficial para generación de sesión directa) | |
| DEFAULT_API_ID = 2040 | |
| DEFAULT_API_HASH = "b18441a1ff607e10a989891a5462e627" | |
| def main(): | |
| print("=" * 55) | |
| print(" INICIO DE SESIÓN TELEGRAM (USERBOT)") | |
| print("=" * 55) | |
| api_id = int(os.getenv("API_ID") or DEFAULT_API_ID) | |
| api_hash = os.getenv("API_HASH") or DEFAULT_API_HASH | |
| phone = input("\n👉 Ingresa tu número de teléfono (ejemplo: +51963264225): ").strip() | |
| if not phone: | |
| print("[ERROR] El número de teléfono no puede estar vacío.") | |
| sys.exit(1) | |
| print("\nConectando con Telegram...") | |
| base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| sessions_dir = os.path.join(base_dir, "sessions") | |
| os.makedirs(sessions_dir, exist_ok=True) | |
| session_file_path = os.path.join(sessions_dir, "userbot") | |
| # Iniciar con archivo .session físico | |
| client = TelegramClient(session_file_path, api_id, api_hash) | |
| client.start(phone=phone) | |
| # También obtener el StringSession para cuando quieras subirlo a HF Space | |
| from telethon.sessions import StringSession | |
| temp_client = TelegramClient(StringSession(), api_id, api_hash) | |
| temp_client.session.set_dc(client.session.dc_id, client.session.server_address, client.session.port) | |
| temp_client.session.auth_key = client.session.auth_key | |
| session_str = temp_client.session.save() | |
| client.disconnect() | |
| print("\n" + "=" * 55) | |
| print("¡AUTENTICACIÓN EXITOSA!") | |
| print("=" * 55) | |
| print(f"✅ Archivo de sesión creado en: sessions/userbot.session") | |
| print(f"✅ SESSION_STRING para HF Spaces:\n{session_str}") | |
| print("=" * 55) | |
| # Guardar en archivo .env automáticamente en la raíz | |
| env_path = os.path.join(base_dir, ".env") | |
| env_content = f"API_ID={api_id}\nAPI_HASH={api_hash}\nSESSION_STRING={session_str}\nPORT=7860\n" | |
| with open(env_path, "w", encoding="utf-8") as f: | |
| f.write(env_content) | |
| print(f"\n[OK] Configuración guardada en '.env'") | |
| if __name__ == "__main__": | |
| main() | |