From 68ce30d566c544b9773a8335ad424e68b05a4ae5 Mon Sep 17 00:00:00 2001 From: H Vs Date: Wed, 19 Feb 2025 16:35:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E6=9F=A5=E5=A5=BD=E5=8F=8B=E5=85=B3?= =?UTF-8?q?=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 2 +- resources/messages_resource.py | 19 ++++++++++++----- wechat/gewe_chat.py | 38 +++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index b56ad0c..94662f1 100644 --- a/app.py +++ b/app.py @@ -84,7 +84,7 @@ def fetch_and_save_contacts2(): time.sleep(3) #time.sleep(60*10) - time.sleep(3600*24) + time.sleep(3600*1) def auto_contacts_from_chatroom_id(): diff --git a/resources/messages_resource.py b/resources/messages_resource.py index b55459a..3435ee7 100644 --- a/resources/messages_resource.py +++ b/resources/messages_resource.py @@ -723,11 +723,20 @@ def handle_mod_contacts(token_id,app_id,wxid,msg_data): contact_wxid = msg_data["UserName"]["string"] # 更新好友信息 - ret,msg,contacts_list = gewe_chat.wxchat.fetch_contacts_list(token_id, app_id) - friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围 - print(friend_wxids) - #friend_wxids.remove('weixin') - gewe_chat.wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids) + # 检查好友关系,不是好友则删除 + ret,msg,check_relation=gewe_chat.wxchat.check_relation(token_id, app_id,[contact_wxid]) + first_item = check_relation[0] + check_relation_status=first_item.get('relation') + logger.info(f'{wxid} 好友 {contact_wxid} 关系检查:{check_relation_status}') + if check_relation_status != 0: + gewe_chat.wxchat.delete_contacts_brief_from_cache(token_id, app_id, wxid, [contact_wxid]) + logger.info(f'好友关系异常:{check_relation_status},删除好友 {contact_wxid} 信息') + + # ret,msg,contacts_list = gewe_chat.wxchat.fetch_contacts_list(token_id, app_id) + # friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围 + # print(friend_wxids) + # #friend_wxids.remove('weixin') + #gewe_chat.wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids) else: logger.info('群聊好友通过验证及好友资料变更的通知消息') diff --git a/wechat/gewe_chat.py b/wechat/gewe_chat.py index c61e583..2ecde0e 100644 --- a/wechat/gewe_chat.py +++ b/wechat/gewe_chat.py @@ -422,7 +422,23 @@ class GeWeChatCom: print(response_object) return response_object.get('ret',None),response_object.get('msg',None) + def check_relation(self,token_id, app_id,wxids:list): + ''' + 检查好友关系 + ''' + api_url = f"{self.base_url}/v2/api/contacts/checkRelation" + headers = { + 'X-GEWE-TOKEN': token_id, + 'Content-Type': 'application/json' + } + data = { + "appId": app_id, + "wxids":wxids # list 1<= wxids <=20 + } + response = requests.post(url=api_url, headers=headers, data=json.dumps(data)) + response_object = response.json() + return response_object.get('ret',None),response_object.get('msg',None),response_object.get('data',None) ############################### 下载模块 ############################### def download_audio_msg(self,token_id:str,app_id:str,msg_id: int, xml: str): @@ -769,7 +785,27 @@ class GeWeChatCom: redis_helper.redis_helper.update_hash_field(hash_key, "data", json.dumps(cache, ensure_ascii=False)) return cache - + + def delete_contacts_brief_from_cache(self, wxid, contacts_wxids: list): + """ + 删除联系人信息保存到 Redis 缓存。 + """ + hash_key = f"__AI_OPS_WX__:CONTACTS_BRIEF:{wxid}" + cache_str = redis_helper.redis_helper.get_hash_field(hash_key, "data") + cache = json.loads(cache_str) if cache_str else [] + + + # 将 contacts_wxids 转换为集合,提高查找效率 + wxids_set = set(contacts_wxids) + + # 过滤 cache:保留 userName 不在集合中的对象 + filtered_cache = [contact for contact in cache if contact["userName"] not in wxids_set] + + # # 如果需要原地修改原 cache 列表: + # cache[:] = filtered_cache + redis_helper.redis_helper.update_hash_field(hash_key, "data", json.dumps(filtered_cache, ensure_ascii=False)) + + def save_contacts_brief_to_cache_prev(self, token_id, app_id, wxid, contacts_wxids: list): """ 将联系人信息保存到 Redis 缓存。