Skip to content

Instantly share code, notes, and snippets.

@cmaneu
Last active January 19, 2024 19:59
Show Gist options
  • Select an option

  • Save cmaneu/932e4628712ec9bbcaea to your computer and use it in GitHub Desktop.

Select an option

Save cmaneu/932e4628712ec9bbcaea to your computer and use it in GitHub Desktop.
Slack slackbot send weather forecast
var request = require("request");
// The Cities IDs can be found on openweathermap.org (make a search, and look the URI)
var cities = [2988507, 5391959];
var slackBotUri = ""; // TODO: Complete
request("http://api.openweathermap.org/data/2.5/group?id="+cities.join(',')+"&units=metric ", function(error, response, body) {
// Maybe we can handle this differently/better ?
if(error != null)
return;
var text = "Hello team, here is the weather forecast for today: \n";
var weatherForecasts = JSON.parse(body);
for (var i = weatherForecasts.list.length - 1; i >= 0; i--) {
var currentCity= weatherForecasts.list[i];
text += "*"+currentCity.name+"*: ";
text += ":" + currentCity.weather[0].icon + ": ";
text += currentCity.weather[0].main + ", " + currentCity.weather[0].description + ". ";
text += "Temp: " + currentCity.main.temp + "°c. "
text += "\n";
};
console.log(text);
request.post({
url: slackBotUri,
body: text
}, function(error, response, body){
console.log(body);
});
});
@cmaneu

cmaneu commented Sep 17, 2014

Copy link
Copy Markdown
Author

If you want to display the forecast icons, you must add custom emoji to your slack instance. You can download/see all used icons here : http://openweathermap.org/weather-conditions

@justinr

justinr commented Dec 2, 2014

Copy link
Copy Markdown

How do I implement this in Slack? Do I use webhooks for this?

@metasim

metasim commented Mar 5, 2015

Copy link
Copy Markdown

Ditto... how do you install this in Slack?

@seanwalsh

Copy link
Copy Markdown

He has a variable called slackBotUri. Set that to the URI that you have setup as a webhook integration.

@khoi-thinh

Copy link
Copy Markdown

Could someone tell me exactly how to do this?
Download this file, install Node.js, input webhook url into slackBotUri, run with the command: node slackWeather.js, is that right?
If so, it didn't work for me.
module.js:340
throw err;
^
Error: Cannot find module 'request'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (/root/slackWeather.js:1:77)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)

@djui

djui commented Apr 28, 2016 •

Copy link
Copy Markdown

@thinhduckhoi npm install request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment