From 5eacc7a86ae1ac2422a6603985a48e5239682d0b Mon Sep 17 00:00:00 2001 From: liuwei Date: Thu, 29 May 2025 16:23:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=81=8A=E5=A4=A9=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/dashboard/blueprints/contacts.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/admin/dashboard/blueprints/contacts.py b/admin/dashboard/blueprints/contacts.py index aa5b456..f6cfb34 100644 --- a/admin/dashboard/blueprints/contacts.py +++ b/admin/dashboard/blueprints/contacts.py @@ -193,6 +193,16 @@ def api_contacts_update(): return jsonify({"success": False, "message": f"更新通讯录失败: {str(e)}"}), 500 +def get_or_create_eventloop(): + """获取或创建事件循环""" + try: + loop = asyncio.get_event_loop() + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + return loop + + @contacts_bp.route('/api/send_message', methods=['POST']) @login_required def api_send_message(): @@ -219,9 +229,12 @@ def api_send_message(): if not server or not server.client: return jsonify({'success': False, 'message': '机器人未初始化'}) + # 获取事件循环 + loop = get_or_create_eventloop() + # 根据消息类型调用不同的发送方法 if msg_type == 'text': - client_msg_id, create_time, new_msg_id = asyncio.run( + client_msg_id, create_time, new_msg_id = loop.run_until_complete( server.client.send_text_message(wxid, content) ) return jsonify({ @@ -237,7 +250,7 @@ def api_send_message(): if 'file' not in request.files: return jsonify({'success': False, 'message': '未上传文件'}) file = request.files['file'] - client_msg_id, create_time, new_msg_id = asyncio.run( + client_msg_id, create_time, new_msg_id = loop.run_until_complete( server.client.send_image_message(wxid, file.read()) ) return jsonify({ @@ -253,7 +266,7 @@ def api_send_message(): if 'file' not in request.files: return jsonify({'success': False, 'message': '未上传文件'}) file = request.files['file'] - client_msg_id, create_time, new_msg_id = asyncio.run( + client_msg_id, create_time, new_msg_id = loop.run_until_complete( server.client.send_voice_message(wxid, file.read()) ) return jsonify({ @@ -269,7 +282,7 @@ def api_send_message(): if 'file' not in request.files: return jsonify({'success': False, 'message': '未上传文件'}) file = request.files['file'] - client_msg_id, new_msg_id = asyncio.run( + client_msg_id, new_msg_id = loop.run_until_complete( server.client.send_video_message(wxid, file.read()) ) return jsonify({ @@ -284,7 +297,7 @@ def api_send_message(): url = content.get('url') title = content.get('title', '') description = content.get('description', '') - client_msg_id, create_time, new_msg_id = asyncio.run( + client_msg_id, create_time, new_msg_id = loop.run_until_complete( server.client.send_link_message(wxid, url, title, description) ) return jsonify({