Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.