forked from BeepBoopHQ/starter-node-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessageCreator.js
More file actions
81 lines (74 loc) · 3.81 KB
/
messageCreator.js
File metadata and controls
81 lines (74 loc) · 3.81 KB
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
81
var messageCreator = function () {};
messageCreator.prototype.getWeather = function(bot,message,controller,inputLocation){
var request = require('request');
var rawLocation = inputLocation;
var location = rawLocation.split(',');
var url = 'http://api.wunderground.com/api/e6d58e1b342bc28a/geolookup/conditions/q/' + location[1] + '/' + location[0] + '.json';
request(url, function(error, response, data){
if (!error && response.statusCode == 200){
var parsedData = JSON.parse(data);
parseWeatherMessage(bot,message,controller,location,parsedData);
}
});
}
messageCreator.prototype.getForecast = function(bot,message,controller,inputLocation){
var request = require('request');
var rawLocation = inputLocation;
var location = rawLocation.split(',');
var url = 'http://api.wunderground.com/api/e6d58e1b342bc28a/forecast/q/' + location[1] + '/' + location[0] + '.json';
request(url, function(error, response, data){
if (!error && response.statusCode == 200){
var parsedData = JSON.parse(data);
parseForecastMessage(bot,message,controller,location,parsedData);
}
});
}
var parseWeatherMessage = function(bot,message,controller,location,parsedData){
if(parsedData.current_observation != null){
controller.storage.users.get(message.user, function(err, user) {
bot.reply(message, '```' + location[0].toUpperCase() +
'\nTemperature: ' + parsedData.current_observation.temp_f + '° F' +
'\nHumidity: ' + parsedData.current_observation.relative_humidity +
'\nDewpoint: ' + parsedData.current_observation.dewpoint_f + '° F' +
'\nWind: ' + parsedData.current_observation.wind_mph + ' mph' +
'\nCurrent Conditions: ' + parsedData.current_observation.weather + '```'
);
});
}
else{
controller.storage.users.get(message.user, function(err, user) {
bot.reply(message, 'Sorry, I\'m not finding any data for ' + rawLocation + ' right now :whew:');
});
}
}
var parseForecastMessage = function(bot,message,controller,location,parsedData){
if(parsedData.forecast != null){
if(location[2] == null){
controller.storage.users.get(message.user, function(err, user) {
bot.reply(message, 'Today\'s ' + location[0].toUpperCase() + ' forecast: \n```' +
'Conditions: ' + parsedData.forecast.simpleforecast.forecastday[0].conditions +
'\nHigh: ' + parsedData.forecast.simpleforecast.forecastday[0].high.fahrenheit + '° F' +
'\nLow: ' + parsedData.forecast.simpleforecast.forecastday[0].low.fahrenheit + '° F' +
'\nHumidity ' + parsedData.forecast.simpleforecast.forecastday[0].avehumidity + '%' +
'\nWind: ' + parsedData.forecast.simpleforecast.forecastday[0].avewind.mph + ' mph' +
'\nChance of precipitation: ' + parsedData.forecast.simpleforecast.forecastday[0].pop +
'\nInches of snow: ' + parsedData.forecast.simpleforecast.forecastday[0].snow_allday.in + '```'
);
});
}
else{
controller.storage.users.get(message.user, function(err, user) {
bot.reply(message, location[0].toUpperCase() + ' forecast for ' + location[2] + ' days from now: \n```' +
'Conditions: ' + parsedData.forecast.simpleforecast.forecastday[location[2]].conditions +
'\nHigh: ' + parsedData.forecast.simpleforecast.forecastday[location[2]].high.fahrenheit + '° F' +
'\nLow: ' + parsedData.forecast.simpleforecast.forecastday[location[2]].low.fahrenheit + '° F' +
'\nHumidity: ' + parsedData.forecast.simpleforecast.forecastday[location[2]].avehumidity + '%' +
'\nWind: ' + parsedData.forecast.simpleforecast.forecastday[location[2]].avewind.mph + ' mph' +
'\nChance of precipitation: ' + parsedData.forecast.simpleforecast.forecastday[location[2]].pop +
'\nInches of snow: ' + parsedData.forecast.simpleforecast.forecastday[location[2]].snow_allday.in + '```'
);
});
}
}
}
module.exports = new messageCreator();