Skip to content

Instantly share code, notes, and snippets.

@abrudtkuhl
Created March 19, 2016 19:27
Show Gist options
  • Save abrudtkuhl/4ae1228b232bad49405b to your computer and use it in GitHub Desktop.
Save abrudtkuhl/4ae1228b232bad49405b to your computer and use it in GitHub Desktop.
Basic Slack Bot Node.js
'use strict';
var messages = [
'Giddyup',
'Isosceles. You know, I love the name Isosceles. If I had a kid, I would name him Isosceles. Isosceles Kramer.',
'You know what you are? You’re an anti-dentite! It starts with a few jokes and slurs… ‘HEY DENTY!’ Then you will say that dentists should have their own schools!',
'Have you ever met a proctologist? They usually have a very good sense of humor. You meet a proctologist at a party, don’t walk away. Plant yourself there because you will hear the funniest stories you’ve ever heard.',
'What do you think Junior? You think these hands – they’ve been soaking in Ivory Liquid?',
'http://i.giphy.com/aMh59aKR8vjdC.gif'
];
var Bot = require('slackbots');
// create a bot
var settings = {
token: 'XXXXX',
name: 'kramerbot'
};
var bot = new Bot(settings);
bot.on( 'message', function(message) {
var me = this.users.filter(function (user) {
return user.name === 'kramerbot';
})[0];
if( message.type === 'message' && Boolean(message.text) ) {
if( message.text.toLowerCase().indexOf('kramer') > -1
|| message.text.toLowerCase().indexOf(this.name) > -1
|| message.text.indexOf(me.id) > -1) {
if( message.user !== me.id ) {
var params = {
link_names: 1,
as_user: true
};
var fromUser = this.users.filter(function(user) {
return user.id === message.user;
})[0]['name'];
var channel = this.channels.filter(function (item) {
return item.id === message.channel;
})[0];
var randMessage = messages[Math.floor(Math.random() * messages.length)];
bot.postMessageToChannel(channel.name, '@' + fromUser + ' ' + randMessage, params);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment