From 20a72cd41f6220445d4881c4c0ec6a1641d04011 Mon Sep 17 00:00:00 2001 From: liuwei Date: Thu, 29 May 2025 16:08:33 +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 | 28 ++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/admin/dashboard/blueprints/contacts.py b/admin/dashboard/blueprints/contacts.py index c20e4a5..7b73875 100644 --- a/admin/dashboard/blueprints/contacts.py +++ b/admin/dashboard/blueprints/contacts.py @@ -195,7 +195,7 @@ def api_contacts_update(): @contacts_bp.route('/api/send_message', methods=['POST']) @login_required -async def api_send_message(): +def api_send_message(): """发送消息API 支持的消息类型: @@ -219,9 +219,15 @@ async def api_send_message(): if not server or not server.robot: return jsonify({'success': False, 'message': '机器人未初始化'}) + # 创建事件循环 + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + # 根据消息类型调用不同的发送方法 if msg_type == 'text': - client_msg_id, create_time, new_msg_id = await server.robot.send_text_message(wxid, content) + client_msg_id, create_time, new_msg_id = loop.run_until_complete( + server.robot.send_text_message(wxid, content) + ) return jsonify({ 'success': True, 'data': { @@ -235,7 +241,9 @@ async 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 = await server.robot.send_image_message(wxid, file.read()) + client_msg_id, create_time, new_msg_id = loop.run_until_complete( + server.robot.send_image_message(wxid, file.read()) + ) return jsonify({ 'success': True, 'data': { @@ -249,7 +257,9 @@ async 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 = await server.robot.send_voice_message(wxid, file.read()) + client_msg_id, create_time, new_msg_id = loop.run_until_complete( + server.robot.send_voice_message(wxid, file.read()) + ) return jsonify({ 'success': True, 'data': { @@ -263,7 +273,9 @@ async 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 = await server.robot.send_video_message(wxid, file.read()) + client_msg_id, new_msg_id = loop.run_until_complete( + server.robot.send_video_message(wxid, file.read()) + ) return jsonify({ 'success': True, 'data': { @@ -276,7 +288,9 @@ async def api_send_message(): url = content.get('url') title = content.get('title', '') description = content.get('description', '') - client_msg_id, create_time, new_msg_id = await server.robot.send_link_message(wxid, url, title, description) + client_msg_id, create_time, new_msg_id = loop.run_until_complete( + server.robot.send_link_message(wxid, url, title, description) + ) return jsonify({ 'success': True, 'data': { @@ -292,3 +306,5 @@ async def api_send_message(): except Exception as e: logger.error(f"发送消息失败: {e}") return jsonify({'success': False, 'message': str(e)}), 500 + finally: + loop.close()