fromflaskimportBlueprint,render_template,request,jsonifyfromflask_loginimportlogin_required,current_userfromkusibot.servicesimportchatbot_servicefromkusibot.app.auth.utilsimportstandard_user_requiredimporttracebackchatbot_bp=Blueprint('chatbot_bp',__name__,template_folder='templates',static_folder='static')CHAT_ERROR_MSG="An error occurred. Sorry for the inconvenience :("
[docs]@chatbot_bp.route('/',methods=['GET'])@login_required@standard_user_requireddefchatbot():"""Render the chatbot interface. Requires user to be logged in. Returns: str: The chatbot HTML page to render. """# Gets the current conversation (last until session expires aka logs out)# or Creates a new oneconvo=chatbot_service.create_or_get_conversation(current_user.id)returnrender_template('chatbot.html',conversation=convo)
[docs]@chatbot_bp.route('/chat',methods=['POST'])@login_required@standard_user_requireddefchat():"""Handle user messages and return the chatbot responses. Returns: Response: The JSON response message from the chatbot. """try:# Get the message from the requestdata=request.jsonuser_message=data.get('message','')# If no user message, respond with a simple message.ifnotuser_message:returnjsonify({'response':chatbot_service.CHATBOT_NO_MSG_PROVIDED})# Return chatbot response as JSONbot_response=chatbot_service.get_response(user_message,current_user.id)returnjsonify({'response':bot_response["agent_response"],'agent_type':bot_response["agent_type"],'intent':bot_response["intent_detected"]})exceptException:returnjsonify({'response':CHAT_ERROR_MSG,'agent_type':None,'intent':None}),500