From f7458572edce0d0672a0ef630ed418805d424f70 Mon Sep 17 00:00:00 2001 From: Zhenyi Wang Date: Sat, 17 May 2025 15:31:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20wechatpadpro=E5=AF=B9=E6=8E=A5?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=81=94=E7=B3=BB=E4=BA=BA=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wechatpadpro/wechatpadpro_adapter.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py index 47d4fa9d..1295db5a 100644 --- a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py +++ b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py @@ -627,3 +627,68 @@ class WeChatPadProAdapter(Platform): ) # 调用实例方法 send await sending_event.send(message_chain) + + + async def get_contact_list(self): + """ + 获取联系人列表。 + """ + url = f"{self.base_url}/friend/GetContactList" + params = {"key": self.auth_key} + payload = { + "CurrentChatRoomContactSeq": 0, + "CurrentWxcontactSeq": 0 + } + async with aiohttp.ClientSession() as session: + try: + async with session.post(url, params=params, json=payload) as response: + if response.status == 200: + result = await response.json() + if result.get("Code") == 200 and result.get("Data"): + contact_list = result.get("Data", {}).get("ContactList", {}).get("contactUsernameList", []) + return contact_list + else: + logger.error(f"获取联系人列表失败: {result}") + return None + else: + logger.error(f"获取联系人列表失败: {response.status}") + return None + except aiohttp.ClientConnectorError as e: + logger.error(f"连接到 WeChatPadPro 服务失败: {e}") + return None + except Exception as e: + logger.error(f"获取联系人列表时发生错误: {e}") + return None + + async def get_contact_details_list(self, room_wx_id_list: [str] = [], user_names : [str] = []) -> Optional[dict]: + """ + 获取联系人详情列表。 + """ + url = f"{self.base_url}/friend/GetContactDetailsList" + params = {"key": self.auth_key} + payload = { + "RoomWxIDList": room_wx_id_list, + "UserNames": user_names + } + async with aiohttp.ClientSession() as session: + try: + async with session.post(url, params=params, json=payload) as response: + if response.status == 200: + result = await response.json() + if result.get("Code") == 200 and result.get("Data"): + contact_list = result.get("Data", {}).get("contactList", {}) + return contact_list + else: + logger.error(f"获取联系人详情列表失败: {result}") + return None + else: + logger.error(f"获取联系人详情列表失败: {response.status}") + return None + except aiohttp.ClientConnectorError as e: + logger.error(f"连接到 WeChatPadPro 服务失败: {e}") + return None + except Exception as e: + logger.error(f"获取联系人详情列表时发生错误: {e}") + return None + + \ No newline at end of file From 7bb4ca5a1480ddfe24dbfe1939b98a1dd1b7f8ce Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Fri, 23 May 2025 17:01:57 +0800 Subject: [PATCH 2/2] perf: code quality --- .../wechatpadpro/wechatpadpro_adapter.py | 65 +++++++++---------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py index 1295db5a..5188540f 100644 --- a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py +++ b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py @@ -339,9 +339,7 @@ class WeChatPadProAdapter(Platform): logger.info(message) asyncio.create_task(self.handle_websocket_message(message)) except asyncio.TimeoutError: - logger.warning( - f"WebSocket 连接空闲超过 {wait_time} s" - ) + logger.warning(f"WebSocket 连接空闲超过 {wait_time} s") break except websockets.exceptions.ConnectionClosedOK: logger.info("WebSocket 连接正常关闭。") @@ -628,67 +626,66 @@ class WeChatPadProAdapter(Platform): # 调用实例方法 send await sending_event.send(message_chain) - async def get_contact_list(self): """ 获取联系人列表。 """ url = f"{self.base_url}/friend/GetContactList" params = {"key": self.auth_key} - payload = { - "CurrentChatRoomContactSeq": 0, - "CurrentWxcontactSeq": 0 - } + payload = {"CurrentChatRoomContactSeq": 0, "CurrentWxcontactSeq": 0} async with aiohttp.ClientSession() as session: try: async with session.post(url, params=params, json=payload) as response: - if response.status == 200: - result = await response.json() - if result.get("Code") == 200 and result.get("Data"): - contact_list = result.get("Data", {}).get("ContactList", {}).get("contactUsernameList", []) - return contact_list - else: - logger.error(f"获取联系人列表失败: {result}") - return None - else: + if response.status != 200: logger.error(f"获取联系人列表失败: {response.status}") return None + result = await response.json() + if result.get("Code") == 200 and result.get("Data"): + contact_list = ( + result.get("Data", {}) + .get("ContactList", {}) + .get("contactUsernameList", []) + ) + return contact_list + else: + logger.error(f"获取联系人列表失败: {result}") + return None except aiohttp.ClientConnectorError as e: logger.error(f"连接到 WeChatPadPro 服务失败: {e}") return None except Exception as e: logger.error(f"获取联系人列表时发生错误: {e}") return None - - async def get_contact_details_list(self, room_wx_id_list: [str] = [], user_names : [str] = []) -> Optional[dict]: + + async def get_contact_details_list( + self, room_wx_id_list: list[str] = None, user_names: list[str] = None + ) -> Optional[dict]: """ 获取联系人详情列表。 """ + if room_wx_id_list is None: + room_wx_id_list = [] + if user_names is None: + user_names = [] url = f"{self.base_url}/friend/GetContactDetailsList" params = {"key": self.auth_key} - payload = { - "RoomWxIDList": room_wx_id_list, - "UserNames": user_names - } + payload = {"RoomWxIDList": room_wx_id_list, "UserNames": user_names} async with aiohttp.ClientSession() as session: try: async with session.post(url, params=params, json=payload) as response: - if response.status == 200: - result = await response.json() - if result.get("Code") == 200 and result.get("Data"): - contact_list = result.get("Data", {}).get("contactList", {}) - return contact_list - else: - logger.error(f"获取联系人详情列表失败: {result}") - return None - else: + if response.status != 200: logger.error(f"获取联系人详情列表失败: {response.status}") return None + result = await response.json() + if result.get("Code") == 200 and result.get("Data"): + contact_list = result.get("Data", {}).get("contactList", {}) + return contact_list + else: + logger.error(f"获取联系人详情列表失败: {result}") + return None except aiohttp.ClientConnectorError as e: logger.error(f"连接到 WeChatPadPro 服务失败: {e}") return None except Exception as e: logger.error(f"获取联系人详情列表时发生错误: {e}") return None - - \ No newline at end of file