Command exited with code 0. Command output: 324- self.http: Optional[aiohttp.ClientSession] = None 325- self.workspaces: dict[str, Workspace] = {} 326- self.auth_clients: dict[str, Client] = {} 327- self.bridge_tasks: dict[str, asyncio.Task] = {} 328- self.workspace_locks: dict[str, asyncio.Lock] = {} 329: self.main_client: Optional[Client] = None 330: self.main_client_ready = False 331- self._bot_restarting = False 332- self.watchdog_task: Optional[asyncio.Task] = None 333- 334- def is_admin(self, user_id: str | int) -> bool: 335- uid = str(user_id).strip() -- 519- if not self.bot.is_connected and not self._bot_restarting: 520- log("[watchdog] Bot client is disconnected! Triggering recovery...") 521- asyncio.create_task(self._restart_bot_client()) 522- 523- # 2. Check main client 524: if self.main_client_ready and self.main_client is not None: 525: if not self.main_client.is_connected: 526- log("[watchdog] Main client is disconnected! Attempting reconnect...") 527- try: 528: await self.main_client.connect() 529- log("[watchdog] Main client reconnected successfully!") 530- except Exception as e: 531- log(f"[watchdog] Main client reconnect failed: {e}") 532- except asyncio.CancelledError: 533- break -- 586- try: 587- await client.disconnect() 588- except Exception: 589- pass 590- 591: def _build_main_client(self) -> Client: 592- return Client( 593- str(LEGACY_SESSION_BASE), 594- api_id=self.api_id, 595- api_hash=self.api_hash, 596- phone_number=self.owner_phone or None, -- 644- await self.bot.get_chat(target_gid) 645- log(f"Successfully primed logs_group_id: {self.logs_group_id}") 646- except Exception as e: 647- log(f"Warning: Could not prime logs_group_id {self.logs_group_id}: {e}") 648- 649: self.main_client = self._build_main_client() 650- session_file = LEGACY_SESSION_BASE.with_suffix(".session") 651- 652- if session_file.exists(): 653- try: 654: await self.main_client.connect() 655: auth_status = await self.main_client.get_me() 656- if auth_status: 657: await self.main_client.disconnect() 658: await self.main_client.start() 659: self.main_client_ready = True 660: self.main_client.add_handler(MessageHandler(self._on_main_message)) 661: async for _ in self.main_client.get_dialogs(): 662- pass 663- else: 664: await self.main_client.disconnect() 665- log("Main session is not authorized. Please login via bot.") 666: self.main_client_ready = False 667- except AuthKeyDuplicated as exc: 668- log(f"Main session duplicated: {exc}. Retrying with a fresh session file.") 669- self._quarantine_main_session_files() 670: self.main_client_ready = False 671- except Exception as exc: 672- log(f"Main Telegram session could not start: {exc}. Please login via bot.") 673: self.main_client_ready = False 674- else: 675- log("Main session not found. Please login via the Telegram bot menu (Login Main Account).") 676: self.main_client_ready = False 677- 678- log("Bot service started.") 679- self.watchdog_task = asyncio.create_task(self._watchdog_loop()) 680- 681- for path in USERS_DIR.glob("*.json"): -- 774- self.bridge_tasks.clear() 775- 776- if self.http: 777- await self.http.close() 778- 779: if self.main_client_ready and self.main_client: 780- try: 781: await self.main_client.stop() 782- except Exception: 783- pass 784- 785- try: 786- await self.bot.stop() -- 828- return 829- 830- await self._deactivate_duplicate_groups(ws) 831- 832- user_id = ws.user_id 833: if not self.main_client_ready: 834- await self.send(ws.user_id, fmt_msg("Session Not Ready", "Main Telegram session is not ready yet. Please wait.", error=True), keyboard=self._workspace_menu(ws)) 835- return 836- 837- task: Optional[asyncio.Task] = None 838- -- 897- else: 898- info.append(f"📡 **Group:** `{ws.group_id}`") 899- if ws.forward_to: 900- info.append(f"📞 **Forward:** `{ws.forward_to}`") 901- info.append(f"📊 **Sent:** {ws.sent}") 902: info.append(f"🤖 **TG:** {'✅ Ready' if self.main_client_ready else '❌ Not ready'}") 903- msg = await self.send(ws.user_id, "\n".join(info), keyboard=self._workspace_menu(ws)) 904- if msg: 905- if ws.pinned_status_msg_id: 906- try: 907- await self.bot.unpin_chat_message(int(ws.user_id), ws.pinned_status_msg_id) -- 1605- except Exception as e: 1606- log(f"Could not rename auth session: {e}") 1607- 1608- # If this is the main client, restart it dynamically 1609- if str(ws.session_path) == str(LEGACY_SESSION_BASE): 1610: if self.main_client_ready and self.main_client: 1611- try: 1612: await self.main_client.stop() 1613- except Exception: 1614- pass 1615: self.main_client = self._build_main_client() 1616- try: 1617: await self.main_client.start() 1618: self.main_client_ready = True 1619: self.main_client.add_handler(MessageHandler(self._on_main_message)) 1620: async for _ in self.main_client.get_dialogs(): 1621- pass 1622- log("Main client dynamically started after login via bot.") 1623- except Exception as e: 1624- log(f"Failed to start main client after login: {e}") 1625: self.main_client_ready = False 1626- 1627- async def _handle_menu_action(self, ws: Workspace, text: str) -> None: 1628- user_id = ws.user_id 1629- 1630- if text in ("🔐 Login Main Account", "🔐 Connect Telegram"): -- 1861- info.append(f"🛰 **Group ID:** `{ws.group_id}`") 1862- if ws.forward_to: 1863- info.append(f"📞 **Forward:** `{ws.forward_to}`") 1864- info.append(f"📊 **Total Sent:** `{ws.sent}`") 1865- info.append(f"{UI_SEP}") 1866: info.append(f"🤖 **Bot Engine:** `{'✅ READY' if self.main_client_ready else '❌ OFFLINE'}`") 1867- 1868- await self.send(user_id, "\n".join(info) + f"\n{UI_FOOTER}", keyboard=self._workspace_menu(ws)) 1869- return 1870- 1871- if "FB Backup" in text: -- 1996- user_id = ws.user_id 1997- max_attempts = 18 # 18 * 10s = 3 minutes 1998- for attempt in range(max_attempts): 1999- await asyncio.sleep(10) 2000- try: 2001: invite = await self.main_client.invoke(CheckChatInvite(hash=invite_hash)) 2002- if isinstance(invite, ChatInviteAlready): 2003- chat = invite.chat 2004- chat_id = self._canonical_chat_id(chat.id, getattr(chat, "type", None)) 2005- group_name = self._group_key_from_chat(chat) 2006- -- 2037- 2038- if not ref: 2039- await self.send(user_id, fmt_msg("Invalid Input", "Send an invite link, @username, or group/channel ID.", error=True), keyboard=reply_kb(BACK_KB)) 2040- return False 2041- 2042: if not self.main_client_ready: 2043- await self.send(user_id, fmt_msg("Session Not Ready", "Main Telegram session is not ready yet. Please wait.", error=True), keyboard=reply_kb(BACK_KB)) 2044- return False 2045- 2046- try: 2047- if re.fullmatch(r"-?\d+", ref): 2048: chat = await self.main_client.get_chat(int(ref)) 2049- elif self._is_invite_like(ref): 2050- try: 2051: chat = await self.main_client.join_chat(ref) 2052- except Exception as join_exc: 2053- invite_hash = self._extract_invite_hash(ref) 2054- if invite_hash and "user_already_participant" in str(join_exc).lower(): 2055: invite = await self.main_client.invoke(CheckChatInvite(hash=invite_hash)) 2056- if isinstance(invite, ChatInviteAlready): 2057- chat = invite.chat 2058- else: 2059- raise join_exc 2060- elif "invite_request_sent" in str(join_exc).lower(): -- 2074- else: 2075- raise join_exc 2076- else: 2077- target = self._normalize_username_ref(ref) 2078- try: 2079: chat = await self.main_client.join_chat(target) 2080- except Exception: 2081: chat = await self.main_client.get_chat(target) 2082- 2083- if not chat or not getattr(chat, "id", None): 2084- raise RuntimeError("Could not resolve chat") 2085- 2086- chat_id = self._canonical_chat_id(chat.id, getattr(chat, "type", None))