diff --git a/robot.py b/robot.py index 1818ff1..e0e8730 100644 --- a/robot.py +++ b/robot.py @@ -183,8 +183,9 @@ class Robot: # 登录成功后加载联系人信息 self.allContacts = self.get_all_contacts() + friends = await self.ipad_bot.get_contract_list() self.head_images = self.get_all_head_images() - self.contact_manager.set_contacts(self.allContacts, self.head_images) + self.contact_manager.set_contacts(self.allContacts, friends, self.head_images) self.message_storage = MessageStorage(self.ipad_bot) @@ -407,7 +408,8 @@ class Robot: self.head_images[wxid] = small_head_img_url - self.contact_manager.set_contacts(self.allContacts, self.head_images) + friends = await self.ipad_bot.get_contract_list() + self.contact_manager.set_contacts(self.allContacts, friends, self.head_images) self.LOG.info(f"已更新群 {group_id} 的成员信息") except Exception as e: self.LOG.error(f"获取群成员信息失败: {e}") @@ -454,10 +456,11 @@ class Robot: time.sleep(1) # 添加一个方法用于刷新联系人信息 - def refresh_contacts(self): + async def refresh_contacts(self): """刷新联系人信息""" self.allContacts = self.get_all_contacts() - self.contact_manager.refresh_contacts(self.allContacts) + friends = await self.ipad_bot.get_contract_list() + self.contact_manager.refresh_contacts(self.allContacts,friends) self.LOG.info("联系人信息已刷新") async def send_group_txt_message(self, msg: str, feature: Feature): @@ -607,8 +610,8 @@ class Robot: self.allContacts[wxid] = nick_name self.head_images[wxid] = small_head_img_url - - self.contact_manager.set_contacts(self.allContacts, self.head_images) + friends = await self.ipad_bot.get_contract_list() + self.contact_manager.set_contacts(self.allContacts, friends, self.head_images) self.LOG.info(f"已更新群 {group_id} 的成员信息") else: self.LOG.error(f"获取群 {group_id} 信息失败,证明用户无该群信息,删除群的相关资料。") diff --git a/utils/wechat/contact_manager.py b/utils/wechat/contact_manager.py index b77b3c2..8cc51ea 100644 --- a/utils/wechat/contact_manager.py +++ b/utils/wechat/contact_manager.py @@ -20,7 +20,7 @@ class ContactManager: _head_images: Dict[str, str] = {} # 头像信息 _initialized = False _logger = logger - _friends: List[Dict] = [] + _friends: List[str] = [] _group_contacts_friends: Dict[str, Dict[str, str]] = {} # 定义公共好友列表 _PUBLIC_FRIENDS = { @@ -51,7 +51,7 @@ class ContactManager: cls._instance = ContactManager() return cls._instance - def set_contacts(self, contacts: Dict[str, str], head_imgs: Dict[str, str]) -> None: + def set_contacts(self, contacts: Dict[str, str], friends: List[str], head_imgs: Dict[str, str]) -> None: """设置联系人字典 Args: @@ -68,7 +68,7 @@ class ContactManager: "gender": gender} """ self._contacts = contacts - self._friends = contacts + self._friends = friends self._head_images = head_imgs self._logger.info(f"联系人信息已更新,共 {len(contacts)} 个联系人") # 分类联系人 @@ -92,7 +92,9 @@ class ContactManager: elif wxid.endswith('@chatroom'): self._group_contacts[wxid] = nickname else: - self._personal_contacts[wxid] = nickname + # 判断 frinds 在contacts 里面,将在里面的用户分在 + if wxid in self._friends: + self._personal_contacts[wxid] = nickname self._logger.info(f"联系人分类完成: {len(self._group_contacts)} 个群组, " f"{len(self._personal_contacts)} 个个人联系人, " f"{len(self._public_contacts)} 个公共好友, " @@ -208,19 +210,20 @@ class ContactManager: # 需要获取群成员昵称信息; 从数据库里面提取。 # self._group_contacts_friends[wxid] = {} else: - self._personal_contacts[wxid] = nickname + if wxid in self._friends: + self._personal_contacts[wxid] = nickname self._logger.debug(f"已更新联系人: {wxid} -> {nickname}") - def refresh_contacts(self, new_contacts: Dict[str, str]) -> None: + def refresh_contacts(self, new_contacts: Dict[str, str], friends: List[str]) -> None: """刷新联系人信息 Args: new_contacts: 新的联系人字典 - head_imgs: 联系人头像字典,格式为 {"wxid": "http://xxxxx"} - wcf :wcf + friends: 联系人信息,格式为 ["wxid","wxid2"] """ self._contacts = new_contacts + self._friends = friends # friends: 好友清单 contact = { # "wxid": cnt.get("wxid", ""), # "code": cnt.get("code", ""),