Files
TG_autoposter/authorize_userbot_simple.sh
Andrew K. Choi e57ca0b36e Add simplified UserBot authorization script and quick guide
NEW FILES:
 authorize_userbot_simple.sh - Simple one-command authorization
 QUICK_AUTH_GUIDE.md - Quick authorization reference
 AUTH_INSTRUCTIONS.txt - Pre-authorization instructions

IMPROVEMENTS:
 Simpler, more user-friendly authorization process
 Direct Python-based authentication (no bash complexity)
 Clear prompts and status messages
 Better error handling
 Works with interactive terminal (docker-compose exec -it)

USAGE:
  ./authorize_userbot_simple.sh
  [Wait for SMS]
  [Enter code when prompted]

This provides two options:
1. Simple mode: ./authorize_userbot_simple.sh
2. Original mode: ./init_telethon_session.sh

Both do the same thing, simple mode is more straightforward for users.
2025-12-21 12:18:05 +09:00

109 lines
3.8 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Simple Telethon Authorization Script - Direct Mode
# Just run: ./authorize_userbot_simple.sh
set -e
CONTAINER_NAME="tg_autoposter_userbot"
echo "════════════════════════════════════════════════════════════════"
echo "🔐 Telethon UserBot Authorization"
echo "════════════════════════════════════════════════════════════════"
echo ""
# Get phone from .env
PHONE=$(grep TELETHON_PHONE .env | cut -d'=' -f2 | tr -d '\r')
echo "📱 Phone: $PHONE"
echo "🔗 Connecting to Telegram in Docker container..."
echo ""
echo "⏳ You will receive an SMS code to $PHONE"
echo ""
# Run the authorization inside Docker
docker-compose exec -it userbot python3 - << 'PYTHON_SCRIPT'
import asyncio
import os
from telethon import TelegramClient
async def auth():
api_id = int(os.getenv('TELETHON_API_ID'))
api_hash = os.getenv('TELETHON_API_HASH')
phone = os.getenv('TELETHON_PHONE')
session_file = '/app/app/sessions/userbot_session'
print("\n" + "="*80)
print("🚀 Starting Authorization")
print("="*80)
client = TelegramClient(session_file, api_id, api_hash)
try:
await client.connect()
print("✅ Connected to Telegram!\n")
if await client.is_user_authorized():
print(" Already authorized!")
me = await client.get_me()
print(f"👤 User: {me.first_name} (@{me.username})")
else:
print("🔐 Sending SMS code request...")
await client.send_code_request(phone)
print("📲 SMS sent! Check your messages.\n")
code = input("📝 Enter the SMS code: ").strip()
try:
await client.sign_in(phone, code)
print("\n✅ Successfully signed in!")
except Exception as e:
if "PHONE_CODE_INVALID" in str(e):
print("❌ Invalid code. Try again.")
return False
print(f"⚠️ {e}")
password = input("🔒 Enter 2FA password (if needed): ").strip()
if password:
await client.sign_in(password=password)
print("✅ Successfully signed in with 2FA!")
else:
return False
me = await client.get_me()
print(f"\n👤 Authorized as: {me.first_name} (@{me.username})")
await client.disconnect()
print("\n✅ Session saved successfully!")
return True
except Exception as e:
print(f"\n❌ Error: {e}")
return False
try:
result = asyncio.run(auth())
if not result:
exit(1)
except KeyboardInterrupt:
print("\n\n❌ Interrupted by user")
exit(1)
PYTHON_SCRIPT
if [ $? -eq 0 ]; then
echo ""
echo "════════════════════════════════════════════════════════════════"
echo "🎉 Authorization Complete!"
echo "════════════════════════════════════════════════════════════════"
echo ""
echo "Restarting UserBot container..."
docker-compose restart userbot
echo ""
sleep 3
echo "Checking status..."
docker-compose logs userbot --tail 10 | grep -E "инициализирован|ERROR|готов"
else
echo ""
echo "❌ Authorization failed. Try again."
exit 1
fi