#!/bin/bash # init_telethon_session.sh - Interactive Telethon session initialization # This script helps authorize the Telethon UserBot by running it in the Docker container set -e CONTAINER_NAME="tg_autoposter_userbot" SCRIPT_NAME="/app/init_telethon_session.py" echo "==================================" echo "šŸ” Telethon Session Initialization" echo "==================================" echo "" # Check if container is running if ! docker-compose ps | grep -q "tg_autoposter_userbot.*Up"; then echo "āš ļø UserBot container is not running" echo "Starting containers..." docker-compose up -d sleep 5 fi echo "šŸ”— Running initialization in Docker container..." echo "" echo "šŸ“± Phone: $(grep TELETHON_PHONE .env | cut -d'=' -f2)" echo "" # Run the initialization script inside the container with interactive input docker-compose exec -it userbot python3 <<'EOF' import asyncio import os import sys from pathlib import Path from telethon import TelegramClient from telethon.sessions import StringSession # Get Telethon credentials TELETHON_API_ID = os.getenv('TELETHON_API_ID') TELETHON_API_HASH = os.getenv('TELETHON_API_HASH') TELETHON_PHONE = os.getenv('TELETHON_PHONE') # Session paths SESSION_FILE = '/app/app/sessions/userbot_session.session' SESSION_STRING_FILE = '/app/app/sessions/telethon_string_session.txt' async def initialize_session(): """Initialize Telethon session with interactive authentication""" print("\n" + "="*80) print("šŸš€ Telethon Session Initialization") print("="*80) # Verify credentials if not all([TELETHON_API_ID, TELETHON_API_HASH, TELETHON_PHONE]): print("āŒ Missing Telethon credentials!") return False try: # Create session directory os.makedirs('/app/app/sessions', exist_ok=True) # Create Telethon client client = TelegramClient( SESSION_FILE, api_id=int(TELETHON_API_ID), api_hash=TELETHON_API_HASH ) print(f"\nšŸ“± Phone: {TELETHON_PHONE}") print(f"šŸ” API ID: {TELETHON_API_ID}") # Connect to Telegram print("\nšŸ”— Connecting to Telegram...") await client.connect() print("āœ… Connected!") # Check if already authorized if await client.is_user_authorized(): print("\nā„¹ļø Session already authorized!") me = await client.get_me() print(f"šŸ‘¤ User: {me.first_name} (@{me.username})") await client.disconnect() return True # Start authorization flow print("\nšŸ” Starting authorization process...") print(f"šŸ“² You will receive an SMS code to {TELETHON_PHONE}") # Send auth code request await client.send_code_request(TELETHON_PHONE) # Get code from user code = input("\nšŸ“ Enter the SMS code: ").strip() try: await client.sign_in(TELETHON_PHONE, code) print("āœ… Successfully signed in!") except Exception as e: if "PHONE_CODE_INVALID" in str(e): print("āŒ Invalid SMS code. Please try again.") return False # Might need 2FA password = input("\nšŸ”’ 2FA Password (if enabled): ").strip() if password: await client.sign_in(password=password) print("āœ… Successfully signed in with 2FA!") else: print("āŒ Failed to sign in") return False # Verify and save session me = await client.get_me() print(f"\nāœ… Session initialized for: {me.first_name} (@{me.username})") print(f" ID: {me.id}") # Save string session as backup string_session = StringSession.save(client.session) with open(SESSION_STRING_FILE, 'w') as f: f.write(string_session) print(f"\nšŸ’¾ Session saved!") print(f" {SESSION_FILE}") # Disconnect await client.disconnect() print("\nāœ… Session initialization completed successfully!") return True except Exception as e: print(f"\nāŒ Error: {e}") return False asyncio.run(initialize_session()) EOF echo "" echo "==================================" echo "āœ… Done!" echo "==================================" echo "" echo "Restarting UserBot container..." docker-compose restart userbot echo "" echo "Checking logs..." sleep 3 docker-compose logs userbot --tail 20