diff --git a/app.py b/app.py index 3d9f6fe..777ecba 100644 --- a/app.py +++ b/app.py @@ -109,14 +109,14 @@ def auto_add_contacts_from_chatrooms(): status=r.get('status') # 启动线程处理登录信息 - thread = threading.Thread(target=process_add_contacts_from_chatrooms, args=(wxchat,status, nickname, wxid, token_id, app_id)) + thread = threading.Thread(target=process_add_contacts_from_chatrooms, args=(wxchat,status, wxid, token_id, app_id)) thread.start() time.sleep(3) #time.sleep(60*10) time.sleep(3600*24) -def process_add_contacts_from_chatrooms(wxchat:gewe_chat.GeWeChatCom,status, nickname, wxid, token_id, app_id): +def process_add_contacts_from_chatrooms(wxchat:gewe_chat.GeWeChatCom,status, wxid, token_id, app_id): if status == '1': c = wxchat.get_wxchat_config_from_cache(wxid) @@ -145,6 +145,8 @@ def process_add_contacts_from_chatrooms(wxchat:gewe_chat.GeWeChatCom,status, nic 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] + nickname = next((member['nickName'] for member in chatroot_member_list if member['nickName'] == wxid), None) + logger.info(f'{nickname} 在 {chatroom_nickname} 群里还可以邀请的好友有:{[x.get("nickName") for x in remaining_chatroot_members]}') 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}') diff --git a/resources/messages_resource.py b/resources/messages_resource.py index cf5c8a9..3f9b4d3 100644 --- a/resources/messages_resource.py +++ b/resources/messages_resource.py @@ -35,6 +35,17 @@ class MessagesResource(Resource): wxid = msg.get("Wxid",'') wx_config = gewe_chat.wxchat.get_wxchat_config_from_cache(wxid) + + # 自发命令 + # if type_name=='AddMsg': + # from_wxid = msg_data["FromUserName"]["string"] + # if wxid=='from_wxid': + # pass + if type_name == 'AddMsg': + handle_self_cmd(wxid,msg) + + + if not bool(wx_config.get("agentEnabled",False)): logger.info(f'微信ID {wxid} 未托管,不处理') return jsonify({"message": "收到微信回调消息"}) @@ -110,9 +121,28 @@ class MessagesResource(Resource): logger.warning(f"未知消息类型") return jsonify({"message": "收到微信回调消息"}) - + +def handle_self_cmd(wxid,msg): + msg_data=msg.get("Data") + from_wxid=msg_data["FromUserName"]["string"] + msg_content=msg_data["Content"]["string"] + if wxid == from_wxid: + commands = { + '启用托管': True, + '关闭托管': False + } + if msg_content in commands: + c_dict = gewe_chat.wxchat.get_wxchat_config_from_cache(wxid) + if c_dict: + from model import Models + agent_config=Models.AgentConfig.model_validate(c_dict) + agent_config.agentEnabled=commands[msg_content] + + gewe_chat.wxchat.save_wxchat_config(wxid, agent_config.model_dump()) + logger.info(f'{wxid} {"启动" if commands[msg_content] else "关闭"}托管') def handle_text(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): + ''' 私聊文本消息 ''' @@ -237,7 +267,7 @@ def handle_text(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): # 取消定时器 timeout_timer.cancel() -def check_timeout( task_thread:threading.Thread, token_id,wxid, app_id, callback_to_user): +def check_timeout(task_thread:threading.Thread, token_id,wxid, app_id, callback_to_user): if task_thread.is_alive(): print(f"任务运行时间超过{timeout_duration}秒,token_id={token_id}, app_id={app_id}, callback_to_user={callback_to_user}") wx_config = gewe_chat.wxchat.get_wxchat_config_from_cache(wxid) @@ -873,6 +903,9 @@ def check_chatroom(userName): return True return False + + + # def remove_markdown_symbol(text: str): # # 移除markdown格式,目前先移除** # if not text or not isinstance(text, str): diff --git a/wechat/gewe_chat.py b/wechat/gewe_chat.py index dfca8e6..71820a1 100644 --- a/wechat/gewe_chat.py +++ b/wechat/gewe_chat.py @@ -953,7 +953,7 @@ class GeWeChatCom: config = redis_helper.redis_helper.get_hash_field(hash_key, wxid) return json.loads(config) if config else {} - def save_wxchat_config(self, wxid, config): + def save_wxchat_config(self, wxid, config:dict): """ 保存配置信息 """