Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
Last active October 20, 2015 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhartikainen/658ec8c27892bf09304c to your computer and use it in GitHub Desktop.
Save jhartikainen/658ec8c27892bf09304c to your computer and use it in GitHub Desktop.
Slack JavaScript REPL bot
var Slack = require('slack-client');
var vm = require('vm');
var s = new Slack('api key here', true, true);
s.on('message', function(msg) {
if(msg.type != 'message') {
return;
}
var c = s.getChannelGroupOrDMByID(msg.channel);
var t = msg.text;
//the bot uses ~ as a trigger when to run JS code
if(t[0] != '~') {
return;
}
try {
var result = vm.runInThisContext(t.substr(1));
}
catch(ex) {
result = ex;
}
console.log('sending %s to %s', result, c.name);
c.send('' + result);
});
s.on('error', console.log);
s.login();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment