How to create a custom Slack command using Google Cloud Functions and NodeJS

Andrew Bliss
JavaScript in Plain English
5 min readApr 9, 2020

--

Now that we have all become accustomed to working remotely, using chats, video conferencing, and screen sharing, we have been encouraged to share ideas, and with that, a need to create custom workflows.

In this article we will explore three popular technologies, Slack, Google Cloud Functions and NodeJS. Combining these technologies will improve our workflow in Slack.

To start we will go over how to create a Google Cloud Function. If you aren’t familiar with Google Cloud, I suggest trying it out for free.

To create a Google Cloud Function go to the Functions page:

  • Click Create Function.
  • Give it a name, such as alert-cpu.
  • If you want anyone to run this function, which we do, as we want Slack to call this function, then click the checkmark “Allow Unauthenticated Invocations”.
  • This will make it possible for anyone to call the function, however we only want to allow calls from Slack. So we will require that it checks for a token in the query string.
  • Check that the trigger is HTTP.
  • You can select that the runtime is Node 8, you can also select a different Node version.

Cloud Function source code:

/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.alertCpu = (req, res) => {
// Check if it is a valid Slack request
if (req.query.token !== process.env.SLACK_TOKEN) {
return res.status(403).send('No Access');
}

// This is just for an example
const stats = await stats.check('CPU');

res.status(200).json({
response_type: 'in_channel',
attachments: [
{
fallback: `CPU Stats`,
color: '#2eb886',
pretext: `This is checking the CPU of our servers`,
author_name: req.query.user_name,
title: 'Company Name App - CPU Check',
title_link: 'https://your.company.com/status',
text: `The CPU is at ${stats.CPU}%`,
fields: [
{
title: 'Priority',
value: 'High',
short: false,
},
],
footer: 'Company Name',
footer_icon: 'https://your.company.com/logo.png',
},
],
});
};

To finish off the creation of this function, make sure to edit the field called “Function to Execute” and give it the name of the function you are exporting. In our code we export the function with a name like this:

exports.alertCpu

So our function to execute is alertCpu

In the underlying architecture of Google Cloud Functions for Node is just a simple Express server.

Note: You can use the same req and res variables as you would in any Express server.

If you are unfamiliar with Express you can go here to read the documentation:

So now let’s talk about the response we can send back to slack.

First we need to send back a JSON payload. Inside this payload we can configure many things, but for this article we will talk about a few.

response_type: 'in_channel'

This means that when you type the Slack command, it will respond to everyone in that channel and not just yourself. If you just want to see it for your user leave this out.

We can also send attachments as parts of our response, and Slack will format them for us when it displays the message in the channel.

By setting the author_name we can show who sent the command.

We can send images, links, and text. The most important part of this response is this:

text: `The CPU is at ${stats.CPU}%`

We want to alert the channel of the CPU % of our servers. You can do this with any metric or stat you want. How many users are on the system. Send an email to the dev team when the server gets over a certain CPU %. Of course you probably want to set up some kind of monitor system in Google Cloud to take care of this, however this is just an example that you can message in Slack and have your team stay up to date with your systems.

You can read the documentation for all the response fields you can return in the JSON payload here:

Before you are done, remember to add the Slack token to your list of environment variables. Once you are done creating your function, it will show you the URL that you must call in order to get the Slack command to work.

To get the Slack token we must first create a Slack command.

Now let’s go to the Slack groups custom integration page so you can start to create your own Slack command:

https://[YOUR-GROUP-NAME].slack.com/apps/manage/custom-integrations

Click on Slash Commands.

Click on Add to Slack.

On the next page it will show a screen for you to enter what you want the Slack command to be called.

Click Add Slash Command Integration.

On the next page you can configure what URL you want the command to be called. In our case we will put in the URL we received from the Google Cloud Function.

We will change the method from POST to GET.

We also the see the Slack token that we need to add to our environment variables in the Cloud Function. This will make is possible to know the call is coming from Slack.

You can also customize the command by giving it a description, logo, and much more.

So from this article, and other documentation, you can see that it is easy to setup a Slack command, have it gather information for you, and then alert the team, channel, or business, without leaving Slack. You can stay connected and customize your workflow.

Now we can type in the Slack command to test:

/alert-cpu

Of course this is just an example, but you can see the benefits of keeping the workflow in Slack.

A note from JavaScript in Plain English:

We have launched three new publications! Show some love for our new publications by following them: AI in Plain English, UX in Plain English, Python in Plain English — thank you and keep learning!

--

--