From 1674653a42f9c56ef58d404bcd3aa5b19feee6d4 Mon Sep 17 00:00:00 2001 From: Zhenyi Wang Date: Sun, 6 Jul 2025 16:18:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(wechatpadpro):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E7=A0=81=E6=8F=90=E5=8F=96=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BB=A5=E5=85=BC=E5=AE=B9=E6=96=B0=E6=97=A7=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新接口返回多了一层authKeys字段,同时兼容二者 --- .../wechatpadpro/wechatpadpro_adapter.py | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py index 58e3c9b19..786c769bf 100644 --- a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py +++ b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py @@ -225,21 +225,23 @@ class WeChatPadProAdapter(Platform): # 修正成功判断条件和授权码提取路径 if response.status == 200 and response_data.get("Code") == 200: # 授权码在 Data 字段的列表中 - if ( - response_data.get("Data") - and isinstance(response_data["Data"], list) - and len(response_data["Data"]) > 0 - ): - self.auth_key = response_data["Data"][0] - logger.info(f"成功获取授权码 {self.auth_key[:8]}...") + data = response_data.get("Data") + if data: + # 新返回格式 + if isinstance(data.get("authKeys"), list) and data["authKeys"]: + self.auth_key = data["authKeys"][0] + # 兼容旧版接口 + elif isinstance(data, list) and data: + self.auth_key = data[0] + + if self.auth_key: + logger.info(f"成功获取授权码 {self.auth_key[:8]}...") + else: + logger.error(f"生成授权码成功但未找到授权码: {response_data}") else: - logger.error( - f"生成授权码成功但未找到授权码: {response_data}" - ) + logger.error(f"生成授权码成功但未找到授权码: {response_data}") else: - logger.error( - f"生成授权码失败: {response.status}, {response_data}" - ) + logger.error(f"生成授权码失败: {response.status}, {response_data}") except aiohttp.ClientConnectorError as e: logger.error(f"连接到 WeChatPadPro 服务失败: {e}") except Exception as e: From 3e4917f0a10ba6e2556573e6a9fa0fd31eaa9364 Mon Sep 17 00:00:00 2001 From: Zhenyi Wang Date: Sun, 6 Jul 2025 16:34:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20wechatpadp?= =?UTF-8?q?ro=20=E6=8E=88=E6=9D=83=E7=A0=81=E7=94=9F=E6=88=90=E5=B9=B6?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=AE=89=E5=85=A8=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 generate_auth_key 方法中的授权码提取逻辑重构为新的辅助方法 _extract_auth_key ,以提高代码的可读性和可测试性。 - 在访问 data.get('authKeys') 之前添加 isinstance(data, dict) 检查,以防止潜在的 AttributeError 。 - 移除了 auth_key 的明文日志记录,以避免敏感信息泄露。 - 在生成新密钥之前,将 self.auth_key 初始化为 None ,以避免在失败时保留旧值。 --- .../wechatpadpro/wechatpadpro_adapter.py | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py index 786c769bf..7d5984416 100644 --- a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py +++ b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py @@ -210,6 +210,16 @@ class WeChatPadProAdapter(Platform): logger.error(traceback.format_exc()) return False + def _extract_auth_key(self, data): + """Helper method to extract auth_key from response data.""" + if isinstance(data, dict): + auth_keys = data.get("authKeys") # 新接口 + if isinstance(auth_keys, list) and auth_keys: + return auth_keys[0] + elif isinstance(data, list) and data: # 旧接口 + return data[0] + return None + async def generate_auth_key(self): """ 生成授权码。 @@ -218,30 +228,26 @@ class WeChatPadProAdapter(Platform): params = {"key": self.admin_key} payload = {"Count": 1, "Days": 365} # 生成一个有效期365天的授权码 + self.auth_key = None # Reset auth_key before generating a new one + async with aiohttp.ClientSession() as session: try: async with session.post(url, params=params, json=payload) as response: + if response.status != 200: + logger.error(f"生成授权码失败: {response.status}, {await response.text()}") + return + response_data = await response.json() - # 修正成功判断条件和授权码提取路径 - if response.status == 200 and response_data.get("Code") == 200: - # 授权码在 Data 字段的列表中 - data = response_data.get("Data") - if data: - # 新返回格式 - if isinstance(data.get("authKeys"), list) and data["authKeys"]: - self.auth_key = data["authKeys"][0] - # 兼容旧版接口 - elif isinstance(data, list) and data: - self.auth_key = data[0] - - if self.auth_key: - logger.info(f"成功获取授权码 {self.auth_key[:8]}...") - else: - logger.error(f"生成授权码成功但未找到授权码: {response_data}") + if response_data.get("Code") == 200: + if data := response_data.get("Data"): + self.auth_key = self._extract_auth_key(data) + + if self.auth_key: + logger.info("成功获取授权码") else: logger.error(f"生成授权码成功但未找到授权码: {response_data}") else: - logger.error(f"生成授权码失败: {response.status}, {response_data}") + logger.error(f"生成授权码失败: {response_data}") except aiohttp.ClientConnectorError as e: logger.error(f"连接到 WeChatPadPro 服务失败: {e}") except Exception as e: