From 497f4b124165c9369647ed95aa1e3148e832d5a9 Mon Sep 17 00:00:00 2001 From: H Vs Date: Thu, 20 Feb 2025 17:50:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=B7=BB=E5=8A=A0=E5=A5=BD?= =?UTF-8?q?=E5=8F=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 46 +++++++++++++++++++++++++++++++--- resources/messages_resource.py | 2 +- wechat/gewe_chat.py | 9 +++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 94662f1..e52b562 100644 --- a/app.py +++ b/app.py @@ -87,15 +87,55 @@ def fetch_and_save_contacts2(): time.sleep(3600*1) -def auto_contacts_from_chatroom_id(): - print('自动从群添加好友') +def auto_contacts_from_chatroom(): + logger.info('自动从群添加好友') + wxchat=gewe_chat.wxchat + while True: + + login_keys = list(redis_helper.redis_helper.client.scan_iter(match='__AI_OPS_WX__:LOGININFO:*')) + logger.info(f"Fetching login keys: {login_keys}") + + # 遍历每一个获取到的登录键 + for k in login_keys: + r= redis_helper.redis_helper.get_hash(k) + # print(r) + token_id = r.get('tokenId') + app_id = r.get('appId') + nickname=r.get('nickName') + wxid = r.get('wxid') + status=r.get('status') + if status=='1': + c = wxchat.get_wxchat_config_from_cache(app_id) + contacts = wxchat.get_contacts_brief_from_cache(wxid) + contact_wxids = [c.get('userName') for c in contacts] + + chatrooms = c.get('addContactsFromChatroomIdWhiteList',[]) + for chatroom_id in chatrooms: + chatroom = wxchat.get_group_info_from_cache(wxid,chatroom_id) + chatroom_nickname=chatroom.get('nickName') + + chatroot_member_list=chatroom.get('memberList',[]) + contact_set = set(contact_wxids) + remaining_chatroot_members = [x for x in chatroot_member_list if x.get('wxid') not in contact_set] + for m in remaining_chatroot_members: + ret,msg,data=wxchat.add_group_member_as_friend(token_id,app_id,chatroom_id,m.get('wxid'),f'您好,我是来自{chatroom_nickname}群的{nickname}') + logger.info(f'{nickname} 向 {chatroom_nickname}群的{m.get('nickname')}发送好友邀请 {msg}') + time.sleep(3) + time.sleep(3) + else: + logger.info(f'微信ID {wxid} 未登录 {app_id} ,群成员不能定时定时') + + time.sleep(3) + + #time.sleep(60*10) + time.sleep(3600*24) def start_wxchat_thread(): # gewe_chat.start() scan_wx_login_info() # 启动同步联系人线程 threading.Thread(target=fetch_and_save_contacts2).start() - + #threading.Thread(target=auto_contacts_from_chatroom).start() diff --git a/resources/messages_resource.py b/resources/messages_resource.py index 3435ee7..9a53340 100644 --- a/resources/messages_resource.py +++ b/resources/messages_resource.py @@ -729,7 +729,7 @@ def handle_mod_contacts(token_id,app_id,wxid,msg_data): 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]) + gewe_chat.wxchat.delete_contacts_brief_from_cache(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) diff --git a/wechat/gewe_chat.py b/wechat/gewe_chat.py index 594f572..b8a4279 100644 --- a/wechat/gewe_chat.py +++ b/wechat/gewe_chat.py @@ -895,6 +895,15 @@ class GeWeChatCom: cache = redis_helper.redis_helper.get_hash(hash_key) groups=[json.loads(v) for v in cache.values()] return groups + + def get_group_info_from_cache(self, wxid,chatroom_id)->dict: + """ + 获取群信息保存到 Redis 缓存。 + """ + hash_key = f"__AI_OPS_WX__:GROUPS_INFO:{wxid}" + cache = redis_helper.redis_helper.get_hash_field(hash_key,chatroom_id) + groups=json.loads(cache) if cache else {} + return groups def get_wxchat_config_from_cache(self, wxid): """