How to use Novu to send notifications to a channel in a Discord server
Learn to send Discord notifications swiftly with Novu
Introduction
In this guide, you’ll learn how to use Novu to send notifications to any channel in a Discord server. But before coding anything up, we just need to go over a couple of setup steps. The corresponding docs for this guide are available on our docs.
So let’s begin!
Set up Discord
Setting up Discord is fairly straightforward. You just need the webhook Url
. It is pretty simple and can be done in the following easy steps:
- Go to the channel you want to add the webhook to (you need to be an admin of the discord server).
Go to the channel on Discord
- Right-click the channel and select “Edit Channel”.
Right click and select 'Edit'
- Select Integrations -> Webhooks -> Create Webhook
Create webhook from the Integrations menu
- Edit the Bot name to your liking, copy the webhook URL and store it somewhere. We’ll need it in the future.
Copy the webhookUrl and keep it somewhere
Set up Novu
In this part, we’ll set up a workflow that will be triggered when we send a notification. Workflows are like blueprints and hold the entire flow of notifications. You can read more about them on our docs. To set up a notification workflow for our app, follow these steps:
- Make sure that you’ve set the Discord Integration as active from the Novu Integrations Store.
Make sure Discord Integration is active
- Goto 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
Copy the trigger code
Now, we’ve completed all the setup required and can move to coding!
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. 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:
novu.js
, in our case, which is as follows:
Add the trigger code you’d copied earlier:
Our final trigger code should look something like this:
.env
file to avoid hardcoding as a good practiceIn this code snippet above, we’re first initializing a new instance of Novu, then using it to ‘identify’ or create a subscriber.
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 Discord notifications using Novu!