Skip to content

Instantly share code, notes, and snippets.

@ajfisher
Last active June 27, 2016 01:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajfisher/2361c4fd200996a6a41272cad54b2fd0 to your computer and use it in GitHub Desktop.
Save ajfisher/2361c4fd200996a6a41272cad54b2fd0 to your computer and use it in GitHub Desktop.
A quick little slack bot to report of brexit results

This is a little slack bot I made which is used to get results for the EU Referendum in the UK

Set up

npm install cheerio botkit request

Log into slack, create a new bot and then get the token for it.

Drop the token into the brexitbot.js file where it says "PUT YOUR TOKEN HERE"

Then

node brexitbot.js

Interacting

Invite the bot to your channel and then you can ask it questions like "@brexitbot who is winning?" or "@brexitbot what are the latest results" etc and it will give you a variety of responses fresh from the BBC referendum results site.

var request = require("request");
var cheerio = require("cheerio");
var url = "http://www.bbc.com/news/politics/eu_referendum/results";
var data = {};
var err = {};
var startup = true;
function retrieve_latest_results() {
request(url, (err, response, html) => {
if (err) {
err.response = "err";
err.msg = err;
} else {
err = {};
var $ = cheerio.load(html);
data.leave = parseInt($("div[data-name='leaveVoteCount']").text().replace(/\,/g, ''));
data.remain = parseInt($("div[data-name='remainVoteCount']").text().replace(/\,/g,''));
data.total_counted = data.leave + data.remain;
data.leave_pct = data.leave / data.total_counted * 100;
data.remain_pct = data.remain / data.total_counted * 100;
data.total_electorate = 46503464;
data.counted_pct = data.total_counted / data.total_electorate * 100;
data.winning = ( data.leave > data.remain ) ? "leave" : "remain";
data.winning_pct = ( data.leave > data.remain) ? data.leave_pct : data.remain_pct;
data.winning_val = ( data.leave > data.remain) ? data.leave : data.remain;
data.losing_val = ( data.leave < data.remain) ? data.leave : data.remain;
data.lastRetrieved = Date.now();
startup = false;
}
});
}
if (startup) {
retrieve_latest_results();
}
setInterval(retrieve_latest_results, 10000);
module.exports = {
data: data,
err: err
};
var os = require('os');
var Botkit = require('botkit');
var controller = Botkit.slackbot();
var brexit = require('./brexit.js');
var bot = controller.spawn({
token: "token"
});
bot.startRTM(function(err,bot,payload) {
if (err) {
throw new Error('Could not connect to Slack');
}
});
controller.hears(['brexit', 'latest results', 'referendum', 'results'],
'direct_message,direct_mention,mention',
function(bot, message) {
var replies = [
"Right now, Leave :flag-gb: are on " + brexit.data.leave_pct.toFixed(2) +
"% and Remain :flag-eu: are on " +
brexit.data.remain_pct.toFixed(2) + "%",
"According to my latest data, the " + (brexit.data.winning == "leave" ? "leavers" : "remainers") +
" are winning with " + brexit.data.winning_val + " votes.",
"The " + (brexit.data.winning == "leave" ? "leavers" : "remainers") +
" are currently ahead by " +
(brexit.data.winning_val - brexit.data.losing_val) + " votes.",
"Results are in. Leave :flag-gb: have " + brexit.data.leave + " votes, " +
"Remain :flag-eu: have " + brexit.data.remain + " votes. Which means the " +
brexit.data.winning + " camp are winning with " +
brexit.data.winning_pct.toFixed(2) + "% of the vote after " +
brexit.data.counted_pct.toFixed(2) + "% of votes have been counted. ",
];
var response = Math.floor(Math.random() * replies.length);
bot.reply(message, replies[response]);
}
);
controller.hears(['winning'],
'direct_message,direct_mention',
function(bot, message) {
var replies = [
"Right now, the " + brexit.data.winning + " side are winning with " + brexit.data.winning_pct.toFixed(2) + "% share of the vote",
"According to the latest data, the " + (brexit.data.winning == "leave" ? "leavers" : "remainers") + " hold " + brexit.data.winning_pct.toFixed(2) + "% of the vote after " + brexit.data.counted_pct.toFixed(2) + "% of the vote is counted",
];
var response = Math.floor(Math.random() * replies.length);
bot.reply(message, replies[response]);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment