-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.py
65 lines (52 loc) · 2 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
import os
import requests
from flask import Flask
from flask import request
from flask import make_response
app = Flask(__name__)
api_key = 'ed43736919fd647843e9723b61be0e50'
@app.route('/webhook', methods=['POST'])
def webhook():
req = request.get_data().decode('utf-8')
req = json.loads(req)
res = makeResponse(req)
res = json.dumps(res, indent=4)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
def makeResponse(req):
result = req.get("queryResult")
parameters = result.get("parameters")
city = parameters.get("city")
date = parameters.get("date")[:10]
url_ = f'http://api.openweathermap.org/data/2.5/forecast?q={city}&appid={api_key}'
r = requests.get(url_)
json_object = r.json()
weather = json_object["list"]
for i in range(0,30):
if date in weather[i]["dt_txt"]:
temperature = str(round(int(weather[i]["main"]["temp"]) - 273.15))
condition = weather[i]["weather"][0]["description"]
break
cond = ['clear sky', 'few clouds','overcast clouds',
'scattered clouds', 'broken clouds',
'shower rain', 'rain', 'thunderstorm',
'snow','mist', "light rain", "moderate rain",
"light snow", "snow"]
cond_tr = ['güneşli', 'az bulutlu','çok bulutlu',
'bulutlu', 'parçalı bulutlu', 'sağanak yağışlı',
'yağmurlu', 'gök gürültülü sağanak yağışlı', 'karlı',
'sisli', "az yağmurlu", "orta seviye yağmurlu",
"az karlı", "karlı"]
for i in range(len(cond)):
if condition == cond[i]:
condition = cond_tr[i]
speech = city + " şehrinde " + date + " tarihinde hava " + condition + " ve " + temperature + " derece."
return {
"fulfillmentText": speech
}
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")