|
| 1 | +""" |
| 2 | +Non-interactive Telegram authentication helper. |
| 3 | +
|
| 4 | +Use this script when you cannot run the interactive setup_auth |
| 5 | +(e.g., from SSH without TTY, CI pipelines, or automation scripts). |
| 6 | +
|
| 7 | +Usage: |
| 8 | + # Step 1: Send verification code to Telegram app |
| 9 | + docker compose run --rm backup python scripts/auth_noninteractive.py send |
| 10 | +
|
| 11 | + # Step 2: Enter the code (and 2FA password if enabled) |
| 12 | + docker compose run --rm backup python scripts/auth_noninteractive.py verify CODE |
| 13 | + docker compose run --rm backup python scripts/auth_noninteractive.py verify CODE 2FA_PASSWORD |
| 14 | +
|
| 15 | +Environment variables required (same as normal operation): |
| 16 | + TELEGRAM_API_ID, TELEGRAM_API_HASH, TELEGRAM_PHONE, SESSION_NAME, BACKUP_PATH |
| 17 | +""" |
| 18 | + |
| 19 | +import asyncio |
| 20 | +import os |
| 21 | +import sys |
| 22 | + |
| 23 | +from telethon import TelegramClient |
| 24 | + |
| 25 | + |
| 26 | +def _get_session_path() -> str: |
| 27 | + """Derive session path from environment (same logic as Config).""" |
| 28 | + backup_path = os.getenv("BACKUP_PATH", "/data/backups") |
| 29 | + session_dir = os.getenv("SESSION_DIR", os.path.join(os.path.dirname(backup_path.rstrip("/\\")), "session")) |
| 30 | + session_name = os.getenv("SESSION_NAME", "telegram_backup") |
| 31 | + os.makedirs(session_dir, exist_ok=True) |
| 32 | + return os.path.join(session_dir, session_name) |
| 33 | + |
| 34 | + |
| 35 | +async def main(): |
| 36 | + if len(sys.argv) < 2 or sys.argv[1] in ("-h", "--help", "help"): |
| 37 | + print(__doc__) |
| 38 | + sys.exit(0) |
| 39 | + |
| 40 | + action = sys.argv[1] |
| 41 | + |
| 42 | + api_id = int(os.environ["TELEGRAM_API_ID"]) |
| 43 | + api_hash = os.environ["TELEGRAM_API_HASH"] |
| 44 | + phone = os.environ["TELEGRAM_PHONE"] |
| 45 | + session_path = _get_session_path() |
| 46 | + |
| 47 | + client = TelegramClient(session_path, api_id, api_hash) |
| 48 | + await client.connect() |
| 49 | + |
| 50 | + if await client.is_user_authorized(): |
| 51 | + me = await client.get_me() |
| 52 | + print(f"Already authorized as {me.first_name} (@{me.username})") |
| 53 | + await client.disconnect() |
| 54 | + return |
| 55 | + |
| 56 | + if action == "send": |
| 57 | + result = await client.send_code_request(phone) |
| 58 | + print(f"Verification code sent to Telegram app ({result.type.__class__.__name__})") |
| 59 | + print(f"Phone code hash: {result.phone_code_hash}") |
| 60 | + await client.disconnect() |
| 61 | + |
| 62 | + elif action == "verify": |
| 63 | + if len(sys.argv) < 3: |
| 64 | + print("Usage: python scripts/auth_noninteractive.py verify CODE [2FA_PASSWORD]") |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | + code = sys.argv[2] |
| 68 | + password = sys.argv[3] if len(sys.argv) > 3 else None |
| 69 | + |
| 70 | + # Must send_code_request first to establish the flow |
| 71 | + await client.send_code_request(phone) |
| 72 | + |
| 73 | + try: |
| 74 | + await client.sign_in(phone, code) |
| 75 | + except Exception as e: |
| 76 | + error_str = str(e) |
| 77 | + if "Two-steps verification" in error_str or "password" in error_str.lower() or "SessionPasswordNeeded" in error_str: |
| 78 | + if not password: |
| 79 | + print("2FA is enabled. Re-run with: verify CODE 2FA_PASSWORD") |
| 80 | + await client.disconnect() |
| 81 | + sys.exit(1) |
| 82 | + await client.sign_in(password=password) |
| 83 | + else: |
| 84 | + print(f"Authentication failed: {e}") |
| 85 | + await client.disconnect() |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | + me = await client.get_me() |
| 89 | + print(f"Authenticated as {me.first_name} (@{me.username})") |
| 90 | + await client.disconnect() |
| 91 | + |
| 92 | + else: |
| 93 | + print(f"Unknown action: {action}") |
| 94 | + print("Usage: send | verify CODE [2FA_PASSWORD]") |
| 95 | + sys.exit(1) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + asyncio.run(main()) |
0 commit comments