File size: 2,866 Bytes
36dde81 1404692 36dde81 1404692 36dde81 1404692 36dde81 1404692 c12bb0b 36dde81 c12bb0b 36dde81 c12bb0b 36dde81 c12bb0b 36dde81 c12bb0b 36dde81 c12bb0b 3e18b74 36dde81 3e18b74 36dde81 c12bb0b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | import asyncio
from pyrogram import Client
from pyrogram.session import Session
from Backend.config import Telegram
from Backend.logger import LOGGER
async def _restart(self) -> None:
lock = self.__dict__.setdefault("_restart_lock", asyncio.Lock())
if lock.locked():
return
async with lock:
delay = 5
while True:
try:
await self.stop()
except Exception:
pass
try:
await self.start()
return
except Exception as e:
LOGGER.warning(f"Telegram session restart failed, retrying in {delay}s: {e}")
await asyncio.sleep(delay)
delay = min(delay * 2, 60)
Session.restart = _restart
#----- Primary bot client (loads plugins)
StreamBot = Client(
name='bot',
api_id=Telegram.API_ID,
api_hash=Telegram.API_HASH,
bot_token=Telegram.BOT_TOKEN,
plugins={"root": "Backend/pyrofork/plugins"},
sleep_threshold=20,
workers=6,
max_concurrent_transmissions=10
)
USERBOT_CLIENT_INDEX = -1
#----- Userbot client — built at runtime via build_userbot() from the stored session
Userbot = None
#----- Shared multi-client registries
multi_clients = {}
work_loads = {}
client_dc_map = {}
client_failures = {}
client_avg_mbps = {}
#----- Build (and register) the Userbot client from a session string at runtime
def build_userbot(session_string: str):
global Userbot
Userbot = Client(
name='userbot',
api_id=Telegram.API_ID,
api_hash=Telegram.API_HASH,
session_string=session_string,
device_model='Telegram Stremio',
sleep_threshold=20,
workers=6,
max_concurrent_transmissions=10,
no_updates=True,
in_memory=True,
)
work_loads[USERBOT_CLIENT_INDEX] = 0
client_failures[USERBOT_CLIENT_INDEX] = 0
client_avg_mbps[USERBOT_CLIENT_INDEX] = 0.0
return Userbot
#----- Resolve the bot's public t.me URL from cached username/me
def get_streambot_url() -> str:
try:
username = getattr(StreamBot, "username", None)
if not username:
me = getattr(StreamBot, "me", None)
username = getattr(me, "username", None) if me else None
if username:
return f"https://t.me/{username}"
except Exception:
pass
return "https://t.me/"
#----- Per-client workload map ({botN: load}), busiest first; {} when idle
def work_loads_summary() -> dict:
if not work_loads:
return {}
return {
f"bot{c + 1}": load
for c, (_, load) in enumerate(sorted(work_loads.items(), key=lambda x: x[1], reverse=True))
}
if Userbot is not None:
work_loads[USERBOT_CLIENT_INDEX] = 0
client_failures[USERBOT_CLIENT_INDEX] = 0
client_avg_mbps[USERBOT_CLIENT_INDEX] = 0.0
|