diff --git a/app.py b/app.py index cea237b..060a74d 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,4 @@ -from flask import Flask, request, jsonify +from flask import Flask, request, jsonify import json app = Flask(__name__) @@ -7,32 +7,28 @@ with open('menu_structure.json', 'r', encoding="utf-8") as file: menu_structure = json.load(file) -MENU_STATE = 'menu' -# Initialize current state -current_state = "menu" - @app.route('/') def home(): return "Welcome to the Chatbot API! Use the /chatbot endpoint to interact with the bot." @app.route('/chatbot', methods=['POST']) def chatbot(): - global current_state user_choice = request.json.get('choice', '').strip() - + + # Determine the current state based on the user choice if user_choice.lower() == 'back to main menu': - # Reset to main menu - current_state = MENU_STATE - elif user_choice.lower() == MENU_STATE: - pass - elif user_choice in menu_structure.get(current_state, []): + current_state = 'menu' + elif user_choice.lower() == 'menu': + current_state = 'menu' + elif user_choice in menu_structure: current_state = user_choice else: - return jsonify({"error": "Invalid choice. Please type menu to get the faq."}) + return jsonify({"error": "Invalid choice. Please type menu to get the faq."}), 400 # Get the current state data - state_data = menu_structure[current_state] - + state_data = menu_structure.get(current_state, []) + + # Prepare the response based on the type of state data if isinstance(state_data, list): response = { "question": f"Please choose an option from {current_state}",