-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
86 lines (65 loc) · 2.54 KB
/
app.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
import urllib
import json
import os
from flask import Flask, render_template
from flask import request
from flask import make_response
from ResponseSelection import ResponseSelector, FeatureOneSelector, FeatureTwoSelector
from Preprocessing import DataPreprocessing, MenuPreprocessing
from Data import Database
# Flask app should start in global layout
app = Flask(__name__)
responseSelector = None
@app.route('/index')
@app.route('/', methods=['POST', 'GET'])
def Home():
return render_template('index.html')
@app.route('/notify', methods=['POST','GET'])
def notify():
responseSelector = ResponseSelector.ResponseSelector()
responseSelector.notifyUser()
@app.route('/preprocessing', methods=['POST','GET'])
def preprocess():
m = MenuPreprocessing.MenuPreprocessing()
m.__run__()
p = DataPreprocessing.DataPreprocessing()
p.__run__()
@app.route('/webhook', methods=['POST', 'GET'])
def webhook():
req = request.get_json(silent=True, force=True)
print("Request:")
print(json.dumps(req, indent=4))
res = makeWebhookResult(req)
res = json.dumps(res, indent=4)
print(res)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
def makeWebhookResult(req):
action = req.get("result").get("action")
if "request_user_name" in action:
responseSelector = ResponseSelector.ResponseSelector()
return responseSelector.requestUserName(req, action)
elif action == "input.unknown":
query = (req.get("result")).get("resolvedQuery")
responseSelector = ResponseSelector.ResponseSelector()
return responseSelector.webSearch(query)
elif action == "Ask-a-question":
question = (req.get("result")).get("resolvedQuery")
responseSelector = FeatureOneSelector.FeatureOneSelector(question)
return responseSelector.getResult()
elif action == "request-game":
return FeatureTwoSelector.FeatureTwoSelector().getRandomQuestion()
elif action == "check-answer":
return FeatureTwoSelector.FeatureTwoSelector().CheckAnswerCorrectness(req.get("result").get("parameters"))
elif action == "get-correct-answer":
return FeatureTwoSelector.FeatureTwoSelector().getCorrectAnswer(req.get("result").get("parameters"))
elif action == "about":
return ResponseSelector.ResponseSelector().about()
else:
return {}
if __name__ == '__main__':
port = int(os.getenv('PORT', 5000))
# print "Starting app on port %d" % port
app.run(debug=True, port=port, host='0.0.0.0')