How to Integrate Slack into a Node app

Make use of Slack’s API to incorporate features into your Node app

Ravish Chaudhary
Rocketium
5 min readAug 7, 2020

--

Slack brings the team together, wherever you are”. Slack is widely used in the professional world and many of the enterprise application gives the option to share directly to Slack.
This article will cover the basics of how to connect your node app to the Slack and send the text message.

Slack provides an easy to use APIs. We will be using Slack’s web API here.

Let’s get started

What you need from Slack

If you have studied about authentication and APIs you must have heard about tokens (bearer, jwt…) and keys, Similarly to connect your node app to Slack you need a token and key.

First, you need to create an app on Slack which will go live in Slack app directory (kind of play store).
Go to https://api.slack.com/apps and click on Create New App and follow the steps

Create New App

After building the app on Slack you will reach the ‘Basic Information’ page which will be having two key information which we need.

Note the Client Id and Client Secret, we will use them later.

Now we need an authentication token from Slack to call the methods provided on behalf of the user to send messages. For that, we need to set up a few things.

Set up redirect URL and add scopes

Once the user will give permission to your Slack app you will be receiving a code that will be used to retrieve bearer token, but where? For that, we need to set up the ‘redirect URL' . This is the route where the request will be redirected with the code.

Go to 'OAuth & Permissions', here you can add redirect URL. For now, I will add http://localhost:3000/connecttoslack

Scopes are the permissions required by each method of API in order to be used. Read about various methods. There are two types of scope:
1. Bot Scope (If you want to interact with Slack as a bot)
2. User Scope (If you want to interact with Slack on behalf of the user)

We will add chat:write scope to both scopes. To do that below redirect URL there is Scopes section, there you can add scopes.

Setting up node app

  1. Create a folder named slack-integration and initialize the node app

2. Add dependencies:

3. Add a public folder inside slack-integration to serve static files on the server. And add index.html to it

4. Create server.js inside slack-integration and add following to it

Client-side Code

You need to make a request from your client-side to integrate Slack which will perform the Slack login and return the authorization token (Bearer token). Slack gives you the readymade button to use. Add code below to index.html:

Button created by code below

Clicking this button will take the user to the page where he will allow your app to interact with Slack on his behalf. Redirect URL which you have passed in redirect_uri is where the request will be sent from this page. It will be having an authentication code that will be used to retrieve the token using oauth.v2.access method.
NOTE: Make sure the redirect_uri you are providing is listed on Slack in Redirect URLs

Setting up the route to handle redirect URI and send message to Slack

In your server.js add a route /connecttoslack and retrieve token and using that token we will send message to Slack using postMessage() method of Slack. It takes an object as an argument and returns a promise with a response.

And that’s it. You have successfully sent a message to Slack. Check the channel which you mentioned and you will see the message you sent.

Things to read

When you are sending messages to Slack, you don’t want to send plain text messages. There is something called Block Kit Builder.

Slack’s Documentation is great you can read about various methods that Slack provide. https://api.slack.com/methods.

Thanks!

--

--