-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (43 loc) · 1.44 KB
/
app.py
File metadata and controls
54 lines (43 loc) · 1.44 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
51
52
53
54
import json
import os
from flask import Flask
from flask import request
from flask import make_response
app = Flask(__name__)
@app.route('/webhook',methods=['POST'])
def webhook():
req = request.get_json(silent=True, force=True)
print("Request:")
print(json.dumps(req, indent=4))
res = createResponse(req)
return res
def createResponse(req):
result = req.get("queryResult")
if result.get("action") == "input.unknown":
print ("Failed to recognize!!..passing to the next engine")
my_result = {
"fulfillmentText": "Fallback intent",
"source": "Demo-DialogFlow-Example"
}
res = json.dumps(my_result, indent=4)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
if result.get("action") != "animal_info":
return{}
parameters = result.get("parameters")
animal = parameters.get("AnimalEntity")
speech = 'Hello,received response from the backend application, this is a demo joke on {}'.format(animal)
print ("Voice output : "+speech)
my_result = {
"fulfillmentText": speech,
"source": "Demo-DialogFlow-Example"
}
res = json.dumps(my_result, indent=4)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
if __name__ == '__main__':
port = int(os.getenv('PORT', 5000))
print("Starting app on port %d" % port)
app.run(debug=False, port=port, host='127.0.0.1', threaded=True)