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()