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.