Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Weather Bot for Slack
// please leave this notice intact, otherwise do as you please :)
// by hello@franciskim.co
// get more info at http://franciskim.co/2015/07/22/how-to-create-a-weather-bot-for-slack-chat/
var express = require('express');
var app = express();
var http = require('http');
var Slackhook = require('slackhook');
var slack = new Slackhook({
domain: 'yoursubdomain',
token: 'abcdedfhijklmnopqrstuvwxyz'
});
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// set your channel here
var channel = '#general';
app.post('/weather', function(req, res) {
// get search query from Slack message
var search = req.body.text.replace(req.body.trigger_word, '').trim();
// prepare Yahoo Weather API URL (get WOEID)
var locationQuery = escape("select item from weather.forecast where woeid in (select woeid from geo.places where text='" + search + "') and u='c'"),
locationUrl = "http://query.yahooapis.com/v1/public/yql?q=" + locationQuery + "&format=json";
http.get(locationUrl, function(res) {
res.setEncoding('binary');
var data = "";
res.on('data', function(chunk) {
return data += chunk;
});
res.on('end', function() {
result = JSON.parse(data);
// parse the data and send to the channel
// sometimes Yahoo returns an array and sometimes doesn't
// depending on how many results are available for the search term
// this code is hardcoded to send the first result if it's an array
if (result.query.results.channel instanceof Array) {
slack.send({
text: result.query.results.channel[0].item.title + '\n`Current` ' + result.query.results.channel[0].item.condition.temp + ' degrees, ' + result.query.results.channel[0].item.condition.text + '\n`' + result.query.results.channel[0].item.forecast[0].day + '` High: ' + result.query.results.channel[0].item.forecast[0].high + ' Low: ' + result.query.results.channel[0].item.forecast[0].low + ', ' + result.query.results.channel[0].item.forecast[0].text + '\n`' + result.query.results.channel[0].item.forecast[1].day + '` High: ' + result.query.results.channel[0].item.forecast[1].high + ' Low: ' + result.query.results.channel[0].item.forecast[1].low + ', ' + result.query.results.channel[0].item.forecast[1].text,
channel: channel
});
} else {
console.log(result.query.results.channel);
slack.send({
text: result.query.results.channel.item.title + '\n`Current` ' + result.query.results.channel.item.condition.temp + ' degrees, ' + result.query.results.channel.item.condition.text + '\n`' + result.query.results.channel.item.forecast[0].day + '` High: ' + result.query.results.channel.item.forecast[0].high + ' Low: ' + result.query.results.channel.item.forecast[0].low + ', ' + result.query.results.channel.item.forecast[0].text + '\n`' + result.query.results.channel.item.forecast[1].day + '` High: ' + result.query.results.channel.item.forecast[1].high + ' Low: ' + result.query.results.channel.item.forecast[1].low + ', ' + result.query.results.channel.item.forecast[1].text,
channel: channel
});
}
});
});
});
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Weather x Slack app listening at http://%s:%s', host, port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.