Add bot funcitonality and problems

This commit is contained in:
Christoffer Müller Madsen 2016-03-21 17:23:26 +01:00
parent 2f3c67b5f6
commit 036e46378b
2 changed files with 95 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules/
data/

93
np-bot.js Normal file
View File

@ -0,0 +1,93 @@
var Botkit = require('botkit');
var controller = Botkit.slackbot({
json_file_store: process.argv[3]
});
var bot = controller.spawn({
token: process.argv[2]
})
var problems = [
{"title": "Travelling salesman problem", "wiki_title": "Travelling_salesman_problem"},
{"title": "Knapsack problem", "wiki_title": "Knapsack_problem"},
{"title": "Bin packing problem", "wiki_title": "Bin_packing_problem"},
{"title": "Partition problem", "wiki_title": "Parition_problem"},
{"title": "Longest path problem", "wiki_title": "Longest_path_problem"},
{"title": "Longest common subsequence problem"},
{"title": "Art gallery problem"},
{"title": "Route inspection problem"},
{"title": "Open-shop scheduling problem"},
{"title": "Calculated minimum spanning tree"}
]
var last_index = -1;
bot.startRTM(function(err,bot,payload) {
if (err) {
throw new Error('Could not connect to Slack');
}
});
function createData(id) {
return {id: id, bot_calls: 0};
};
function incrementKey(id, key, amount, scope) {
controller.storage[scope].get(id, function(err,data) {
if (!data) {
console.log("Initializing " + scope + " data");
data = createData(id);
}
console.log("Current " + scope + " data:",data);
console.log("Incrementing mentions to " + (data[key] + amount));
data[key] = data[key] + amount
controller.storage[scope].save(data);
});
}
controller.hears(["np"],["direct_message","direct_mention","mention","ambient"],function(bot,message) {
console.log(message.user + " mentioned \"np\"");
// Increment the logged amount of mentions
incrementKey(message.user, "bot_calls", 1, "users");
incrementKey(message.channel, "bot_calls", 1, "channels");
incrementKey(message.team, "bot_calls", 1, "teams");
// Decide what to do
if (Math.random() < 0.9) {
var index = Math.floor(Math.random() * problems.length);
var base_url = "https://en.wikipedia.org/wiki/";
var problem, title, wiki_title;
// Ensure that the same problem won't get posted twice
while (index == last_index && problems.length > 1) {
index = Math.floor(Math.random() * problems.length);
}
// Find the problem metadata
problem = problems[index];
title = problem.title;
wiki_title = title.replace(/ /g,"_");
// Reply with the chosen problem
bot.reply(message,"Have you heard about the _" + title + "_? That is an NP-complete problem!\nSee " + base_url + wiki_title + " for more info!");
last_index = index;
} else {
bot.say(
{
text: '<@U0NEAMJS3|jonbot>, what do you think about NP-complete problems?',
channel: message.channel
}
);
}
// React with appropriate emoji
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: "mount_fuji",
},function(err, res) {
if (err) {
bot.botkit.log('Failed to add reaction emoji');
}
});
});