添加聊天功能
This commit is contained in:
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user