Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update app.py for end to end handling of all FAQS calls serving #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, request, jsonify
from flask import Flask, request, jsonify
import json

app = Flask(__name__)
Expand All @@ -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}",
Expand Down