llm-telegram / generate_session.py
dragxd's picture
Add session corruption detection and recovery instructions
b989b58
#!/usr/bin/env python3
"""
Pyrogram Bot Session File Generator
Generates a .session file for Pyrogram bot authentication using bot token
"""
import os
import sys
from pyrogram import Client
import logging
# Configure logging
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
def generate_session():
"""
Generate a Pyrogram session file (.session) for bot token
This script will:
1. Get bot token from environment or user input
2. Get API_ID and API_HASH
3. Generate and save .session file in root directory
"""
logger.info("=" * 60)
logger.info("Pyrogram Bot Session File Generator")
logger.info("=" * 60)
logger.info("")
# Get bot token
bot_token = os.getenv("BOT_TOKEN")
if not bot_token:
bot_token = input("πŸ€– Enter your bot token (from @BotFather): ").strip()
if not bot_token:
logger.error("❌ Bot token is required!")
sys.exit(1)
logger.info(f"βœ… Bot token: {bot_token[:20]}***")
logger.info("")
# Get API credentials from environment or user input
api_id = os.getenv("API_ID")
if not api_id:
api_id = input("πŸ“± Enter your API_ID: ").strip()
api_hash = os.getenv("API_HASH")
if not api_hash:
api_hash = input("πŸ”‘ Enter your API_HASH: ").strip()
if not api_id or not api_hash:
logger.error("❌ Missing API credentials!")
logger.error("")
logger.error("To get API_ID and API_HASH:")
logger.error("1. Go to https://my.telegram.org/auth")
logger.error("2. Login with your phone number")
logger.error("3. Go to 'API development tools'")
logger.error("4. Create an application")
logger.error("5. Copy your API_ID and API_HASH")
sys.exit(1)
logger.info(f"βœ… API_ID: {api_id}")
logger.info(f"βœ… API_HASH: {api_hash[:10]}***")
logger.info("")
# Create client with bot token
client = Client(
name="bot",
api_id=int(api_id),
api_hash=api_hash,
bot_token=bot_token,
workdir="." # Save session file in current directory
)
try:
logger.info("πŸ” Connecting to Telegram...")
logger.info("")
with client:
logger.info("βœ… Successfully authenticated with bot token!")
logger.info("")
# Get bot info
me = client.get_me()
logger.info(f"Bot Name: {me.first_name}")
logger.info(f"Bot Username: @{me.username}")
logger.info(f"Bot ID: {me.id}")
logger.info("")
# Check if session file was created
session_file = "bot.session"
if os.path.exists(session_file):
logger.info("=" * 60)
logger.info("βœ… Session file generated successfully!")
logger.info("=" * 60)
logger.info("")
logger.info(f"πŸ“ Session file: {os.path.abspath(session_file)}")
logger.info("")
logger.info("Next steps:")
logger.info("1. Start the bot:")
logger.info(" - python3 app.py")
logger.info("")
else:
logger.error("❌ Session file was not created!")
sys.exit(1)
except Exception as e:
logger.error(f"❌ Error during authentication: {e}")
logger.error("")
logger.error("Troubleshooting:")
logger.error("1. Verify your bot token is correct (get from @BotFather)")
logger.error("2. Verify your API_ID and API_HASH are correct")
logger.error("3. Check your internet connection")
sys.exit(1)
if __name__ == '__main__':
generate_session()