-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.js
32 lines (27 loc) · 1.22 KB
/
weather.js
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
var sys = require('sys');
var http = require('http');
var util = require('util');
function addWeatherAnswerer(bot, properties, wundergroundApiKey) {
bot.addCommandListener("!weather [city], [state]", /!weather (.*)/, "weather for state / city", function(stateCity) {
var match = stateCity.match(/([A-Za-z ]*), ([A-Za-z]{2})/);
if (match) {
var state = match[2];
var city = match[1];
var url = 'http://api.wunderground.com/api/' + wundergroundApiKey + '/conditions/q/' +
state + '/' + city + '.json';
http.get(url, function(response) {
var data = "";
response.setEncoding('utf8');
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
var weather = JSON.parse(data);
bot.say(weather.current_observation.weather + " and " + weather.current_observation.temp_f +
" degrees, with wind " + weather.current_observation.wind_string);
});
});
}
});
}
module.exports = addWeatherAnswerer;