Use bot.session files instead of bot tokens
Browse files- config.py +13 -8
- sample.env +8 -2
- utils/clients.py +46 -19
config.py
CHANGED
|
@@ -8,16 +8,21 @@ load_dotenv()
|
|
| 8 |
API_ID = int(os.getenv("API_ID")) # Your Telegram API ID
|
| 9 |
API_HASH = os.getenv("API_HASH") # Your Telegram API Hash
|
| 10 |
|
| 11 |
-
# List of Telegram bot
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
# List of
|
| 16 |
STRING_SESSIONS = os.getenv("STRING_SESSIONS", "").strip(", ").split(",")
|
| 17 |
STRING_SESSIONS = [
|
| 18 |
session.strip() for session in STRING_SESSIONS if session.strip() != ""
|
| 19 |
]
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Chat ID of the Telegram storage channel where files will be stored
|
| 22 |
STORAGE_CHANNEL = int(os.getenv("STORAGE_CHANNEL")) # Your storage channel's chat ID
|
| 23 |
|
|
@@ -30,11 +35,11 @@ DATABASE_BACKUP_MSG_ID = int(
|
|
| 30 |
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "admin") # Default to "admin" if not set
|
| 31 |
|
| 32 |
# Determine the maximum file size (in bytes) allowed for uploading to Telegram
|
| 33 |
-
#
|
| 34 |
-
if len(STRING_SESSIONS)
|
| 35 |
-
MAX_FILE_SIZE =
|
| 36 |
else:
|
| 37 |
-
MAX_FILE_SIZE =
|
| 38 |
|
| 39 |
# Database backup interval in seconds. Backups will be sent to the storage channel at this interval
|
| 40 |
DATABASE_BACKUP_TIME = int(
|
|
|
|
| 8 |
API_ID = int(os.getenv("API_ID")) # Your Telegram API ID
|
| 9 |
API_HASH = os.getenv("API_HASH") # Your Telegram API Hash
|
| 10 |
|
| 11 |
+
# List of Telegram bot session files (e.g., "bot1.session,bot2.session") used for file upload/download operations
|
| 12 |
+
# Session files should be placed in the cache directory
|
| 13 |
+
BOT_SESSIONS = os.getenv("BOT_SESSIONS", "").strip(", ").split(",")
|
| 14 |
+
BOT_SESSIONS = [session.strip() for session in BOT_SESSIONS if session.strip() != ""]
|
| 15 |
|
| 16 |
+
# List of Telegram Account Pyrogram String Sessions used for file upload/download operations (optional)
|
| 17 |
STRING_SESSIONS = os.getenv("STRING_SESSIONS", "").strip(", ").split(",")
|
| 18 |
STRING_SESSIONS = [
|
| 19 |
session.strip() for session in STRING_SESSIONS if session.strip() != ""
|
| 20 |
]
|
| 21 |
|
| 22 |
+
# List of Telegram bot tokens (deprecated - use BOT_SESSIONS instead)
|
| 23 |
+
BOT_TOKENS = os.getenv("BOT_TOKENS", "").strip(", ").split(",")
|
| 24 |
+
BOT_TOKENS = [token.strip() for token in BOT_TOKENS if token.strip() != ""]
|
| 25 |
+
|
| 26 |
# Chat ID of the Telegram storage channel where files will be stored
|
| 27 |
STORAGE_CHANNEL = int(os.getenv("STORAGE_CHANNEL")) # Your storage channel's chat ID
|
| 28 |
|
|
|
|
| 35 |
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "admin") # Default to "admin" if not set
|
| 36 |
|
| 37 |
# Determine the maximum file size (in bytes) allowed for uploading to Telegram
|
| 38 |
+
# String sessions support up to 4GB (if Premium), bot sessions/tokens limited to 2GB
|
| 39 |
+
if len(STRING_SESSIONS) > 0:
|
| 40 |
+
MAX_FILE_SIZE = 3.98 * 1024 * 1024 * 1024 # 4 GB in bytes (with Premium)
|
| 41 |
else:
|
| 42 |
+
MAX_FILE_SIZE = 1.98 * 1024 * 1024 * 1024 # 2 GB in bytes (bot sessions/tokens)
|
| 43 |
|
| 44 |
# Database backup interval in seconds. Backups will be sent to the storage channel at this interval
|
| 45 |
DATABASE_BACKUP_TIME = int(
|
sample.env
CHANGED
|
@@ -2,6 +2,12 @@
|
|
| 2 |
|
| 3 |
API_ID=123456
|
| 4 |
API_HASH=dagsjdhgjfsahgjfh
|
| 5 |
-
|
| 6 |
STORAGE_CHANNEL=-100123456789
|
| 7 |
-
DATABASE_BACKUP_MSG_ID=123
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
API_ID=123456
|
| 4 |
API_HASH=dagsjdhgjfsahgjfh
|
| 5 |
+
BOT_SESSIONS=bot1.session,bot2.session
|
| 6 |
STORAGE_CHANNEL=-100123456789
|
| 7 |
+
DATABASE_BACKUP_MSG_ID=123
|
| 8 |
+
|
| 9 |
+
# Optional: String sessions for Premium accounts (files > 2GB)
|
| 10 |
+
# STRING_SESSIONS=1BVtsOMwBu5...your_session_string_here...
|
| 11 |
+
|
| 12 |
+
# Optional: Bot tokens (fallback if BOT_SESSIONS not available)
|
| 13 |
+
# BOT_TOKENS=21413535:gkdshajfhjfakhjf
|
utils/clients.py
CHANGED
|
@@ -21,47 +21,71 @@ async def initialize_clients():
|
|
| 21 |
session_cache_path = Path(f"./cache")
|
| 22 |
session_cache_path.parent.mkdir(parents=True, exist_ok=True)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
-
async def start_client(client_id,
|
| 30 |
try:
|
| 31 |
logger.info(f"Starting - {type.title()} Client {client_id}")
|
| 32 |
|
| 33 |
-
if type == "
|
|
|
|
|
|
|
| 34 |
client = Client(
|
| 35 |
-
name=
|
| 36 |
api_id=config.API_ID,
|
| 37 |
api_hash=config.API_HASH,
|
| 38 |
-
bot_token=token,
|
| 39 |
workdir=session_cache_path,
|
| 40 |
)
|
| 41 |
client.loop = asyncio.get_running_loop()
|
| 42 |
await client.start()
|
| 43 |
await client.send_message(
|
| 44 |
config.STORAGE_CHANNEL,
|
| 45 |
-
f"Started -
|
| 46 |
)
|
| 47 |
multi_clients[client_id] = client
|
| 48 |
work_loads[client_id] = 0
|
| 49 |
-
elif type == "
|
|
|
|
| 50 |
client = await Client(
|
| 51 |
name=str(client_id),
|
| 52 |
api_id=config.API_ID,
|
| 53 |
api_hash=config.API_HASH,
|
| 54 |
-
session_string=
|
| 55 |
sleep_threshold=config.SLEEP_THRESHOLD,
|
| 56 |
workdir=session_cache_path,
|
| 57 |
no_updates=True,
|
| 58 |
).start()
|
| 59 |
await client.send_message(
|
| 60 |
config.STORAGE_CHANNEL,
|
| 61 |
-
f"Started -
|
| 62 |
)
|
| 63 |
premium_clients[client_id] = client
|
| 64 |
premium_work_loads[client_id] = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
logger.info(f"Started - {type.title()} Client {client_id}")
|
| 67 |
except Exception as e:
|
|
@@ -72,15 +96,20 @@ async def initialize_clients():
|
|
| 72 |
)
|
| 73 |
logger.error(f"Traceback: {error_trace}")
|
| 74 |
|
|
|
|
| 75 |
await asyncio.gather(
|
| 76 |
*(
|
| 77 |
[
|
| 78 |
-
start_client(client_id,
|
| 79 |
-
for client_id,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
]
|
| 81 |
+ [
|
| 82 |
-
start_client(client_id,
|
| 83 |
-
for client_id,
|
| 84 |
]
|
| 85 |
)
|
| 86 |
)
|
|
@@ -89,11 +118,9 @@ async def initialize_clients():
|
|
| 89 |
|
| 90 |
if not clients_initialized:
|
| 91 |
logger.warning("No Clients Were Initialized - Website will run in offline mode")
|
|
|
|
| 92 |
return False
|
| 93 |
|
| 94 |
-
if len(premium_clients) == 0:
|
| 95 |
-
logger.info("No Premium Clients Were Initialized")
|
| 96 |
-
|
| 97 |
logger.info("Clients Initialized")
|
| 98 |
|
| 99 |
# Load the drive data
|
|
@@ -122,7 +149,7 @@ def get_client(premium_required=False) -> Client:
|
|
| 122 |
return premium_clients[index]
|
| 123 |
|
| 124 |
if not work_loads:
|
| 125 |
-
raise RuntimeError("No Clients Available - Check
|
| 126 |
index = min(work_loads, key=work_loads.get)
|
| 127 |
work_loads[index] += 1
|
| 128 |
return multi_clients[index]
|
|
|
|
| 21 |
session_cache_path = Path(f"./cache")
|
| 22 |
session_cache_path.parent.mkdir(parents=True, exist_ok=True)
|
| 23 |
|
| 24 |
+
# Use BOT_SESSIONS (session files) as primary method
|
| 25 |
+
all_bot_sessions = dict((i, s) for i, s in enumerate(config.BOT_SESSIONS, start=1))
|
| 26 |
+
all_string_sessions = dict(
|
| 27 |
+
(i, s) for i, s in enumerate(config.STRING_SESSIONS, start=len(all_bot_sessions) + 1)
|
| 28 |
+
)
|
| 29 |
+
# Fallback to bot tokens if session files not available
|
| 30 |
+
all_tokens = dict(
|
| 31 |
+
(i, t) for i, t in enumerate(config.BOT_TOKENS, start=len(all_bot_sessions) + len(all_string_sessions) + 1)
|
| 32 |
)
|
| 33 |
|
| 34 |
+
async def start_client(client_id, session_name_or_token, type):
|
| 35 |
try:
|
| 36 |
logger.info(f"Starting - {type.title()} Client {client_id}")
|
| 37 |
|
| 38 |
+
if type == "bot_session":
|
| 39 |
+
# Use bot session file (e.g., "bot1.session")
|
| 40 |
+
# Session file should be in cache directory
|
| 41 |
client = Client(
|
| 42 |
+
name=session_name_or_token.replace(".session", ""), # Remove .session extension
|
| 43 |
api_id=config.API_ID,
|
| 44 |
api_hash=config.API_HASH,
|
|
|
|
| 45 |
workdir=session_cache_path,
|
| 46 |
)
|
| 47 |
client.loop = asyncio.get_running_loop()
|
| 48 |
await client.start()
|
| 49 |
await client.send_message(
|
| 50 |
config.STORAGE_CHANNEL,
|
| 51 |
+
f"Started - Bot Session Client {client_id}",
|
| 52 |
)
|
| 53 |
multi_clients[client_id] = client
|
| 54 |
work_loads[client_id] = 0
|
| 55 |
+
elif type == "string_session":
|
| 56 |
+
# Use string session (user account)
|
| 57 |
client = await Client(
|
| 58 |
name=str(client_id),
|
| 59 |
api_id=config.API_ID,
|
| 60 |
api_hash=config.API_HASH,
|
| 61 |
+
session_string=session_name_or_token,
|
| 62 |
sleep_threshold=config.SLEEP_THRESHOLD,
|
| 63 |
workdir=session_cache_path,
|
| 64 |
no_updates=True,
|
| 65 |
).start()
|
| 66 |
await client.send_message(
|
| 67 |
config.STORAGE_CHANNEL,
|
| 68 |
+
f"Started - String Session Client {client_id}",
|
| 69 |
)
|
| 70 |
premium_clients[client_id] = client
|
| 71 |
premium_work_loads[client_id] = 0
|
| 72 |
+
elif type == "bot_token":
|
| 73 |
+
# Fallback: Use bot token if session files not available
|
| 74 |
+
client = Client(
|
| 75 |
+
name=str(client_id),
|
| 76 |
+
api_id=config.API_ID,
|
| 77 |
+
api_hash=config.API_HASH,
|
| 78 |
+
bot_token=session_name_or_token,
|
| 79 |
+
workdir=session_cache_path,
|
| 80 |
+
)
|
| 81 |
+
client.loop = asyncio.get_running_loop()
|
| 82 |
+
await client.start()
|
| 83 |
+
await client.send_message(
|
| 84 |
+
config.STORAGE_CHANNEL,
|
| 85 |
+
f"Started - Bot Token Client {client_id}",
|
| 86 |
+
)
|
| 87 |
+
multi_clients[client_id] = client
|
| 88 |
+
work_loads[client_id] = 0
|
| 89 |
|
| 90 |
logger.info(f"Started - {type.title()} Client {client_id}")
|
| 91 |
except Exception as e:
|
|
|
|
| 96 |
)
|
| 97 |
logger.error(f"Traceback: {error_trace}")
|
| 98 |
|
| 99 |
+
# Prioritize bot session files
|
| 100 |
await asyncio.gather(
|
| 101 |
*(
|
| 102 |
[
|
| 103 |
+
start_client(client_id, session_name, "bot_session")
|
| 104 |
+
for client_id, session_name in all_bot_sessions.items()
|
| 105 |
+
]
|
| 106 |
+
+ [
|
| 107 |
+
start_client(client_id, session, "string_session")
|
| 108 |
+
for client_id, session in all_string_sessions.items()
|
| 109 |
]
|
| 110 |
+ [
|
| 111 |
+
start_client(client_id, token, "bot_token")
|
| 112 |
+
for client_id, token in all_tokens.items()
|
| 113 |
]
|
| 114 |
)
|
| 115 |
)
|
|
|
|
| 118 |
|
| 119 |
if not clients_initialized:
|
| 120 |
logger.warning("No Clients Were Initialized - Website will run in offline mode")
|
| 121 |
+
logger.warning("Please configure BOT_SESSIONS in your environment variables")
|
| 122 |
return False
|
| 123 |
|
|
|
|
|
|
|
|
|
|
| 124 |
logger.info("Clients Initialized")
|
| 125 |
|
| 126 |
# Load the drive data
|
|
|
|
| 149 |
return premium_clients[index]
|
| 150 |
|
| 151 |
if not work_loads:
|
| 152 |
+
raise RuntimeError("No Clients Available - Check BOT_SESSIONS configuration")
|
| 153 |
index = min(work_loads, key=work_loads.get)
|
| 154 |
work_loads[index] += 1
|
| 155 |
return multi_clients[index]
|