How to use Novu to send notifications to a Slack channel
Learn to send Slack notifications swiftly with Novu (manually managed)
Introduction
In this guide, you’ll learn how to use Novu to send notifications directly to a Slack channel. But before coding anything up, we first need to go through a setup process. The corresponding docs for this guide are available on our docs.
So let’s begin!
Create a Slack App
Creating a Slack app is fairly simple. Follow these steps to create your app:
- Go to Slack’s app dashboard and click on the ‘Create new App’ button, as shown in the image:
Create an app from Slack's app dashboard
- Choose ‘From Scratch’ from the following dialog:
Select 'From Scratch' in the following dialog
- Choose a name for your app and select the Slack workspace in which you want to send notifications:
Name your app and select the workspace
- Once you’re done, simply click the ‘Create App’ button:
Click on the 'Create App' button
- Once done, you’ll be greeted with the screen shown below. We’ll make a couple of changes here and it’ll be ready to go.
Slack greets you with this screen
- We’ll need
Client Id
from the Slack Developer’s Dashboard for configuring Slack Integration in the Novu Web Dashboard later, so keep it handy.
Get 'Client Id' from the Slack developer's dashboard
Create a workflow in the Novu Web Dashboard
- To create a workflow, head to the workflow section in the Novu Web Dashboard.
- Click on the ‘Add a workflow’ button and select ‘Blank workflow’ from the dropdown.
Create a workflow from the Novu Web Dashboard
- Once there, give your workflow a name and drag and drop the ‘chat’ option below the ‘workflow trigger’ step.
Drag and drop the 'chat' option
- You can also add variables in the Workflow Editor. For example, here I’ve added ‘chatMsg’ as a variable as I’ll be sending data using it.
Configure the step as per your liking
Make sure that Slack integration is set to active
- To turn the Slack integration on, you’ll need
Client Id
. You should have it already but if you don’t, you can obtain it from the Slack Developer’s Dashboard.
Get 'Client Id' from the Slack developer's dashboard
- Once you have it, you need to plug it into the respective field in the Slack Integration Settings on the Novu Web Dashboard.
Plug the 'Client Id' into the Slack Integration setting in Novu web dashboard
Create the backend
The backend for this app is quite simple. Simply install the Novu package:
Now, create a route that you’ll hit when called from the front end. We’ll also need to add it to our Slack app (discussed below). For our demo app, this is the route I’ve created:
Now, we need a controller function to handle what is to be sent in the trigger’s function payload. Here’s the controller I wrote:
To make it modular, we’ll keep the trigger code in a separate function in a separate file, novu.js
, in our case, which is as follows:
In this code snippet above, we’re first initializing a new instance of Novu, then using it to ‘identify’ or create a subscriber.
Click the 'Get Snippet' button to get trigger code
Paste this trigger code in your backend
Configure the Slack app
There are two ways to configure the Slack app. One is the Novu managed option and another the manually managed option. Since the Novu managed is fairly straightforward, I’ll demonstrate the manual method here. Follow along to set it up!
- Goto ‘Incoming Webhooks’ in your Slack app settings and turn it on.
Turn on 'Incoming Webhooks' in your Slack app
- Click on the ‘Add New Webhook to Workspace’:
Click the 'Add New Webhook to Workspace' button
- Now, go ahead and select the channel in which you want to send notifications and click ‘allow’.
Select the channel
- Then, copy the ‘webhookUrl’ from Slack.
Copy the webhookUrl from Slack
- Now, add the code below to the backend trigger functionality. Using this code, we are specifying what
webhookUrl
Novu will use to authenticate when using the Slack provider.
In our case, we’ll add it to the Novu.js
file we created earlier so that the final file becomes :
- Finally, we’ll add the route we’d created earlier to the redirect URL section, as shown:
Add your endpoint to the redirect URL section on Slack
Front end set up
For our demonstration purposes, the front end is pretty basic. All we have to do is have an input field to take the notification text in and send the payload. The front end code for this demo app is available on our Github repo. And here’s our app in all its glory!
Our app in action!
This is how we send Slack notifications using Novu!