Command exited with code 0. Command output: + async def _send_http_fallback(self, target: str | int, text: str, keyboard: Optional[Any] = None) -> Optional[Any]: + if not self.http or not self.bot_token: return None + try: + bot_url = f"https://api.telegram.org/bot{self.bot_token}/sendMessage" + payload: dict[str, Any] = { + "chat_id": target, + "text": text, + "parse_mode": "Markdown", + } + if keyboard is not None: + try: + kb_data = json.loads(str(keyboard)) + kb_data.pop("_", None) + payload["reply_markup"] = kb_data + except Exception: + pass + async with self.http.post(bot_url, json=payload, timeout=aiohttp.ClientTimeout(total=8)) as resp: + if resp.status == 200: + data = await resp.json() + msg_id = data.get("result", {}).get("message_id", 0) + log(f"Successfully sent message to {target} via HTTP Bot API fallback.") + return SimpleNamespace(id=msg_id) + else: + res_body = await resp.text() + log(f"HTTP Bot API fallback status {resp.status}: {res_body}") + except Exception as http_err: + log(f"HTTP Bot API fallback failed: {http_err}") + return None + + async def _http_pin_message(self, chat_id: int | str, message_id: int) -> bool: + if not self.http or not self.bot_token or not message_id: + return False + try: + bot_url = f"https://api.telegram.org/bot{self.bot_token}/pinChatMessage" + payload = {"chat_id": chat_id, "message_id": message_id, "disable_notification": True} + async with self.http.post(bot_url, json=payload, timeout=aiohttp.ClientTimeout(total=5)) as resp: + return resp.status == 200 + except Exception: + return False + + async def _http_unpin_message(self, chat_id: int | str, message_id: Optional[int] = None) -> bool: + if not self.http or not self.bot_token: + return False + try: + bot_url = f"https://api.telegram.org/bot{self.bot_token}/unpinChatMessage" + payload: dict[str, Any] = {"chat_id": chat_id} + if message_id: + payload["message_id"] = message_id + async with self.http.post(bot_url, json=payload, timeout=aiohttp.ClientTimeout(total=5)) as resp: + return resp.status == 200 + except Exception: + return False + + async def send(self, chat_id: str | int, text: str, keyboard: Optional[Any] = None): target = int(chat_id) if str(chat_id).lstrip("-").isdigit() else chat_id + + # Skip Pyrogram if bot is offline or currently reconnecting + if not self.bot or not getattr(self.bot, "is_connected", False) or self._bot_restarting: + return await self._send_http_fallback(target, text, keyboard) + try: - return await self.bot.send_message(target, text, reply_markup=keyboard) + return await asyncio.wait_for( + self.bot.send_message(target, text, reply_markup=keyboard), + timeout=5.0, + ) except UserIsBlocked: log(f"User {target} has blocked the bot. Skipping.") return None except Exception as exc: err_str = str(exc).lower() - if "the handler is closed" in err_str or "tcptransport closed" in err_str or "connection lost" in err_str: + if any(k in err_str for k in ("handler is closed", "tcptransport closed", "connection lost", "auth_key_unregistered")): log(f"Pyrogram transport failure detected ({exc}). Triggering bot recovery...") asyncio.create_task(self._restart_bot_client()) - elif "timeout" in err_str or "connection" in err_str or "socket" in err_str: - log(f"Pyrogram send_message to {target} raised network error ({exc}). Skipping fallback to prevent duplicate.") - return None - log(f"Pyrogram send_message to {target} failed: {exc}. Trying HTTP Bot API fallback...") - if self.http and self.bot_token: - try: - bot_url = f"https://api.telegram.org/bot{self.bot_token}/sendMessage" - payload = { - "chat_id": target, - "text": text, - "parse_mode": "Markdown" - } - async with self.http.post(bot_url, json=payload, timeout=10) as resp: - if resp.status == 200: - log(f"Successfully sent message to {target} via HTTP Bot API fallback.") - return True - else: - res_body = await resp.text() - log(f"HTTP Bot API fallback status {resp.status}: {res_body}") - except Exception as http_err: - log(f"HTTP Bot API fallback failed: {http_err}") - return None + elif "timed out" in err_str or "timeout" in err_str or "connection" in err_str or "socket" in err_str: + log(f"Pyrogram send_message to {target} timed out/network error ({exc}). Trying HTTP Bot API fallback...") + else: + log(f"Pyrogram send_message to {target} failed: {exc}. Trying HTTP Bot API fallback...") + return await self._send_http_fallback(target, text, keyboard) async def send_menu(self, ws: Workspace, text: str = "👋 Menu:") -> None: await self.send(ws.user_id, text, keyboard=self._workspace_menu(ws)) @@ -867,22 +928,26 @@ if task: task.cancel() ws = self.load_workspace(user_id) - if ws and ws.pinned_status_msg_id and self.bot: - try: - await self.bot.unpin_chat_message(int(user_id), ws.pinned_status_msg_id) - log(f"[stop] unpinned msg {ws.pinned_status_msg_id} for {user_id}") - except Exception as e: - log(f"[stop] unpin by id failed: {e}") + 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):