1) Write a Rest API in node.js

I made you a repo to fork, you can change this as much as you like. Or not. For this demo, you can leave it as is — as I’ll be referring to it throughout. So you know, make your life harder if you wish.

This is a simple /slack/hello-world REST endpoint, that when POSTed to returns back an appropriate response, and is about as simple as it can be. When you hit an endpoint (be that via Slack, Postman, Curl, GraphQL anything) you’ll get a response back. In this case, it’s set up to be a response that Slack can read and translate into a message to the user.

This isn’t a tutorial on how to write node.js or slash commands, there are plenty of those out there. I’ve even linked some at the bottom of this article.

2) Test locally with Postman

Clone your repo git clone ... and cd path/to/folder

Install: npm i

Run: npm run start

Go to Postman, create a new request, set the HTTP method to POST and the URL to: localhost:5000/slack/hello-world , hit send, and unless you changed it already (I did warn you), your response should be like this:

{

"attachments": [

{

"color": "#000000",

"text": "hello world!"

}

],

"response_type": "in_channel"

}

If you wanted to develop your API a little more, run it locally and test it with Slack; then ngrok is a great tool for this (this article goes into much more detail on this subject).

You can also write tests against the responses of your API requests within Postman too, which can be useful for developing this further.

Postman. Reliable, unlike a real Postman.

3) Github -> Heroku

Set up your repo in GitHub (or not if you forked mine).

In Heroku, create a new application, and when prompted, set your deployment method to GitHub. You may be asked to authenticate, do that. Finally, select the appropriate repo.

This will link your newly made repo up with the Heroku app.

You will need to hit “Deploy” the first time. I’ve set mine to auto-deploy on push to master. This way, you don’t really need to touch Heroku again unless you want to see logs or whatever.

At this point, once it’s deployed, check your rest API in Postman against your Heroku URL https://{your-url}.herokuapp.com/slack/hello-world — you should get the same response as you did locally.

If you’ve had problems here, i.e. a 503, it’s worth noting you need a Procfile for Heroku. The Heroku logs are useful and will be your best place to start tracking down any problems. (There is a Procfile included in the repo provided)

ooh, fancy gif of doing that thing

4) Slack slash commands

The final piece of the puzzle. Go to Slack apps and “Create new app”, set it up with colours/pictures etc. For now, we’re taking the simplest route, but this API can do loads of neat things and it is really worth taking the time to play around with it.

Be more creative than me, please.

Create a new slack command — this will be linked to your Heroku API — specifically your URL https://{your-url}.herokuapp.com/slack/hello-world — which will essentially do the same thing you’ve done on Postman, only now, it can be triggered within your workspace!

(Don’t forget to link it up to your workspace)