From 1a4d40174e82a595d9f3a7b7de87b5d108128a05 Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 23 Apr 2025 11:48:47 +0800 Subject: [PATCH] =?UTF-8?q?server=E5=90=AF=E5=8A=A8=E4=B9=8B=E5=90=8E?= =?UTF-8?q?=EF=BC=8C=E5=A1=AB=E5=85=A5callback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robot.py | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/robot.py b/robot.py index cfa5a38..1d971f3 100644 --- a/robot.py +++ b/robot.py @@ -41,6 +41,7 @@ class Robot(Job): def __init__(self, config: Config) -> None: self.client = gewe_client.client self.config = config + self.app_id = gewe_client.app_id self.LOG = logging.getLogger("Robot") self.LOG.info(f"DB+REDIS 连接池开始初始化") @@ -62,7 +63,7 @@ class Robot(Job): self.contact_manager.set_contacts(self.allContacts) # 获取个人信息 - obj = json_to_object(self.client.get_profile(self.client.app_id)) + obj = json_to_object(self.client.get_profile(self.app_id)) if obj.data.wxid is None: self.LOG.info(f"获取个人信息失败,退出程序!") return @@ -407,80 +408,80 @@ class Robot(Job): try: # 先尝试从数据库获取联系人信息 contacts_dict = self.contacts_db.get_all_contacts() - + # 如果数据库中有联系人信息,直接返回 if contacts_dict: self.LOG.info(f"从数据库成功获取了 {len(contacts_dict)} 个联系人信息") return contacts_dict - + # 数据库中没有联系人信息,需要初始化 self.LOG.info("数据库中没有联系人信息,开始初始化...") - + contacts_dict = {} # 获取所有联系人列表 response = self.client.fetch_contacts_list(self.app_id) self.LOG.info(f"获取联系人列表响应: {response}") - + if not response or response.get("ret") != 200: self.LOG.warning(f"获取联系人列表失败: {response}") return contacts_dict - + # 从响应中提取联系人数据 contact_data = response.get("data", {}) - + # 处理好友列表 friends = contact_data.get("friends", []) for wxid in friends: contacts_dict[wxid] = wxid # 默认使用wxid作为昵称 - + # 处理群聊列表 chatrooms = contact_data.get("chatrooms", []) for chatroom_id in chatrooms: contacts_dict[chatroom_id] = chatroom_id # 如果是群聊,则获取群成员信息 self.update_chatroom_member_details(chatroom_id) - + # 处理公众号列表 ghs = contact_data.get("ghs", []) for gh_id in ghs: contacts_dict[gh_id] = gh_id - + # 获取联系人详细信息(昵称等) self.update_contact_details(contacts_dict) - + self.LOG.info(f"成功获取并保存{len(contacts_dict)}个联系人信息") return contacts_dict - + except Exception as e: self.LOG.error(f"获取联系人信息失败: {e}") return {} - + def update_contact_details(self, contacts_dict): """更新联系人详细信息(昵称等)""" try: # 将wxid列表分批处理,每批50个 batch_size = 10 wxids = list(contacts_dict.keys()) - + for i in range(0, len(wxids), batch_size): batch_wxids = wxids[i:i + batch_size] - + # 批量获取联系人详细信息 - contact_info = self.client.get_detail_info(self.client.app_id, batch_wxids) + contact_info = self.client.get_detail_info(self.app_id, batch_wxids) self.LOG.info(f"获取联系人详细信息响应: {contact_info}") # 处理返回的数据 if contact_info and contact_info.get("ret") == 200 and "data" in contact_info: contact_data = contact_info.get("data", []) - + if contact_data: for contact in contact_data: user_name = contact.get("userName") if not user_name or user_name not in contacts_dict: continue - + # 更新昵称 contacts_dict[user_name] = contact.get("nickName") or user_name - + try: # 判断联系人类型 contact_type = "friends" # 默认为好友类型 @@ -488,16 +489,16 @@ class Robot(Job): contact_type = "chatrooms" elif user_name.startswith("gh_"): contact_type = "ghs" - + # 保存到数据库 self.contacts_db.save_contacts([contact], contact_type) - + except Exception as e: self.LOG.error(f"处理联系人 {user_name} 失败: {e}") continue else: self.LOG.error(f"获取联系人详情失败: {contact_info}") - + except Exception as e: self.LOG.error(f"更新联系人详细信息失败: {e}") @@ -505,7 +506,7 @@ class Robot(Job): """更新群成员详细信息""" try: # 首先获取群成员列表 - members_response = self.client.get_chatroom_member_list(self.client.app_id, chatroom_id) + members_response = self.client.get_chatroom_member_list(self.app_id, chatroom_id) if members_response and members_response.get('ret') == 200: member_list = members_response.get('data', {}).get('memberList', []) @@ -515,7 +516,7 @@ class Robot(Job): if member_wxids: # 按照官方接口格式传递参数 details_response = self.client.get_chatroom_member_detail( - self.client.app_id, + self.app_id, chatroom_id, member_wxids # 直接传递列表,不需要转换为集合 )