Most Popular
Courier is a notification service that centralizes all of your templates and messaging channels in one place which increases visibility and reduces engineering time.
Sign-up
GitHub Repository: https://github.com/shreythecray/infiltration
The hackathon ends soon, and we are giving away over $1K in prizes! Join us in building a cool project and winning any of the following prizes. 🏆
Additionally, everyone who submits a project successfully integrating the Courier API will receive a $20 Amazon gift card! Submissions close on September 28th. Register now to submit this project for a chance to win some cool prizes.
Register for the Hackathon: https://courier-hacks.devpost.com/
Not sure where to start? In this tutorial, we will create a Discord bot that sends daily automated messages encrypted in Morse code with Node.js and the Courier API.
We are secret agents, and we previously built an application to send secret messages encrypted in Morse code to communicate with our spy network. Learn more >
Last time, headquarters told us that one of our spies had leaked sensitive, top-secret information to our enemies, so we built a lie detector that alerted our spy network when we identified the mole. We used Azure's Cognitive Services to perform facial recognition on everyone on our team and Courier to broadcast the identity of the mole to our spy network. Learn more >
We have successfully identified the mule and have alerted our spy network! In an unfortunate turn of events, the mule happens to be our partner, Agent X, and now we are being suspected as a traitor as well. Since we are highly skilled, Headquarters knows that the Lie Detector won’t work on us, but we have been placed off-duty until we can prove that we are innocent.
Before we were removed from duty, we were able to use the Lie Detector to find out that the enemy had thousands of civilians under control in a secret Discord server. The civilians are being brainwashed with enemy propaganda every day. To prove our innocence, we decide to go undercover and infiltrate the enemy’s Discord server. With the help of Agent X, we have been added to the server as an administrator. Our plan is to create and install a Discord bot that automates encrypted messages to the civilians and alerts them about the situation so that they can escape.
Today, we will be building this with Node.js. If you’re curious about how to build this project using Ruby, cURL, Powershell, Go, PHP, Python, or Java, let us know: https://discord.com/invite/courier
. You can also access code for these within our API reference. Let’s get started:
Install dotenv npm package to store variables: npm install dotenv --save
Import and configure dotenv by adding to top of index.js: require("dotenv").config();
Similarly, install node-fetch npm package to make API calls: npm install node-fetch@2
Import and configure node-fetch by adding to top of index.js: const fetch = require("node-fetch");
bot
scopeView Channels
, Send Messages
, and Read Message History
Back in the Discord server, right click on the channel and copy the channel ID (bottom of list). Add this as the value of channelID
in the .env file within the project and save it as a variable within the index.js file:
1const channelID = process.env.channelID
Now, Courier has access to sending messages to this server as the bot.
Run while you can. You can find shelter here: https://discord.com/invite/courier.
Copy the notification template ID from the notification’s settings and add it as the value of templateID
in the .env file within the project and save it as a variable within the index.js file:
1const templateID = process.env.templateID
Create a test event and replace the channel_id
in the JSON with the channel_id
we received from Discord earlier.
1{2"courier": {},3"data": {},4"profile": {5"discord": {6"channel_id": "768866348853383208"7}8},9"override": {}10}
Test a message to ensure that the Discord provider integration is working correctly.
{secretMessage}
so that, later, we can edit the message from our code directly.Create an asynchronous function called encryptMessage()
, which takes originalMessage
as a parameter. This function will call the Morse API, which will allow us to translate any message from English to Morse code. The enemy will have to spend more time and resources into decrypting our messages, which will give our civilians time to escape from the server.
1async function encryptMessage(originalMessage) {2}
Let’s define the GET API call options:
1const morseOptions = {2method: 'GET',3headers: {4Accept: 'application/json',5'Content-Type': 'application/json'6}7};
We need to attach the originalMessage
function parameter to the Morse API endpoint:
1const morseEndpoint = "https://api.funtranslations.com/translate/morse.json?text="+originalMessage
We need to be able to access the translation from this API call in the body of the Courier API call. To call the API, we can use node-fetch as we did before.
morseResponse
, which will hold the entire response from this call.morseResponseJSON
so that we can read it within our code.encryptedMessage
.Return encryptedMessage
so that we can call this function to access it elsewhere.
1const morseResponse = await fetch(morseEndpoint, morseOptions);2const morseResponseJSON = await morseResponse.json();3const encryptedMessage = morseResponseJSON.contents.translated;4console.log(encryptedMessage);5return encryptedMessage;
NOTE: The Morse API has a rate limit, which may give you an error if you run it too many times within the hour. In this case, you will have to wait for some time before continuing.
Check out the Automations API reference >
Create a new asynchronous function called runDiscordAutomation()
, which will call the encryptMessage()
function to translate a message and use the Courier API to automatically send messages to the enemy Discord server everyday.
1async function runDiscordAutomation() {2}
Before we can run our message through the Morse translation API, we need to ensure that it is in the correct format, with all spaces converted into their URL encoding, %20
as shown below. We can call encryptMessage()
with originalMessage
as a parameter to translate it. encryptedMessage
will evaluate as the translated message.
1const originalMessage = "run%20while%20you%20can%20you%20can%20find%20shelter%20here";2const encryptedMessage = await encryptMessage(originalMessage);
Add the link to the safe server in the notification template within the designer: “https://discord.com/invite/courier”
Let’s define the Courier Automation endpoint and options. Here we will need access to our Courier API Key, which can be found within the Courier Settings page. Save the first value in the .env file as apiKey and access it in this file as process.env.apiKey.
1const automationsEndpoint = "https://api.courier.com/automations/invoke"23const courierOptions = {4method: "POST",5headers: {6Accept: "application/json",7"Content-Type": "application/json",8Authorization: 'Bearer ' + process.env.apiKey9},10body: JSON.stringify({11//next steps12}),13};
The body object within the options will encompass two objects: automation
and data
1body: JSON.stringify({2"automation": {3},4"data": {5}6}),
The automation
object will include a steps
array, which will consist of all steps required for the automation. Our automation consists of reminders that are sent once a day - in this case we will be adding three steps: a send step, a delay, and another send step (so on).
1"automation": {2"steps": [],3},
action
(send, delay, cancel, etc.) and message
. The message
consists of the notification template ID (we saved this in the .env file earlier) and information about where this message is being sent. A Discord message requires either a user_id
or a channel_id
. In order to reach as many innocent civilians as possible, as quickly as possible, we will directly send messages in a channel.This is what the send and delay steps would look like:
1{2"action": "send",3"message": {4"template": templateID,5"to": {6"discord": {7"channel_id": process.env.channelID8}9}10}11},
1{2"action": "send",3"duration":"1 day"4},
The data object would need to contain the encryptedMessage
:
1"data": {2"secretMessage": encryptedMessage3}
1 minute
Finally, we can use node-fetch again to call the Automations API and trigger this automation
1fetch(automationsEndpoint, courierOptions)2.then((response) => response.json())3.then((response) => console.log(response))4.catch((err) => console.error(err));
Our Discord bot is ready to save some civilians. Try building a Discord bot of your own and tweet a screenshot of your Courier automated messages in action, and we will send a gift to the first three Secret Agents to complete this task! Head to courier.com/hack-now to get started. Don’t forget to submit your project to our Hackathon for a chance to win over $1000 in cash and prizes!
🔗 GitHub Repository: https://github.com/shreythecray/infiltration
🔗 Courier: app.courier.com
🔗 Register for the Hackathon: https://courier-hacks.devpost.com/
🔗 Discord Application and Bot Guide: https://discord.com/developers/docs/getting-started
🔗 Courier Discord Provider Docs: https://www.courier.com/docs/guides/providers/direct-message/discord/
🔗 Courier Automations Docs: https://www.courier.com/docs/automations/
🔗 Courier Automations API Reference: https://www.courier.com/docs/reference/
Courier is a notification service that centralizes all of your templates and messaging channels in one place which increases visibility and reduces engineering time.
Sign-up
How to Set Up Automatic Push Notifications Based on Segment Events
Push notifications have carved their own niche as a powerful tool for continuous user engagement. Regardless of whether an app is actively in use, they deliver your messages straight to your user's device. Two key players that can combine to enhance your push notification strategy are Segment and Courier. In this tutorial, we show you how to set up Courier to listen to your Segment events and then send push notifications to an Android device based on data from these events.
Sarah Barber
November 17, 2023
How to Send Firebase Notifications to iOS Devices Using Courier
This tutorial explains how to send push notifications to iOS devices from your iOS application code using Firebase FCM and Courier’s iOS SDK.
Martina Caccamo
November 01, 2023
Free Tools
Comparison Guides
Send up to 10,000 notifications every month, for free.
Get started for free
Send up to 10,000 notifications every month, for free.
Get started for free
© 2024 Courier. All rights reserved.