添加聊天功能
This commit is contained in:
@@ -191,3 +191,104 @@ def api_contacts_update():
|
||||
except Exception as e:
|
||||
logger.error(f"更新通讯录失败: {e}")
|
||||
return jsonify({"success": False, "message": f"更新通讯录失败: {str(e)}"}), 500
|
||||
|
||||
|
||||
@contacts_bp.route('/api/send_message', methods=['POST'])
|
||||
@login_required
|
||||
async def api_send_message():
|
||||
"""发送消息API
|
||||
|
||||
支持的消息类型:
|
||||
- text: 文本消息
|
||||
- image: 图片消息
|
||||
- voice: 语音消息
|
||||
- video: 视频消息
|
||||
- link: 链接消息
|
||||
"""
|
||||
try:
|
||||
data = request.form if request.files else request.json
|
||||
wxid = data.get('wxid')
|
||||
msg_type = data.get('type')
|
||||
content = data.get('content')
|
||||
|
||||
if not wxid or not msg_type:
|
||||
return jsonify({'success': False, 'message': '缺少必要参数'})
|
||||
|
||||
# 获取机器人实例
|
||||
server = current_app.dashboard_server
|
||||
if not server or not server.robot:
|
||||
return jsonify({'success': False, 'message': '机器人未初始化'})
|
||||
|
||||
# 根据消息类型调用不同的发送方法
|
||||
if msg_type == 'text':
|
||||
client_msg_id, create_time, new_msg_id = await server.robot.send_text_message(wxid, content)
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'data': {
|
||||
'client_msg_id': client_msg_id,
|
||||
'create_time': create_time,
|
||||
'new_msg_id': new_msg_id
|
||||
}
|
||||
})
|
||||
|
||||
elif msg_type == 'image':
|
||||
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())
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'data': {
|
||||
'client_msg_id': client_msg_id,
|
||||
'create_time': create_time,
|
||||
'new_msg_id': new_msg_id
|
||||
}
|
||||
})
|
||||
|
||||
elif msg_type == 'voice':
|
||||
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())
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'data': {
|
||||
'client_msg_id': client_msg_id,
|
||||
'create_time': create_time,
|
||||
'new_msg_id': new_msg_id
|
||||
}
|
||||
})
|
||||
|
||||
elif msg_type == 'video':
|
||||
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())
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'data': {
|
||||
'client_msg_id': client_msg_id,
|
||||
'new_msg_id': new_msg_id
|
||||
}
|
||||
})
|
||||
|
||||
elif msg_type == 'link':
|
||||
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)
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'data': {
|
||||
'client_msg_id': client_msg_id,
|
||||
'create_time': create_time,
|
||||
'new_msg_id': new_msg_id
|
||||
}
|
||||
})
|
||||
|
||||
else:
|
||||
return jsonify({'success': False, 'message': '不支持的消息类型'})
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"发送消息失败: {e}")
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
Reference in New Issue
Block a user