How to Integrate Slack into a Node app
Make use of Slack’s API to incorporate features into your Node app
“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
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
- Create a folder named slack-integration and initialize the node app
In terminalmkdir slack-integration
cd slack-integration
npm init -y
2. Add dependencies:
// For creating server
npm install express// Slack web API module
npm install @slack/web-api// For sending api calls
npm install axios
3. Add a public folder inside slack-integration to serve static files on the server. And add index.html
to it
mkdir public
touch ./public/index.html
4. Create server.js
inside slack-integration and add following to it
const express = require('express');
const app = express();
const axios = require('axios');
const { WebClient } = require('@slack/web-api');
const PORT = 3000;// this will allow us to use slack api methods
const client = new WebClient();app.use(express.static('public'));
app.use(express.json());app.listen(PORT, () => {
console.log('listening at', PORT);
});
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
:
<a href="https://slack.com/oauth/v2/authorize?client_id=[clientId retrived from above]&scope=[scopes you need]&user_scope=[user scopes you need]&redirect_uri=http://localhost/connecttoslack"><img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcSet="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x" /></a>
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.
const express = require('express');
const app = express();
const axios = require('axios');
const { WebClient } = require('@slack/web-api');
const PORT = 3000;// this will allow us to use slack api methods
const client = new WebClient();app.use(express.static('public'));
app.use(express.json());// code is a query parameter
app.get('/connecttoslack', async (req, res) => {
const auth = await client.oauth.v2.access({
code: req.query.code,
client_id: <your_client_id>,
client_secret: <your_client_secret>,
redirect_uri:'http://localhost:4000/connecttoslack'
});
try {
// Sending Message to slack
const res = await client.chat.postMessage({
token: auth.authed_user.access_token,
channel: <channelName>,
text: <Enter your message to send>,
as_user: true
});
console.log(res);
} catch (e) {}
});app.listen(PORT, () => {
console.log('listening at', PORT);
});
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!