Last active
February 18, 2016 19:16
-
-
Save fordprefect480/424ed87dddb98a2c6501 to your computer and use it in GitHub Desktop.
A slackbot which retrieves Star Wars articles given a search term.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
______ ______ ______ __ __ __ ______ | |
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\ | |
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/ | |
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\ | |
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/ | |
This is a simple Slack bot built with Botkit. The bot is called 'holocron' and will go get a Star Wars article (from Wookiepedia) | |
if you mention or direct mention the bot with a search term. | |
Read all about Botkit here: | |
-> http://howdy.ai/botkit | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
var http = require('http') | |
var S = require('string'); | |
var Botkit = require('./lib/Botkit.js') | |
var os = require('os'); | |
var controller = Botkit.slackbot({ | |
debug: false, | |
}); | |
var bot = controller.spawn( | |
{ | |
token:<<Add your token here>> | |
} | |
).startRTM(); | |
controller.hears(['hello','hi'],'direct_message,direct_mention,mention',function(bot,message) { | |
bot.api.reactions.add({ | |
timestamp: message.ts, | |
channel: message.channel, | |
name: 'robot_face', | |
},function(err,res) { | |
if (err) { | |
bot.botkit.log("Failed to add emoji reaction :(",err); | |
} | |
}); | |
controller.storage.users.get(message.user,function(err,user) { | |
if (user && user.name) { | |
bot.reply(message,"Hello " + user.name+"!!"); | |
} else { | |
bot.reply(message,"Hello."); | |
} | |
}); | |
}) | |
controller.hears(['shutdown'],'direct_message,direct_mention,mention',function(bot,message) { | |
bot.startConversation(message,function(err,convo) { | |
convo.ask("Are you sure you want me to shutdown?",[ | |
{ | |
pattern: bot.utterances.yes, | |
callback: function(response,convo) { | |
convo.say("Bye!"); | |
convo.next(); | |
setTimeout(function() { | |
process.exit(); | |
},3000); | |
} | |
}, | |
{ | |
pattern: bot.utterances.no, | |
default:true, | |
callback: function(response,convo) { | |
convo.say("*Phew!*"); | |
convo.next(); | |
} | |
} | |
]) | |
}) | |
}) | |
controller.hears(['uptime','identify yourself','who are you','what is your name'],'direct_message,direct_mention,mention',function(bot,message) { | |
var hostname = os.hostname(); | |
var uptime = formatUptime(process.uptime()); | |
bot.reply(message,':robot_face: I am a bot named <@' + bot.identity.name +'>. I have been running for ' + uptime + ' on ' + hostname + "."); | |
}) | |
function formatUptime(uptime) { | |
var unit = 'second'; | |
if (uptime > 60) { | |
uptime = uptime / 60; | |
unit = 'minute'; | |
} | |
if (uptime > 60) { | |
uptime = uptime / 60; | |
unit = 'hour'; | |
} | |
if (uptime != 1) { | |
unit = unit +'s'; | |
} | |
uptime = uptime + ' ' + unit; | |
return uptime; | |
} | |
/////////////////////////////////////////////////// | |
controller.hears([''],'direct_message,direct_mention',function(bot,message) { | |
var searchTerm = message.text; | |
http.get('http://starwars.wikia.com/api/v1/Search/List?query='+ searchTerm +'&limit=1&minArticleQuality=10&batch=1&namespaces=0%2C14', function(res) { | |
if (res.statusCode !== 200) { | |
bot.reply(message, "Sorry, couldn't find anything about that. :confused:"); | |
} else { | |
var body = ""; | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end',function() { | |
var firstResult = JSON.parse(body).items[0]; | |
if (firstResult !== null) { | |
if (firstResult.quality > 75) { | |
bot.reply(message, "Got it! :grin:\r\n" + firstResult.url); | |
} | |
else { | |
bot.reply(message, "Is this what you're after? " + firstResult.url + ' :thinking_face:'); | |
} | |
} | |
else { | |
bot.reply(message, "Sorry, couldn't find anything about " + searchTerm + ' :confused:'); | |
} | |
}); | |
} | |
}).on('error', function(e) { | |
console.log("error: ", e); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment