forked from dialogflow/fulfillment-weather-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·121 lines (88 loc) · 3.13 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
from __future__ import print_function
from future.standard_library import install_aliases
install_aliases()
# noinspection PyCompatibility
from urllib.parse import urlencode
# noinspection PyCompatibility
from urllib.request import urlopen
import json
import os
from flask import Flask
from flask import request
from flask import make_response
from intents import appointments
from intents import login
from intents import sales
from intents import weather
from intents import targets
# Flask app should start in global layout
app = Flask(__name__)
class AuthError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
@app.route('/webhook', methods=['POST'])
def webhook():
req = request.get_json(silent=True, force=True)
print("Request:")
print(json.dumps(req, indent=4))
res = processRequest(req)
res = json.dumps(res, indent=4)
print("Response:")
print(res)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
def get_token(req):
try:
contexts = req.get("result").get("contexts")
for c in contexts:
if c['name'] == "token":
return c.get("parameters").get("token")
except (RuntimeError, TypeError, NameError):
raise AuthError("No session token")
def get_business_token(req):
try:
contexts = req.get("result").get("contexts")
for c in contexts:
if c['name'] == "token":
return c.get("parameters").get("business_token")
except (RuntimeError, TypeError, NameError):
raise AuthError("No session token")
def processRequest(req):
print("action:" + req.get("result").get("action"))
base_url = "https://staging.kitomba.com"
token = get_token(req)
business_token = get_business_token(req)
print(token)
print(business_token)
res = {} # handles non matching case
if req.get("result").get("action") == "login":
res = login.login(req)
if req.get("result").get("action") == "yahooWeatherForecast":
res = weather.doYahooWeatherForecast(req)
if req.get("result").get("action") == "appointments.first_visit":
print(req)
day = req.get("result").get("parameters").get("day")
print(day)
res = appointments.first_visit(base_url, token, business_token, day)
if req.get("result").get("action") == "appointments.today":
print(req)
day = req.get("result").get("parameters").get("day")
print(day)
res = appointments.today(base_url, token, business_token, day)
if req.get("result").get("action") == "sales.day":
print(req)
date = req.get("result").get("parameters").get("date")
print(date)
res = sales.today(base_url, token, business_token, date)
if req.get("result").get("action") == "targets":
print(req)
res = targets.today(base_url, token, business_token)
return res
if __name__ == '__main__':
port = int(os.getenv('PORT', 5000))
print("Starting app on port %d" % port)
app.run(debug=False, port=port, host='0.0.0.0')