Skip to content

Instantly share code, notes, and snippets.

@wmantly
Created February 17, 2016 18:32
Show Gist options
  • Select an option

  • Save wmantly/594fcfcb23197ed87a78 to your computer and use it in GitHub Desktop.

Select an option

Save wmantly/594fcfcb23197ed87a78 to your computer and use it in GitHub Desktop.
var SlackBot = require('slackbots');
var todos = {} // object to hold the users lits
var userList = [];
var commands = {
echo: function(data){
return bot.postMessage(data.channel, data.payload, params)
},
add: function(data){
if(!todos[data.channel]) todos[data.channel] = [];
todos[data.channel].push(data.payload);
var message = "Added *" + data.payload + "* to your list";
return bot.postMessage(data.channel, message, params);
},
show: function(data){
if(!todos[data.channel]) bot.postMessage(data.channel, '*Empyt*', params)
var message = '';
todos[data.channel].forEach(function(value, i){
message += i + ': '+ value + '\n';
});
return bot.postMessage(data.channel, message, params);
},
help: function(data, message){
var message = message || '';
message += 'usage `@todo: [command] [payload]`\n'
Object.keys(this).forEach(function(value, i){
message += i + ': '+ value + '\n';
});
return bot.postMessage(data.channel, message, params);
}
}
// create a bot
var token = 'xoxb-21818585331-9rq3yEmJmu1T3WNuvP15lzFn'; // hide me somewhere!
var bot = new SlackBot({
token: token, // Add a bot https://my.slack.com/services/new/bot and put the token
name: 'Todo'
});
var params = {
icon_emoji: ':page_with_curl:'
};
bot.on('start', function() {
bot.postMessageToUser('wmantly', 'list time!', params);
userList = bot.getUsers()._value.members
userList.search = function( key, value ){
for ( var index = 0; index < this.length; ++index ) {
if( this[index][key] === value ){
return this[index];
}
}
return -1;
};
});
bot.on('message', function(data) {
// !data.subtype prevents message loop
if( !data.subtype && data.text ){
if(data.text.match(/^<@U0E3MNZTP>:/) || data.channel.match(/^D/)){
// parse the user id, command and payload out of the user's message
var parsed = data.text.replace('<@U0E3MNZTP>:', '').trim();
data.parsed = /(.\w+)\s?(.*)$/i.exec(parsed);
data.command = data.parsed[1];
data.payload = data.parsed[2];
if(commands.hasOwnProperty(data.command)){
commands[data.command](data);
}else{
commands['help'](data, '*Command not found.*\n');
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment