Building A Slack Bot With Node.js And WordPress REST API

Some of my first experiments in the world of building a slack bot was to develop a basic Slash command using the WordPress REST API as a backend. If you or your team are the only users that intend to use it, you can just set it up as a simple integration. However if you wish to distribute it for others to use you can package it into a Slack bot.

An advantage of building a Slack bot versus a slash command is the additional features of the Slack API you can leverage – in this example, the RTM (Real Time Messaging) API. That allows you to listen and respond to messages in real-time. I started out building this layer as a WordPress plugin so anyone could install the plugin and build a WP powered bot. Not only was that out of scope for a fun side project, Node is really the right tool for the job – asynchronous, great packages for the Slack API, easy to deploy and scale (because who doesn’t wan’t a Kramer bot).

Bot Slack RTM API
Kramer Bot using Slack RTM API

Continue reading Building A Slack Bot With Node.js And WordPress REST API

Slack Slash Command With WordPress REST API Backend

I’ve spent the last several weeks building Slack bots and other custom integrations. One of the simplest types of Slack integration to build is the Slash Command – which can be a way for a user to interact with a bot or to provide functionality to a team or channel. Slack provides built in commands (like /topic and /remind) or you can develop custom commands either through a Slack app or just a one off implementation.

Setting up a slash command is pretty easy, because all you need to provide is a RESTful endpoint that returns data to post to a channel.

Slack Slash Command
Hey Kramer bot slash command integration

Slack will do a POST or GET to your endpoint with either JSON or query string parameters giving you information about the command that was given.

Slash Command POST Data
Slash Command POST Data

And your endpoint can simply return a string to send back to the user who initiated the slash command. Or you can send back a formatted message which is essentially a JSON package with the message to send back and some options along with it.

Kramer Slack Bot

This is where WordPress and its REST API come in. This is also where WordPress starts to become a platform. I’m not designing a theme or even building a website with WordPress. I’m creating a RESTful API with a nice simple way for users to enter data. In less than 5 minutes you can have a fully functional API outputting JSON via RESTful URLs backed by an easy to use and extend CMS.

Out of the box you have access to all data in your WP backend with a simple URL schema…. ie ‘/posts’ get a paged list of posts, ‘/posts/{id}’ to get a specific post. Here is an example from the Seinfeld API I am developing for the Kramer bot. Notice the URL structure: https://heykramer.com/wp-json/wp/v2/posts

WordPress REST API JSON Output
WordPress REST API JSON Output

Great! But not so fast … Slack is expecting something specific back. Well thankfully the REST API is easily extendible. Below is an example plugin I created to show how easy it is to extend the WP REST API to provide Slack what it needs back. (here it is on Github

To extend WP REST API, I register a custom endpoint and define a callback function to execute when that URL is requested. In this instance we are expecting a GET request with query string parameters from Slack. It expects one of two commands: gif or image. Based on the command, it will query different categories of content on WP backend and return one random result to be posted in the Slack channel where the slash command was initiated.

Kramer Bot Responds To Slash Command

Next up – building out a full Slack bot on NodeJS with a WordPress backend…