Command exited with code 0. Command output: + if self.bot_polling_task.done(): + log("[watchdog] Bot polling task was stopped! Restarting...") + self.bot_polling_task = asyncio.create_task(self._bot_http_polling_loop()) + + # 2. Check main client + if self.main_client_ready and self.main_client is not None: + if not getattr(self.main_client, "is_connected", False): + log("[watchdog] Main client is disconnected! Attempting reconnect...") + try: + await asyncio.wait_for(self.main_client.connect(), timeout=10.0) + log("[watchdog] Main client reconnected successfully!") + except Exception as e: + log(f"[watchdog] Main client reconnect failed: {e}") + except asyncio.CancelledError: + break + except Exception as exc: + log(f"[watchdog] Loop error: {exc}") async def send_menu(self, ws: Workspace, text: str = "👋 Menu:") -> None: await self.send(ws.user_id, text, keyboard=self._workspace_menu(ws)) @@ -692,20 +702,8 @@ self._migrate_fcm_urls() self._sync_old_fcm_creds() - self.bot.add_handler(MessageHandler(self._handle_bot_message)) - try: - await self.bot.start() - except FloodWait as exc: - log(f"[FLOOD WAIT] Telegram bot auth rate limited. Required wait: {exc.value}s (~{round(exc.value/60, 1)}m). Sleeping to avoid PM2 crash loop...") - await asyncio.sleep(exc.value + 5) - await self.bot.start() - if self.logs_group_id: - try: - target_gid = int(self.logs_group_id) if str(self.logs_group_id).lstrip("-").isdigit() else self.logs_group_id - await self.bot.get_chat(target_gid) - log(f"Successfully primed logs_group_id: {self.logs_group_id}") - except Exception as e: - log(f"Warning: Could not prime logs_group_id {self.logs_group_id}: {e}") + self.bot_polling_task = asyncio.create_task(self._bot_http_polling_loop()) + log("Bot HTTP polling loop started.") self.main_client = self._build_main_client() session_file = LEGACY_SESSION_BASE.with_suffix(".session") @@ -843,38 +841,12 @@ except Exception: pass - try: - await self.bot.stop() - except Exception: - pass + if hasattr(self, "bot_polling_task") and self.bot_polling_task: + self.bot_polling_task.cancel() async def _deactivate_duplicate_groups(self, ws: Workspace) -> None: - if not ws.active or not ws.group_id: - return - - target_group = self._canonical_chat_id(ws.group_id) - current_user = ws.user_id - - for other_uid, other_ws in list(self.workspaces.items()): - if other_uid == current_user: - continue - if not other_ws.active or not other_ws.group_id: - continue - - if self._canonical_chat_id(other_ws.group_id) == target_group: - other_ws.active = False - self.save_workspace(other_ws) - await self.stop_bridge_runtime(other_uid, keep_state=True) - - log(f"[session auto-pause] Deactivated duplicate session for user {other_uid} because user {current_user} activated group {target_group}") - - premium_emoji = "[✨](tg://emoji?id=5258500422393415126)" - body = ( - f"⚠️ **Session Paused Automatically**\n\n" - f"Your active session has been paused because another user is now actively monitoring the group `{target_group}`.\n\n" - f"Only one user can actively monitor a group at a time." - ) - await self.send(other_uid, fmt_msg("Session Paused", body, error=True)) + # Multiple users can actively monitor the same group without deactivating each other. + return async def _delayed_auto_pin(self, ws: Workspace, delay: float = 0.0) -> None: if delay > 0: @@ -930,20 +902,11 @@ ws = self.load_workspace(user_id) if ws and ws.pinned_status_msg_id: pinned_id = ws.pinned_status_msg_id - unpinned = False - if self.bot and getattr(self.bot, "is_connected", False): - try: - await asyncio.wait_for(self.bot.unpin_chat_message(int(user_id), pinned_id), timeout=3.0) - log(f"[stop] unpinned msg {pinned_id} for {user_id}") - unpinned = True - except Exception as e: - log(f"[stop] unpin by id failed: {e}") - if not unpinned: - try: - await self._http_unpin_message(int(user_id), pinned_id) - log(f"[stop] unpinned msg {pinned_id} for {user_id} via HTTP API") - except Exception as e2: - log(f"[stop] HTTP unpin failed: {e2}") + try: + await self._http_unpin_message(int(user_id), pinned_id) + log(f"[stop] unpinned msg {pinned_id} for {user_id}") + except Exception as e: + log(f"[stop] unpin failed: {e}") ws.pinned_status_msg_id = 0 self.save_workspace(ws) @@ -970,35 +933,19 @@ msg_id = getattr(msg, "id", None) if ws.pinned_status_msg_id: old_id = ws.pinned_status_msg_id - unpinned = False - if self.bot and getattr(self.bot, "is_connected", False): - try: - await asyncio.wait_for(self.bot.unpin_chat_message(int(ws.user_id), old_id), timeout=3.0)