Created
March 21, 2016 20:53
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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