From 2023303e04dc0309d27a9b94d537def15e488188 Mon Sep 17 00:00:00 2001 From: Sagar Kori <129517558+sagarkori143@users.noreply.github.com> Date: Sat, 15 Jun 2024 15:08:41 +0530 Subject: [PATCH] Update app.py for end to end handling of all FAQS calls serving --- app.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) 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}",