Files
ds_zagres_ai/app/api/routes.py
T

101 lines
2.1 KiB
Python
Raw Normal View History

2025-05-09 15:41:16 +01:00
"""
Flask routes for the application.
"""
from flask import Blueprint, jsonify, request, abort
from app.services.chatbot_service import chatbot_service
bp = Blueprint('main', __name__)
@bp.route('/')
def index():
"""
Root endpoint.
Returns:
JSON response with application information.
"""
return jsonify({
'name': 'Chatbot Application',
'version': '1.0.0',
'status': 'running'
})
@bp.route('/api/health')
def health_check():
"""
Health check endpoint.
Returns:
JSON response with health status.
"""
return jsonify({
'status': 'healthy'
})
@bp.route('/api/chat', methods=['POST'])
def create_chat():
"""
Create a new chat.
Returns:
JSON response with chat ID.
"""
user_id = request.json.get('user_id', 'default_user')
chat_id = chatbot_service.create_chat(user_id)
return jsonify({
'chat_id': chat_id,
'messages': []
})
@bp.route('/api/chat/<int:chat_id>/message', methods=['POST'])
def send_message(chat_id):
"""
Send a message to the chatbot.
Args:
chat_id: ID of the chat.
Returns:
JSON response with bot response.
"""
if not request.json or 'message' not in request.json:
abort(400, description="Message is required")
try:
message = request.json['message']
response = chatbot_service.get_response(chat_id, message)
# Get the last message (bot response)
messages = chatbot_service.get_chat_messages(chat_id)
last_message = messages[-1]
return jsonify(last_message)
except ValueError as e:
abort(404, description=str(e))
@bp.route('/api/chat/<int:chat_id>', methods=['GET'])
def get_chat(chat_id):
"""
Get a chat by ID.
Args:
chat_id: ID of the chat.
Returns:
JSON response with chat messages.
"""
try:
messages = chatbot_service.get_chat_messages(chat_id)
return jsonify({
'chat_id': chat_id,
'messages': messages
})
except ValueError as e:
abort(404, description=str(e))