Learn to send Discord notifications swiftly with Novu
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.
The entire code for this app is available on our GitHub.
So let’s begin!
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 on Discord
Right click and select **'Edit'**
Create webhook from the Integrations menu
Copy the webhookUrl and keep it somewhere
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 Discord Integration is active
Create a workflow from the Novu Web Dashboard
Drag and drop the **'chat'** option
Configure the step as per your liking
Copy the trigger code
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:
Notice how we’re expecting ‘chatMsg’ in our payload. This is why we added it as a variable in the workflow created earlier.
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: ```jsx import from ‘@novu/node’;
Our final trigger code should look something like this:
We’re keeping all the sensitive data in a .env
file to avoid hardcoding as a
good practice
In this code snippet above, we’re first initializing a new instance of Novu, then using it to ‘identify’ or create a subscriber.
The ‘identify’ method tries to find a subscriber with the given info. If it can’t find any such subscriber, it creates a new subscriber with the supplied info.
After creating the subscriber, it runs the trigger code for the workflow we had created.
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!