Spaces:
Paused
Paused
feat: Support bot.session file and improve bot session handling - Add support for bot.session file in root/sessions directory - Prioritize bot.session file over session strings - Improve bot_session_gen.py with better error handling - Fix async send_message warning - Update config template with bot session instructions
Browse files- config_template.env +5 -0
- pyUltroid/__init__.py +59 -31
- pyUltroid/configs.py +1 -1
- resources/session/bot_session_gen.py +147 -0
config_template.env
CHANGED
|
@@ -10,6 +10,11 @@ MONGO_URI=mongodb://localhost:27017/ultroid
|
|
| 10 |
# Bot token (if using bot mode)
|
| 11 |
BOT_TOKEN=your_bot_token_here
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# API credentials
|
| 14 |
API_ID=your_api_id
|
| 15 |
API_HASH=your_api_hash
|
|
|
|
| 10 |
# Bot token (if using bot mode)
|
| 11 |
BOT_TOKEN=your_bot_token_here
|
| 12 |
|
| 13 |
+
# Bot session string (alternative to BOT_TOKEN - generate using bot_session_gen.py)
|
| 14 |
+
# You can use either BOT_SESSION or bot.session as the key name
|
| 15 |
+
# BOT_SESSION=your_bot_session_string_here
|
| 16 |
+
# bot.session=your_bot_session_string_here
|
| 17 |
+
|
| 18 |
# API credentials
|
| 19 |
API_ID=your_api_id
|
| 20 |
API_HASH=your_api_hash
|
pyUltroid/__init__.py
CHANGED
|
@@ -78,8 +78,27 @@ if run_as_module:
|
|
| 78 |
asst = ultroid_bot
|
| 79 |
else:
|
| 80 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
BOT_SESSION = Var.BOT_SESSION or udB.get_key("BOT_SESSION") or udB.get_key("bot.session")
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
asst = UltroidClient(
|
| 84 |
validate_session(BOT_SESSION, LOGS),
|
| 85 |
udB=udB,
|
|
@@ -87,40 +106,49 @@ if run_as_module:
|
|
| 87 |
device_model="Ultroid",
|
| 88 |
)
|
| 89 |
else:
|
| 90 |
-
#
|
| 91 |
bot_token = udB.get_key("BOT_TOKEN")
|
| 92 |
if bot_token:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
asst = UltroidClient("sessions/asst", bot_token=bot_token, udB=udB)
|
| 122 |
else:
|
| 123 |
-
|
|
|
|
| 124 |
except Exception as e:
|
| 125 |
LOGS.warning(f"Failed to initialize assistant bot: {e}")
|
| 126 |
LOGS.info("Running without assistant bot...")
|
|
|
|
| 78 |
asst = ultroid_bot
|
| 79 |
else:
|
| 80 |
try:
|
| 81 |
+
# Check for bot.session file first (in root or sessions directory)
|
| 82 |
+
bot_session_file = None
|
| 83 |
+
if os.path.exists("bot.session"):
|
| 84 |
+
bot_session_file = "bot.session"
|
| 85 |
+
elif os.path.exists("sessions/bot.session"):
|
| 86 |
+
bot_session_file = "sessions/bot.session"
|
| 87 |
+
|
| 88 |
+
# Also check for session string from env/db
|
| 89 |
BOT_SESSION = Var.BOT_SESSION or udB.get_key("BOT_SESSION") or udB.get_key("bot.session")
|
| 90 |
+
|
| 91 |
+
if bot_session_file:
|
| 92 |
+
# Use the bot.session file directly
|
| 93 |
+
LOGS.info(f"Using bot session file: {bot_session_file}")
|
| 94 |
+
asst = UltroidClient(
|
| 95 |
+
bot_session_file,
|
| 96 |
+
udB=udB,
|
| 97 |
+
app_version=ultroid_version,
|
| 98 |
+
device_model="Ultroid",
|
| 99 |
+
)
|
| 100 |
+
elif BOT_SESSION:
|
| 101 |
+
# Use session string from env/db
|
| 102 |
asst = UltroidClient(
|
| 103 |
validate_session(BOT_SESSION, LOGS),
|
| 104 |
udB=udB,
|
|
|
|
| 106 |
device_model="Ultroid",
|
| 107 |
)
|
| 108 |
else:
|
| 109 |
+
# Use bot_token directly (recommended: create BOT_SESSION manually)
|
| 110 |
bot_token = udB.get_key("BOT_TOKEN")
|
| 111 |
if bot_token:
|
| 112 |
+
# Optionally auto-generate session if enabled and connection is available
|
| 113 |
+
# Note: Manual session generation is recommended for reliability
|
| 114 |
+
auto_gen_session = udB.get_key("AUTO_GEN_BOT_SESSION")
|
| 115 |
+
if auto_gen_session:
|
| 116 |
+
try:
|
| 117 |
+
# Create temporary client with bot_token to generate session
|
| 118 |
+
from telethon.sessions import StringSession
|
| 119 |
+
temp_session = StringSession()
|
| 120 |
+
temp_client = UltroidClient(
|
| 121 |
+
temp_session,
|
| 122 |
+
bot_token=bot_token,
|
| 123 |
+
udB=udB,
|
| 124 |
+
log_attempt=False,
|
| 125 |
+
exit_on_error=False,
|
| 126 |
+
)
|
| 127 |
+
# The client auto-starts in __init__, so session should be ready
|
| 128 |
+
# Generate session string from the bot client
|
| 129 |
+
bot_session_string = temp_client.session.save()
|
| 130 |
+
# Save the session to database
|
| 131 |
+
udB.set_key("BOT_SESSION", bot_session_string)
|
| 132 |
+
LOGS.info("Generated bot session from bot token and saved it.")
|
| 133 |
+
# Close temp client and create new one with session
|
| 134 |
+
temp_client.disconnect()
|
| 135 |
+
asst = UltroidClient(
|
| 136 |
+
validate_session(bot_session_string, LOGS),
|
| 137 |
+
udB=udB,
|
| 138 |
+
app_version=ultroid_version,
|
| 139 |
+
device_model="Ultroid",
|
| 140 |
+
)
|
| 141 |
+
except Exception as session_error:
|
| 142 |
+
LOGS.warning(f"Failed to create session from bot token: {session_error}")
|
| 143 |
+
LOGS.info("Falling back to bot_token method. Consider creating BOT_SESSION manually.")
|
| 144 |
+
# Fall back to bot_token method
|
| 145 |
+
asst = UltroidClient("sessions/asst", bot_token=bot_token, udB=udB)
|
| 146 |
+
else:
|
| 147 |
+
# Use bot_token directly (default behavior)
|
| 148 |
asst = UltroidClient("sessions/asst", bot_token=bot_token, udB=udB)
|
| 149 |
else:
|
| 150 |
+
LOGS.warning("No BOT_TOKEN found. Assistant bot will not be available.")
|
| 151 |
+
asst = ultroid_bot
|
| 152 |
except Exception as e:
|
| 153 |
LOGS.warning(f"Failed to initialize assistant bot: {e}")
|
| 154 |
LOGS.info("Running without assistant bot...")
|
pyUltroid/configs.py
CHANGED
|
@@ -38,7 +38,7 @@ class Var:
|
|
| 38 |
)
|
| 39 |
# extras
|
| 40 |
BOT_TOKEN = config("BOT_TOKEN", default=None)
|
| 41 |
-
BOT_SESSION = config("BOT_SESSION", default=None)
|
| 42 |
LOG_CHANNEL = config("LOG_CHANNEL", default=0, cast=int)
|
| 43 |
HEROKU_APP_NAME = config("HEROKU_APP_NAME", default=None)
|
| 44 |
HEROKU_API = config("HEROKU_API", default=None)
|
|
|
|
| 38 |
)
|
| 39 |
# extras
|
| 40 |
BOT_TOKEN = config("BOT_TOKEN", default=None)
|
| 41 |
+
BOT_SESSION = config("BOT_SESSION", default=None) or config("bot.session", default=None)
|
| 42 |
LOG_CHANNEL = config("LOG_CHANNEL", default=0, cast=int)
|
| 43 |
HEROKU_APP_NAME = config("HEROKU_APP_NAME", default=None)
|
| 44 |
HEROKU_API = config("HEROKU_API", default=None)
|
resources/session/bot_session_gen.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Generate Bot Session String for Ultroid
|
| 4 |
+
This script generates a session string for your bot account.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
# Add parent directory to path
|
| 11 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
from decouple import config
|
| 15 |
+
except ImportError:
|
| 16 |
+
print("Installing python-decouple...")
|
| 17 |
+
os.system("pip install python-decouple")
|
| 18 |
+
from decouple import config
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
from telethon.sessions import StringSession
|
| 22 |
+
from telethon import TelegramClient
|
| 23 |
+
except ImportError:
|
| 24 |
+
print("Installing Telethon...")
|
| 25 |
+
os.system("pip install telethon")
|
| 26 |
+
from telethon.sessions import StringSession
|
| 27 |
+
from telethon import TelegramClient
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def get_api_credentials():
|
| 31 |
+
"""Get API ID and API HASH from environment or config"""
|
| 32 |
+
# Try to get from config without casting first
|
| 33 |
+
api_id_str = config("API_ID", default=None)
|
| 34 |
+
API_HASH = config("API_HASH", default=None)
|
| 35 |
+
|
| 36 |
+
# Convert API_ID to int if it exists
|
| 37 |
+
API_ID = None
|
| 38 |
+
if api_id_str:
|
| 39 |
+
try:
|
| 40 |
+
API_ID = int(api_id_str)
|
| 41 |
+
except (ValueError, TypeError):
|
| 42 |
+
API_ID = None
|
| 43 |
+
|
| 44 |
+
# Prompt user if not found in config
|
| 45 |
+
if not API_ID or not API_HASH:
|
| 46 |
+
print("Please set API_ID and API_HASH in your .env file or environment variables.")
|
| 47 |
+
if not API_ID:
|
| 48 |
+
api_id_input = input("Enter API_ID: ").strip()
|
| 49 |
+
try:
|
| 50 |
+
API_ID = int(api_id_input)
|
| 51 |
+
except ValueError:
|
| 52 |
+
print("Error: API_ID must be a number!")
|
| 53 |
+
sys.exit(1)
|
| 54 |
+
if not API_HASH:
|
| 55 |
+
API_HASH = input("Enter API_HASH: ").strip()
|
| 56 |
+
if not API_HASH:
|
| 57 |
+
print("Error: API_HASH is required!")
|
| 58 |
+
sys.exit(1)
|
| 59 |
+
|
| 60 |
+
return API_ID, API_HASH
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def generate_bot_session():
|
| 64 |
+
"""Generate a session string for bot account"""
|
| 65 |
+
print("Bot Session Generator for Ultroid")
|
| 66 |
+
print("=" * 50)
|
| 67 |
+
|
| 68 |
+
API_ID, API_HASH = get_api_credentials()
|
| 69 |
+
|
| 70 |
+
bot_token = config("BOT_TOKEN", default=None)
|
| 71 |
+
if not bot_token:
|
| 72 |
+
bot_token = input("Enter your Bot Token (from @BotFather): ").strip()
|
| 73 |
+
|
| 74 |
+
if not bot_token:
|
| 75 |
+
print("Error: Bot token is required!")
|
| 76 |
+
return
|
| 77 |
+
|
| 78 |
+
print("\nGenerating bot session...")
|
| 79 |
+
print("This may take a few seconds...\n")
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
# Create a fresh in-memory session to avoid reusing existing sessions
|
| 83 |
+
import tempfile
|
| 84 |
+
import os
|
| 85 |
+
|
| 86 |
+
# Create a temporary session file that won't conflict
|
| 87 |
+
temp_session_file = os.path.join(tempfile.gettempdir(), f"bot_session_temp_{os.getpid()}.session")
|
| 88 |
+
|
| 89 |
+
# Remove if exists to ensure fresh session
|
| 90 |
+
if os.path.exists(temp_session_file):
|
| 91 |
+
os.remove(temp_session_file)
|
| 92 |
+
|
| 93 |
+
with TelegramClient(temp_session_file, API_ID, API_HASH) as client:
|
| 94 |
+
# Start with bot_token - this will authenticate as bot
|
| 95 |
+
client.start(bot_token=bot_token)
|
| 96 |
+
|
| 97 |
+
# Verify it's actually a bot account
|
| 98 |
+
if not client.is_bot():
|
| 99 |
+
print("\n⚠️ Warning: The session appears to be for a user account, not a bot account.")
|
| 100 |
+
print("Make sure your BOT_TOKEN is correct.\n")
|
| 101 |
+
|
| 102 |
+
# Get bot session string
|
| 103 |
+
session_string = client.session.save()
|
| 104 |
+
|
| 105 |
+
# Clean up temp session file
|
| 106 |
+
try:
|
| 107 |
+
if os.path.exists(temp_session_file):
|
| 108 |
+
os.remove(temp_session_file)
|
| 109 |
+
except Exception:
|
| 110 |
+
pass
|
| 111 |
+
|
| 112 |
+
print("\n" + "=" * 50)
|
| 113 |
+
print("✓ Bot session generated successfully!")
|
| 114 |
+
print("=" * 50)
|
| 115 |
+
print(f"\nYour BOT_SESSION string:\n\n{session_string}\n")
|
| 116 |
+
print("=" * 50)
|
| 117 |
+
print("\nIMPORTANT:")
|
| 118 |
+
print("1. Copy the session string above")
|
| 119 |
+
print("2. Add it to your .env file as:")
|
| 120 |
+
print(" BOT_SESSION=your_session_string")
|
| 121 |
+
print(" OR")
|
| 122 |
+
print(" bot.session=your_session_string")
|
| 123 |
+
print("3. Or set it in your database with key: BOT_SESSION or bot.session")
|
| 124 |
+
print("4. Do NOT share this session string with anyone!\n")
|
| 125 |
+
|
| 126 |
+
# Try to send to saved messages (async call)
|
| 127 |
+
try:
|
| 128 |
+
import asyncio
|
| 129 |
+
if client.is_connected():
|
| 130 |
+
asyncio.run(client.send_message("me", f"**ULTROID BOT SESSION**\n\n`{session_string}`\n\n**DO NOT SHARE THIS!**"))
|
| 131 |
+
print("✓ Session string also sent to your Telegram saved messages.\n")
|
| 132 |
+
except Exception as e:
|
| 133 |
+
# Ignore errors sending to saved messages - not critical
|
| 134 |
+
pass
|
| 135 |
+
|
| 136 |
+
except Exception as e:
|
| 137 |
+
print(f"\n✗ Error generating session: {e}")
|
| 138 |
+
print("\nPlease check:")
|
| 139 |
+
print("- Your API_ID and API_HASH are correct")
|
| 140 |
+
print("- Your BOT_TOKEN is valid")
|
| 141 |
+
print("- You have a stable internet connection")
|
| 142 |
+
sys.exit(1)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
if __name__ == "__main__":
|
| 146 |
+
generate_bot_session()
|
| 147 |
+
|