Building a Slack App with Google Cloud Functions

Previously I’ve built a few slack apps for integrating things like my WordPress stats, reporting server crashes, and some things for work, but I wanted to build one for fun.

A little while ago I built a npm package called the Leslie Knope Compliment Generator (knope.js). This package provides functions that accept a name (and a few additional parameters), and returns a compliment in the style of Park’s and Recreation’s Leslie Knope.

Since I think it’d be a fun idea to have for slack, I’m going to build a slack app that compliments people in the style of Leslie Knope.

 

Planning It Out

Since executing JavaScript from PHP is a little sketchy, and I’m too lazy to set up my own Node server and forward it all through Apache/Nginx, I instead decided on using Google Cloud Functions. I’m pretty familiar with the Google Cloud Platform (GCP), and although it can be quite expensive at time, it’s also quite easy to work with. Also they gave me a bunch of free credits for developing apps for them, so I’ve got to use them.

Slack Leslie Knope GCP Code

This is the basics of how the function looks on first setup, before I’ve modified the code significantly. First thing’s first, we need to install knope.js with the package.json. Our current one looks like this:


{
  "name": "sample-http",
  "version": "0.0.1"
}

We simply need to modify it to include knope.js, like this:


{
  "name": "knope-slack-integration",
  "version": "0.0.1",
  "dependencies": {
    "knope": "^1.1.2"
  }
}

Next, we can make some changes to the code, which looks like this to start:


/**
 * Responds to any HTTP request.
 *
 * @param {!Object} req HTTP request context.
 * @param {!Object} res HTTP response context.
 */
exports.main = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

After changing a few basic things, we get this code here:


/**
 * Responds to any HTTP request.
 *
 * @param {!Object} req HTTP request context.
 * @param {!Object} res HTTP response context.
 */
exports.main = (req, res) => {
  const knope = require('knope')
  let message = req.query.message || req.body.message || 'Hello World!';
  let compliment = knope.getCompliment('Ann', 'random')
  res.status(200).send(compliment);
};

This code will respond to any HTTP request with a compliment with the name Ann, which is pretty cool.

Test Compliment

But we want this to compliment slack users, so we need to be able to accept an incoming POST from the slack API. We can accept parameters from the slash command from the slack API by checking the request’s body’s “text” parameter. Here’s what it looks like in code.


/**
 * Responds to Slack API Reuqests with a compliment
 *
 * @param {!Object} req HTTP request context.
 * @param {!Object} res HTTP response context.
 */
exports.main = (req, res) => {
  const knope = require('knope')
  let name = req.body.text;
  if (name.indexOf(' ') !== -1) {
    // If we've got spaces, strip 'em
    name = name.split(' ')[0]
  }
  let compliment = knope.getCompliment(name, 'random')
  res.status(200).send(compliment);
};

I also added a little method to strip away anything after the first space, so only the first name is shown. With it tested and functional, we can now integrate it with slack!

 

Integrating with Slack

We can now head on over to api.slack.com and create an app with our workspace – and eventually for distribution. I have my own private workspace I use for development and server monitoring – and I’d highly recommend it. It’s nice to get alerts directly to my phone and very easy to set up.

Creating a slack app screenshot

Next, we follow the instructions for creating a new slack app (which are pretty easy). Afterwards, we’re going to enable slash commands, which allows the user to request a compliment for a name of their choosing.

 

slash command

 

It’s a very easy setup process. Choose your command, a short description, usage hints, and your request URL. The request URL is the one you’ll find under the “trigger” section of the Google Cloud Function, which allows slack to trigger the function by sending a POST request.

 

knope slack test

and…

knope slack test

 

It works! In the first example I tested the command

/knope ryder

and in the second

/knope ryder more text

 

Next Steps

The next steps are distribution and polish. Before release and distribution I intend on doing the following.

  • Adding a profile photo
  • Adding a better description
  • Creating a project for the site
  • Creating a privacy policy for the app
  • Altering the code to always capitalize the first letter of the name
  • Maybe adding the alliteration compliment option.

 

That’s all folks! Happy coding!