diff --git a/services/gewe_service.py b/services/gewe_service.py index a312867..2b71c98 100644 --- a/services/gewe_service.py +++ b/services/gewe_service.py @@ -6,7 +6,7 @@ import io import json import os import threading -import time +import time,datetime import uuid from fastapi import FastAPI, Depends from common.singleton import singleton @@ -1135,7 +1135,25 @@ class GeWeService: wxids = [key for key, value in cache.items() if len(json.loads(value)) == 2] return wxids - + async def is_group_add_contacts_history_one_day_200_async(self, wxid, chatroom_id) -> bool: + # 生成 hash_key + hash_key = f"__AI_OPS_WX__:GROUPS_ADD_CONTACT_HISTORY:{wxid}:{chatroom_id}" + cache = await self.redis_service.get_hash(hash_key) + today_list = [] + today = datetime.datetime.now().date() + + for key, value in cache.items(): + value_data_list = json.loads(value) + for value_data in value_data_list: + add_time_date = datetime.datetime.fromtimestamp(value_data["addTime"]).date() + if add_time_date == today: + today_list.append(value_data) + if len(today_list) == 200: + return True + return False + + + async def enqueue_to_add_contacts_async(self,wxid,scene:int,v3,v4): """ 入列待添加好友 diff --git a/tasks.py b/tasks.py index 13e2790..5cb4be7 100644 --- a/tasks.py +++ b/tasks.py @@ -210,7 +210,7 @@ def scheduled_task_add_contacts_from_chatrooms(self, redis_config, kafka_config, async for key in redis_service.client.scan_iter(match='__AI_OPS_WX__:LOGININFO:*'): login_keys.append(key) - print(login_keys) + #print(login_keys) for k in login_keys: r = await redis_service.get_hash(k) app_id = r.get("appId") @@ -220,22 +220,32 @@ def scheduled_task_add_contacts_from_chatrooms(self, redis_config, kafka_config, if status == '0': logger.warning(f"微信号 {wxid} 已经离线,群成员不能定时添加") continue + c = await gewe_service.get_wxchat_config_from_cache_async(wxid) contacts = await gewe_service.get_contacts_brief_from_cache_async(wxid) + contact_wxids = [c.get('userName') for c in contacts] chatrooms = c.get('addContactsFromChatroomIdWhiteList', []) for chatroom_id in chatrooms: chatroom = await gewe_service.get_group_info_from_cache_async(wxid, chatroom_id) chatroom_member=await gewe_service.get_group_members_from_cache_async(wxid, chatroom_id) + chatroom_nickname = chatroom.get('nickName') chatroom_owner_wxid = chatroom_member.get('chatroomOwner', None) + admin_wxids = chatroom_member.get('adminWxid', []) admin_wxids = chatroom_member.get('adminWxid') if admin_wxids is None: admin_wxids = [] # 如果 admin_wxids 是 None,将其初始化为空列表 - + + # 判断当天群成员是否已经加了200个好友 + is_add_group_200_times = await gewe_service.is_group_add_contacts_history_one_day_200_async(wxid, chatroom_id) + if is_add_group_200_times: + logger.info(f"{wxid}在 {chatroom_nickname} 群成员已经加了200个好友,不再添加,群id:{chatroom_id}") + continue + logger.info(f'{chatroom_nickname} 的群主是 {chatroom_owner_wxid},管理员是{admin_wxids}') contact_wxids_set = set(contact_wxids) # for admin_wxid in admin_wxids: @@ -246,8 +256,8 @@ def scheduled_task_add_contacts_from_chatrooms(self, redis_config, kafka_config, contact_wxids_set.add(wxid) - unavailable_wixds=await gewe_service.check_wixd_group_add_contacts_history_async(wxid,chatroom_id) - contact_wxids_set.update(unavailable_wixds) + # unavailable_wixds=await gewe_service.check_wixd_group_add_contacts_history_async(wxid,chatroom_id) + # contact_wxids_set.update(unavailable_wixds) chatroot_member_list = chatroom.get('memberList', []) remaining_chatroot_members = [x for x in chatroot_member_list if x.get('wxid') not in contact_wxids_set] @@ -256,16 +266,52 @@ def scheduled_task_add_contacts_from_chatrooms(self, redis_config, kafka_config, logger.info(f'{nickname}-{wxid} 在 {chatroom_nickname} 群里还可以邀请的好友有:{[x.get("nickName") for x in remaining_chatroot_members]}') for m in remaining_chatroot_members: + contact_wxid= m.get('wxid') + member_nickname=m.get("nickName") + group_add_contacts_history = await gewe_service.get_group_add_contacts_history_async(wxid,chatroom_id,contact_wxid) + sorted_history = sorted(group_add_contacts_history, key=lambda x: x.addTime, reverse=True) + + # 已经邀请过两次,不再邀请 + if len(sorted_history)==2: + logger.info(f'{nickname}-{wxid}在{chatroom_nickname}-{chatroom_id} 群的 {member_nickname}-{contact_wxid} 已经邀请过2次,不再邀请') + continue + + # 当天邀请过,不再邀请 + if len(sorted_history) > 0: + last_add_time = sorted_history[0].addTime + def is_add_time_more_than_one_day(addTime: int) -> bool: + """ + 判断 addTime 是否与当前时间相隔大于 3600 × 24 秒 + :param addTime: Unix 时间戳 + :return: 如果 addTime 与当前时间相隔大于 3600 × 24 秒,返回 True;否则返回 False + """ + # 获取当前时间的时间戳 + current_time = time.time() + + # 计算时间戳差值 + time_difference = abs(current_time - addTime) + + # 检查是否大于 3600 × 24 秒 + return time_difference > 3600 * 24 + + is_more_than_one_day= is_add_time_more_than_one_day(last_add_time) + + if not is_more_than_one_day: + logger.info(f'{nickname}-{wxid}在{chatroom_nickname}-{chatroom_id} 群的{member_nickname}-{contact_wxid}已经当天邀请,不再邀请') + continue + + + ret, msg, data = await gewe_service.add_group_member_as_friend_async(token_id, app_id, chatroom_id, m.get('wxid'), f'我是群聊"{chatroom_nickname}"群的{nickname}') if ret==200: - contact_wxids= m.get('wxid') + history=AddGroupContactsHistory.model_validate({ "chatroomId":chatroom_id, "wxid":wxid, - "contactWixd":contact_wxids, + "contactWixd":contact_wxid, "addTime":int(time.time()) }) - await gewe_service.save_group_add_contacts_history_async(wxid,chatroom_id,contact_wxids,history) + await gewe_service.save_group_add_contacts_history_async(wxid,chatroom_id,contact_wxid,history) else: logger.info(f'群好友邀请失败原因:{data}') logger.info(f'{nickname} 向 {chatroom_nickname}-{chatroom_id} 群的 {m.get("nickName")}-{m.get("wxid")} 发送好友邀请 {msg}')