-
Notifications
You must be signed in to change notification settings - Fork 4
/
webhook.py
64 lines (40 loc) · 1.33 KB
/
webhook.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
import json
import os
import requests
from flask import Flask
from flask import request
from flask import make_response
# Flask app should start in global layout
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
req = request.get_json(silent=True, force=True)
print(json.dumps(req, indent=4))
res = makeResponse(req)
res = json.dumps(res, indent=4)
# print(res)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
def makeResponse(req):
result = req.get("result")
parameters = result.get("parameters")
city = parameters.get("geo-city")
date = parameters.get("date")
r=requests.get('https://api.openweathermap.org/data/2.5/forecast?q='+city+'&appid=06f070197b1f60e55231f8c46658d077')
json_object = r.json()
weather=json_object['list']
for i in range(0,30):
if date in weather[i]['dt_txt']:
condition= weather[i]['weather'][0]['description']
break
speech = "The forecast for"+city+"for "+date+" is "+condition
return {
"speech": speech,
"displayText": speech,
"source": "apiai-weather-webhook"
}
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')