diff --git a/DOCS.md b/DOCS.md index 3758ba4..514236d 100644 --- a/DOCS.md +++ b/DOCS.md @@ -172,12 +172,13 @@ This posts weather information of a given location. __Usage__ -`@weather ` +`@weather > ` __Arguments__ * `zipcode`: A five digit US zipcode. * `location name`: A letter and space only location name, or city name and state separated by a comma and a space. +* `degree type`: A degree type to display the weather, the parameter can be 'C' for Celsius or 'F' for Fahrenheit. The default type is Fahrenheit. __Example__ @@ -205,3 +206,14 @@ Feels like 73°. Humidity 94%. 08-02 | 71°/76° :umbrella: 80% 08-03 | 72°/78° :umbrella: 60% +>`@weather 78701 C` + +>37°C +Austin, TX +Sunny :sunny: +Feels like 37°. Humidity 35%. +07-31 | 24°/35° +08-01 | 24°/36° :umbrella: 0% +08-02 | 26°/37° :umbrella: 0% +08-03 | 26°/38° :umbrella: 0% +08-04 | 26°/37° :umbrella: 30% \ No newline at end of file diff --git a/public/javascript/bot.js b/public/javascript/bot.js index f01fc06..c30693b 100644 --- a/public/javascript/bot.js +++ b/public/javascript/bot.js @@ -99,7 +99,7 @@ function messageHandler(event) { handlerFunctions['getPokemon'](userAPI, event.threadID, event.senderID, message); } else if ((/^@stock .+$/).test(message)) { handlerFunctions['getStock'](userAPI, event.threadID, message); - } else if ((/^@weather ([0-9]{5}|([a-zA-Z ]+(, )?[a-zA-Z ]+))$/).test(message)) { + } else if ((/^@weather ([0-9]{5} [CF]|([a-zA-Z ]+(, )?[a-zA-Z ]+))$/).test(message)) { handlerFunctions['getWeather'](userAPI, event.threadID, message); } else if ((/^@meme$/).test(message)) { handlerFunctions['rickroll'](userAPI, event.threadID); diff --git a/public/javascript/getWeather.js b/public/javascript/getWeather.js index 4e08b46..b4494b1 100644 --- a/public/javascript/getWeather.js +++ b/public/javascript/getWeather.js @@ -8,9 +8,22 @@ var weather = require("weather-js"); module.exports = function getWeather(api, threadID, body) { - const locale = body.substring('@weather '.length); - console.log('Fetching weather for ' + locale + '...'); - weather.find({ search: locale, degreeType: 'F' }, function(err, result) { + var locale = ''; + var currentDType = 'F'; + const infoArray = body.split(' '); + for (var i = 1; i < infoArray.length; i++) { + if (i == 1) { + locale += infoArray[i]; + } else { + if (i != infoArray.length - 1 || (i == infoArray.length - 1 && infoArray[infoArray.length - 1] != 'F' && infoArray[infoArray.length - 1] != 'C')) { + locale += ' ' + infoArray[i]; + } else { + currentDType = infoArray[infoArray.length - 1]; + } + } + } + console.log('Fetching weather for ' + locale + ' with degreeType ' + currentDType + '...'); + weather.find({ search: locale, degreeType: currentDType }, function(err, result) { if (err) { api.sendMessage(err, threadID); console.error(err); @@ -65,9 +78,9 @@ module.exports = function getWeather(api, threadID, body) { } else { emoji = ''; } - + var displayType = currentDType === 'F' ? '\xB0F\n' : '\xB0C\n'; let message = - data.current.temperature + '\xB0F\n' + data.location.name + '\n' + data.current.skytext + emoji + '\n' + + data.current.temperature + displayType + data.location.name + '\n' + data.current.skytext + emoji + '\n' + 'Feels like ' + data.current.feelslike + '\xB0. Humidity ' + data.current.humidity + '%.\n'; // Concatenate forecast to message