forked from kkpoon/hubot-weather-underground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.coffee
80 lines (70 loc) · 2.6 KB
/
weather.coffee
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
# Description
# a hubot weather reporter
#
# Configuration:
# HUBOT_WEATHER_WUNDERGROUND_KEY
#
# Commands:
# hubot weather of PLACE <show the weather of a place>
# hubot what is PLACE exactly called? <find the exact name of a place>
#
# Author:
# K.K. POON <[email protected]>
WUNDERGROUND_KEY = process.env.HUBOT_WEATHER_WUNDERGROUND_KEY
WUNDERGROUND_UNITS = process.env.HUBOT_WEATHER_WUNDERGROUND_UNITS
WUNDERGROUND_URL = "http://api.wunderground.com/api/#{WUNDERGROUND_KEY}"
module.exports = (robot) ->
robot.respond /weather (.*)/i, (msg) ->
place = msg.match[1]
msg.http("#{WUNDERGROUND_URL}/conditions/q/#{place}.json")
.get() (err, res, body) ->
if err
msg.send err
robot.emit 'error', err
return
try
data = JSON.parse(body)
obs = data.current_observation
switch WUNDERGROUND_UNITS
when "c"
msg.send "The current weather condition of " +
"#{obs.display_location.full} is #{obs.weather}:\n" +
"#{obs.observation_time}, " +
"Temperature is #{obs.temp_c}°C " +
"(feels like #{obs.feelslike_c}°C), " +
"Humidity #{obs.relative_humidity}, " +
"Pressure #{obs.pressure_mb}hPa, " +
"Wind #{obs.wind_string}, " +
"UV #{obs.UV}\n" +
"More information: #{obs.ob_url}"
when "f"
msg.send "The current weather condition of " +
"#{obs.display_location.full} is #{obs.weather}:\n" +
"#{obs.observation_time}, " +
"Temperature is #{obs.temp_f}°F " +
"(feels like #{obs.feelslike_f}°F), " +
"Humidity #{obs.relative_humidity}, " +
"Pressure #{obs.pressure_mb}hPa, " +
"Wind #{obs.wind_string}, " +
"UV #{obs.UV}\n" +
"More information: #{obs.ob_url}"
else msg.send "Please define WUNDERGROUND_UNITS"
catch err
msg.send err
robot.emit 'error', err
robot.respond /what is (.*) (exactly|exact) (called|named|call|name)[\?]?/i, (msg) ->
place = msg.match[1]
msg.http("http://autocomplete.wunderground.com/aq?query=#{place}")
.get() (err, res, body) ->
if err
msg.send err
robot.emit 'error', err
return
try
data = JSON.parse(body)
results = data.RESULTS
msg.send "Here are the possibilities:\n" +
("- " + r.name for r in results).join "\n"
catch err
msg.send err
robot.emit 'error', err