Skip to content

Instantly share code, notes, and snippets.

@abrudtkuhl
Created March 21, 2016 20:53
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 abrudtkuhl/d70b1d53ee93d83b2f2c to your computer and use it in GitHub Desktop.
Save abrudtkuhl/d70b1d53ee93d83b2f2c to your computer and use it in GitHub Desktop.
Slack bot in Node.js grabbing data from a WordPress REST API backend.
'use strict';
var WP = require( 'wordpress-rest-api' );
var wp = new WP({ endpoint: 'http://local.dev/wp-json/' });
var Bot = require('slackbots');
var settings = {
token: XXXX,
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('seinfeld') > -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];
wp.root('api/any').get(function( err, data ) {
if ( err ) {
console.log("nothing there");
}
bot.postMessageToChannel(channel.name, '@' + fromUser + ' ' + data[0]['post_content'], params);
});
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment