@@ -4,7 +4,7 @@ from resources.messages_resource import MessagesResource | |||
from resources.contacts_resources import DeleteFriendResource,GetFriendsInfoResource | |||
from resources.config_reources import GetWxchatConfigResource ,SaveWxchatConfigResource | |||
from resources.groups_resources import GetGroupsInfoResource | |||
from resources.login_resources import GetLoginInfoResource | |||
from resources.login_resources import GetLoginInfoResource,GetLoginWxQRCodeResource | |||
from resources.sns_resources import SendSNSTextResource,SendSNSImageResource, SendSNSVideoResource | |||
from common.log import logger, log_exception | |||
from common.interceptors import before_request, after_request, handle_exception | |||
@@ -76,6 +76,7 @@ def fetch_and_save_contacts2(): | |||
logger.info(f'微信ID {wxid} 登录APPID {app_id} 成功,联系人已定时保存') | |||
chatrooms=contacts_list['chatrooms'] | |||
wxchat.save_groups_info_to_cache(token_id, app_id, wxid, chatrooms) | |||
wxchat.save_groups_members_to_cache(token_id, app_id, wxid, chatrooms) | |||
logger.info(f'微信ID {wxid} 登录APPID {app_id} 成功,群信息已定时保存') | |||
else: | |||
@@ -122,10 +123,11 @@ def process_add_contacts_from_chatrooms(wxchat:gewe_chat.GeWeChatCom,status, nic | |||
for chatroom_id in chatrooms: | |||
chatroom = wxchat.get_group_info_from_cache(wxid, chatroom_id) | |||
chatroom_member=wxchat.get_group_members_from_cache(wxid, chatroom_id) | |||
chatroom_nickname = chatroom.get('nickName') | |||
chatroom_owner_wxid = chatroom.get('chatroomOwner', None) | |||
admin_wxid = chatroom.get('adminWxid', None) | |||
chatroom_owner_wxid = chatroom_member.get('chatroomOwner', None) | |||
admin_wxid = chatroom_member.get('adminWxid', None) | |||
logger.info(f'{chatroom_nickname} 的群主是 {chatroom_owner_wxid},管理员是{admin_wxid}') | |||
contact_wxids_set = set(contact_wxids) | |||
if admin_wxid is not None: | |||
contact_wxids_set.add(admin_wxid) | |||
@@ -222,7 +224,8 @@ flask_api.add_resource(SaveWxchatConfigResource, '/api/wxchat/saveconfig') | |||
flask_api.add_resource(GetGroupsInfoResource, '/api/groups/getchatroominfo') | |||
flask_api.add_resource(GetLoginInfoResource, '/api/agent/getlogin') | |||
flask_api.add_resource(GetLoginInfoResource, '/api/agent/getlogin') | |||
flask_api.add_resource(GetLoginWxQRCodeResource, '/api/agent/getwxqrcode') | |||
flask_api.add_resource(SendSNSTextResource, '/api/sns/sendtext') | |||
flask_api.add_resource(SendSNSImageResource, '/api/sns/sendimages') | |||
@@ -28,3 +28,23 @@ class LoginWxCaptchCodeResource(Resource): | |||
res=self.wxchat.save_login_wx_captch_code_to_cache(token_id,captch_code) | |||
return jsonify(res) | |||
class GetLoginWxQRCodeResource(Resource): | |||
def __init__(self): | |||
self.parser = reqparse.RequestParser() | |||
self.wxchat = gewe_chat.wxchat | |||
def post(self): | |||
req = request.get_json() | |||
token_id = req.get("tokenId") | |||
tel= req.get("tel") | |||
region_id= req.get("regionId") | |||
agent_token_id= req.get("agentTokenId") | |||
data={ | |||
"tokenId": token_id, | |||
"tel": tel, | |||
"base64Img": "data:image/png;base64,"+"aaaaaa", | |||
"expiredTime": "12345678901", | |||
} | |||
return jsonify(data) | |||
@@ -36,7 +36,7 @@ class MessagesResource(Resource): | |||
wxid = msg.get("Wxid",'') | |||
wx_config = gewe_chat.wxchat.get_wxchat_config_from_cache(wxid) | |||
if not bool(wx_config.get("agentEnabled",False)): | |||
logger.info('智能体未启用,不处理') | |||
logger.info(f'微信ID {wxid} 未托管,不处理') | |||
return jsonify({"message": "收到微信回调消息"}) | |||
if type_name=='AddMsg': | |||
@@ -65,6 +65,7 @@ class MessagesResource(Resource): | |||
ret,msg,data=gewe_chat.wxchat.save_contract_list(token_id,app_id,chatroom_id,3) | |||
logger.info(f'保存到通讯录 chatroom_id {chatroom_id} {msg}') | |||
gewe_chat.wxchat.update_group_info_to_cache(token_id,app_id,wxid,chatroom_id) | |||
gewe_chat.wxchat.update_group_members_to_cache(token_id,app_id,wxid,chatroom_id) | |||
handlers[1]=handle_text_group | |||
handlers[3]=handle_image_group | |||
handlers[34]=handle_voice_group | |||
@@ -695,6 +696,7 @@ def handle_10002_msg(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): | |||
ret,msg,data=gewe_chat.wxchat.save_contract_list(token_id,app_id,chatroom_id,3) | |||
logger.info(f'群聊邀请,保存到通讯录 chatroom_id {chatroom_id} {msg}') | |||
gewe_chat.wxchat.update_group_info_to_cache(token_id,app_id,wxid,chatroom_id) | |||
gewe_chat.wxchat.update_group_members_to_cache(token_id,app_id,wxid,chatroom_id) | |||
if '移出了群聊' in msg_content_xml and 'sysmsgtemplate' in msg_content_xml : | |||
chatroom_id=msg_data["FromUserName"]["string"] | |||
@@ -342,6 +342,7 @@ def login_or_reconnect(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, region_id | |||
# else: | |||
# login_info.update({"login_time":datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]}) | |||
cleaned_login_info = {k: (v if v is not None else '') for k, v in login_info.items()} | |||
redis_helper.redis_helper.set_hash(hash_key, cleaned_login_info) | |||
wxchat.release_login_lock(token_id) | |||
# 默认配置 | |||
@@ -355,8 +356,8 @@ def login_or_reconnect(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, region_id | |||
}) | |||
config_dict=config.model_dump() | |||
wxchat.save_wxchat_config(agent_tel,config_dict) | |||
wxid=cleaned_login_info.get('wxid',agent_tel) | |||
wxchat.save_wxchat_config(wxid,config_dict) | |||
return login_info | |||
else: | |||
logger.info(f"登录检查中: {ret}-{msg}-{res}") | |||
@@ -576,6 +576,21 @@ class GeWeChatCom: | |||
response_object = response.json() | |||
return response_object.get('ret',None),response_object.get('msg',None),response_object.get('data',None) | |||
def get_group_memberlist(self,token_id,app_id,chatroom_id): | |||
api_url = f"{self.base_url}/v2/api/group/getChatroomMemberList" | |||
headers = { | |||
'X-GEWE-TOKEN': token_id, | |||
'Content-Type': 'application/json' | |||
} | |||
data = { | |||
"appId": app_id, | |||
"chatroomId": chatroom_id, | |||
} | |||
response = requests.post(url=api_url, headers=headers, data=json.dumps(data)) | |||
response_object = response.json() | |||
#print(response_object) | |||
return response_object.get('ret',None),response_object.get('msg',None),response_object.get('data',None) | |||
############################### 朋友圈模块 ################################### | |||
# 在新设备登录后的1-3天内,您将无法使用朋友圈发布、点赞、评论等功能。在此期间,如果尝试进行这些操作,您将收到来自微信团队的提醒。请注意遵守相关规定。 | |||
@@ -849,6 +864,53 @@ class GeWeChatCom: | |||
# 更新缓存 | |||
redis_helper.redis_helper.update_hash_field(hash_key, chatroom_id, json.dumps(data, ensure_ascii=False)) | |||
time.sleep(1) | |||
def save_groups_members_to_cache(self, token_id, app_id, wxid, chatroom_ids: list): | |||
""" | |||
将群成员保存到 Redis 缓存。 | |||
""" | |||
# Redis 缓存的 key | |||
hash_key = f"__AI_OPS_WX__:GROUPS_MEMBERS:{wxid}" | |||
# 获取当前缓存中所有的 chatroom_id | |||
existing_chatrooms = redis_helper.redis_helper.get_hash(hash_key) | |||
# 找出需要删除的 chatroom_ids | |||
chatrooms_to_delete = set(existing_chatrooms.keys()) - set(chatroom_ids) | |||
# 删除缓存中不再需要的 chatroom_id 数据 | |||
for chatroom_id in chatrooms_to_delete: | |||
redis_helper.redis_helper.delete_hash_field(hash_key, chatroom_id) | |||
for chatroom_id in chatroom_ids: | |||
# 获取群信息 | |||
ret, msg, data = self.get_group_memberlist(token_id, app_id, chatroom_id) | |||
if ret != 200: | |||
continue | |||
# 更新缓存 | |||
redis_helper.redis_helper.update_hash_field(hash_key, chatroom_id, json.dumps(data, ensure_ascii=False)) | |||
time.sleep(1) | |||
def update_group_members_to_cache(self, token_id, app_id, wxid, chatroom_id: str): | |||
""" | |||
更新将群信息保存到 Redis 缓存。 | |||
""" | |||
# Redis 缓存的 key | |||
hash_key = f"__AI_OPS_WX__:GROUPS_MEMBERS:{wxid}" | |||
# 获取群信息 | |||
ret, msg, data = self.get_group_memberlist(token_id, app_id, chatroom_id) | |||
redis_helper.redis_helper.update_hash_field(hash_key, chatroom_id, json.dumps(data, ensure_ascii=False)) | |||
def get_group_members_from_cache(self, wxid,chatroom_id)->dict: | |||
""" | |||
获取缓存中群成员。 | |||
""" | |||
hash_key = f"__AI_OPS_WX__:GROUPS_MEMBERS:{wxid}" | |||
cache = redis_helper.redis_helper.get_hash_field(hash_key,chatroom_id) | |||
groups=json.loads(cache) if cache else {} | |||
return groups | |||
def update_group_info_to_cache(self, token_id, app_id, wxid, chatroom_id: str): | |||
""" | |||
@@ -861,6 +923,7 @@ class GeWeChatCom: | |||
ret, msg, data = self.get_chatroom_info(token_id, app_id, chatroom_id) | |||
redis_helper.redis_helper.update_hash_field(hash_key, chatroom_id, json.dumps(data, ensure_ascii=False)) | |||
def get_groups_info_from_cache(self, wxid)->list: | |||
""" | |||
获取群信息保存到 Redis 缓存。 | |||