Send Server messages to Discord

 

Send a server message
into a Discord Channel
Discord Button
Image by Alexander Shatov



For our SaaS service JUST-SELL.online, we have married our servers to Discord.

Since our team works decentrally, we do communicate very much via a messenger. It's always on, during the day.

Minimize reaction time

Since Discord is an central point of our communication, it was clear to us that important and critical server messages should be visible in real time in our Discord-Channels.
This way, we can react immediately or stay up-to-date with important events that happen on the server.
Here are a few events that are interesting for us as a source of ideas:

  • Check if our servers are available form the internet
  • Check whether the automatic deployment of a new software version was successful
  • Warnings when server processes are heading towards critical areas (RAM / CPU)
  • Make root logins visible on the server
  • and a some more...

Group / channel structure in Discord

How we do it: We are grouping our single Server-Channels into one Discord-Group "Server" .
For each server, we create an own channel and give it the name of the respective server.
In the server channels we only post things that relate to this server. General discussions, agreements on the infrastructure, servers, etc. take place in the "#general" channel. In this way, the history in the server channels remains clear and clear.

Channels in Discord


 

Setup Discord

To connect a sever with Discord, you need to create an WebHook in Discord. Choose a channel in the List at the left side and click at the gear-icon.

Now choose Integrations at the left side menu. Then you see the Dialoge to create an WebHook.
Click the Button "Create Webhook" and set it up. 
Here is how our WebHool looks like:





Now click at the Button "Copy Webhook URL" - we'll need it later on in our BASH-Script.

Server Setup

Our server has a Linux operation-system. 


BTW: 
You also can create an Batch-Script for Windows based on my suggestion below.
If you'll do, please paste your Windows-Script in the comments belowm
it might be useful for other ones.


Connect to your server and put following BASH-script into it. I recommend `/usr/local/bin`  and Filename: `discord_notification`.

Now put following sourcecode in there. 

NOTICE: Replace the value of the second parameter `
--url` with the Webhook URL you copied from Discord some seconds ago.

#!/bin/bash
if [ $# -ne 2 ]; then
    echo -e "\nNo arguments provided.\nUse this script like this:\n\t $0 <yourBotName> \"<yourMessage>\""
    exit 1
fi
 
curl --request POST \
  --url https://discord.com/api/webhooks/123123123123/xxxxxxxxxxxx-yyyyyyyyyyyyyyyyyyyyyy_zzz \
  --header 'Content-Type: application/json' \
  --data '{"username": "'"$1"'-BOT:","content": "'"$2"'"}'

Now make the script `discord_notification` executable:


⇒ chmod +x /usr/local/bin/discord_notification

Use the Script

Now you can send Messages from your Server to a Discord-Channel by this command:

⇒ discord_notification XST01 "Hi, I'm the new one"


Did you find the article helpful / liked?

Then feel free to grab one (or all) of the following:

  • Send this article to someone it may be of interest to
  • Mention us in your channels (Facebook, LinkedIn etc.)
  • Leave a comment


Photo by Howie R






Comments