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…

Published by

Andy Brudtkuhl

Andy is a full stack web developer, growth hacker, entrepreneur, bike commuter, seinfeld enthusiast, and daddy to three little girls and two dogs in Iowa.

One thought on “Slack Slash Command With WordPress REST API Backend”

Leave a Reply

Your email address will not be published. Required fields are marked *