-
Notifications
You must be signed in to change notification settings - Fork 23
/
weather.py
61 lines (48 loc) · 2.08 KB
/
weather.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
from flask import Flask,request,make_response
import os,json
import pyowm
import os
app = Flask(__name__)
owmapikey=os.environ.get('OWMApiKey') #or provide your key here
owm = pyowm.OWM(owmapikey)
#geting and sending response to dialogflow
@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(res)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
#processing the request from dialogflow
def processRequest(req):
result = req.get("result")
parameters = result.get("parameters")
city = parameters.get("geo-city")
observation = owm.weather_at_place(city)
w = observation.get_weather()
latlon_res = observation.get_location()
lat=str(latlon_res.get_lat())
lon=str(latlon_res.get_lon())
wind_res=w.get_wind()
wind_speed=str(wind_res.get('speed'))
humidity=str(w.get_humidity())
celsius_result=w.get_temperature('celsius')
temp_min_celsius=str(celsius_result.get('temp_min'))
temp_max_celsius=str(celsius_result.get('temp_max'))
fahrenheit_result=w.get_temperature('fahrenheit')
temp_min_fahrenheit=str(fahrenheit_result.get('temp_min'))
temp_max_fahrenheit=str(fahrenheit_result.get('temp_max'))
speech = "Today the weather in " + city + ": \n" + "Temperature in Celsius:\nMax temp :"+temp_max_celsius+".\nMin Temp :"+temp_min_celsius+".\nTemperature in Fahrenheit:\nMax temp :"+temp_max_fahrenheit+".\nMin Temp :"+temp_min_fahrenheit+".\nHumidity :"+humidity+".\nWind Speed :"+wind_speed+"\nLatitude :"+lat+".\n Longitude :"+lon
return {
"speech": speech,
"displayText": speech,
"source": "dialogflow-weather-by-satheshrgs"
}
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')