-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
50 lines (40 loc) · 1.38 KB
/
app.py
File metadata and controls
50 lines (40 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from flask import Flask, request, jsonify
import json
app = Flask(__name__)
# Load menu structure from JSON file
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()
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 = user_choice
else:
return jsonify({"error": "Invalid choice. Please type menu to get the faq."})
# Get the current state data
state_data = menu_structure[current_state]
if isinstance(state_data, list):
response = {
"question": f"Please choose an option from {current_state}",
"options": state_data
}
else:
response = {
"answer": state_data,
"options": ["Back to Main Menu"]
}
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=5000)