Kaynağa Gözat

检查好友关系

develop
H Vs 2 ay önce
ebeveyn
işleme
68ce30d566
3 değiştirilmiş dosya ile 52 ekleme ve 7 silme
  1. +1
    -1
      app.py
  2. +14
    -5
      resources/messages_resource.py
  3. +37
    -1
      wechat/gewe_chat.py

+ 1
- 1
app.py Dosyayı Görüntüle

@@ -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():


+ 14
- 5
resources/messages_resource.py Dosyayı Görüntüle

@@ -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('群聊好友通过验证及好友资料变更的通知消息')


+ 37
- 1
wechat/gewe_chat.py Dosyayı Görüntüle

@@ -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 缓存。


Yükleniyor…
İptal
Kaydet