Skip to content

Instantly share code, notes, and snippets.

@abinashmohanty
Created November 25, 2016 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abinashmohanty/bece55d640b65a0560d7fb8748bd3eef to your computer and use it in GitHub Desktop.
Save abinashmohanty/bece55d640b65a0560d7fb8748bd3eef to your computer and use it in GitHub Desktop.
Doc for Slack bot conversation in javascript - compilation from multiple sources
// 01
// Listening
// EVENTS -- controller.on
/*
in events, one can use "bot_channel_join" "direct_mention" or "direct_message"
*/
// event template starts
controller.on("bot_channel_join", function(bot, message) {
// do stuff
})
// ends
// event example - long version starts
controller.on('direct_mention', function(bot, message) {
bot.reply(message, "Hey there! Thanks for looking at my resume. To get to the good bits there are a few command you'll need. If you want to hear about my work experience type 'experience'. If you want to hear about my education type 'eduction'. If you want my contact information type 'contact'. If you want a list of my past awards and achievements type 'achievements'");
});
// ends
/*
the above function's description is here. Botkit uses “on” to respond to situations that don’t involve messages to the bot. In this case we tell it to respond to “direct_mention” with a block of text. This means that whenever someone calls it by name e.g. “@resbot” it will reply with this message. You can see that botkit has a helpful method called “reply” built in. It lets you open up some brackets after “reply”, tell it you’re going to be writing a message by using “message” and then simply write out whatever that message is going to be.
So to translate this block of code into plain english: on a direct mention of the bots name “@resbot” it will reply with the message we have defined for it e.g “Hey there! …”.
*/
// 02
// Listening
// KEYWORDS -- controller.hears
/*
unlike events, The keyword to listen for, e.g. "hello"
The context in which the keyword was mentioned. For example, if it was a direct_message, a direct_mention, or ambient (when the bot is not directly mentioned, but is active in the channel)
*/
// keyword template starts
controller.hears(array_of_keywords, array_of_contexts, function(bot, message) {
// do stuff
})
// ends
//For @piku bot, whenever someone mentions “piku", the bot was alerted - like...
controller.hears(["piku"], ["ambient"], function(bot, message) {
// do stuff
})
//ends
// another example of controller.hears
controller.hears(['experience', 'previous employment'],['ambient'], function(bot,message){
return bot.reply(message, "2010 - experience. 2011 - experience. 2012 - experience". 2013 - chocolate eating champion of the world. 2014 - Transformed into Lionel Messi for a year while on a spirit quest. 2015 - Ate lots of toast. 2016 - wrote JavaScript tutorials");
});
/*
You can see that this block follows a similar pattern to the last. It replaces “on” with “hears” in order to listen for specific phrases. You can pass it any number of phrases to look out for e.g. “experience” and “previous employment” in square brackets (this is called an array and is basically a way for computers to understand lists). You could add something nonsensical if you wanted like “eggs are delicious” and it would also listen for that and respond with the same method. It also uses “ambient” instead of “direct message”. This just lets Botkit know that you want your user to be able to type “experience” rather that “@resbot experience” for it to display this message.
*/
// 03
// RESPONDING
// to events
/*
When an event happens, we want the bot to do something in response. A bot can send a single reply using bot.reply. This function accepts two arguments - the message the bot is replying to, and the reply text.
let's say for a bote name @piku, whenever someone mentions "piku', the bot responds to them.
*/
controller.hears(["piku"], ["ambient"], function(bot, message) {
var userID = message.user // the ID of the user that mentioned "khaled"
var user = "<@"+userID+">" // wrap around like this to create an @ mention of the user
var reply = user+" you spoke my name?"; // create reply
bot.reply(message, reply); // send reply
})
// SOURCES
// Making KhaledBot - An Introduction to Slack Bots
// https://bitsofco.de/creating-khaledbot-an-introduction-to-slack-bots/
// “Make your resumé into a slackbot with less than 30 lines of JavaScript”
// https://medium.com/@james.winestock/make-your-resume-into-a-slackbot-with-less-than-30-lines-of-javascript-f1cde261f10e#.t6xo2yl47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment