jasonloong.com Forums

Sending Linux Variables to Discord Webhook from Linux Bash Shell Script

Originally published at: https://jasonloong.com/2018/sending-linux-variables-to-discord-webhook-from-linux-bash-shell-script/

Sending Linux Variables to Discord Webhook from Linux Bash Shell Script Discord provides an API where you can send messages/notifications via their webhook using curl . Which is all very helpful, except the code provided on their website only allows plain text, you can’t pass $variables onto it Having started playing with Linux Bash scripts,…

used the script, works for normal messages but when I write $ commands I get this
{“message”: “Cannot send an empty message”, “code”: 50006}

can you post your script ?

1 Like
#!/bin/bash

DISCORD_WEBHOOK_URL=""
DISCORD_ICON_OVERRIDE="https://i.imgur.com/KorF8zC.png"
DISCORD_NAME_OVERRIDE="TEST-Messages"
DISCORD="/tmp/discord.discord"
###############
rm -rf "${DISCORD}" && touch "${DISCORD}"

echo "[test message] `date '+%T %A %d-%B,%Y'`  status done" >"${DISCORD}"

# Send start message via Díscord
if [ $DISCORD_WEBHOOK_URL != "null" ]; then
   message=$(cat "${DISCORD}")
   msg_content=\"$message\"
   USERNAME=\"${DISCORD_NAME_OVERRIDE}\"
   IMAGE=\"${DISCORD_ICON_OVERRIDE}\"
   DISCORD_WEBHOOK_URL="${DISCORD_WEBHOOK_URL}"
   curl -H "Content-Type: application/json" -X POST -d "{\"username\": $USERNAME, \"avatar_url\": $IMAGE, \"content\": $msg_content}" $DISCORD_WEBHOOK_URL
 else
   echo "${output}  failed"
fi

exit 0

if you want to send more as a one_liner

echo "[test message] \n `date '+%T %A %d-%B,%Y'`  \n status done" >"${DISCORD}"

Screenshot1

1 Like

Welcome to the forums @Bryce_Canion

Could you show us some code that you are trying to run so that we can further help you?

As the error implied, you sent an empty message over, which most probably indicates that the command you ran return empty or error

@doob187 thx for the quick code snippet example!

I used the very same script of this article, having some issues posting it

but can you show us the script ?

maybe its a simple small error …

Try put this in front of your code

``` code here

and end it with ```

or select your code and then click on the " </> "

screenshot works too :slight_smile:

#!/bin/bash
message="$@"
## format to parse to curl
## echo Sending message: $message
msg_content=\"$message\"

## discord webhook
url='https://discordapp.com/api/webhooks/urlnumbers
curl -H "Content-Type: application/json" -X POST -d "{\"content\": $msg_content}" $url

can you try :smile:
sudo chmod a+x discord-notify.sh

./discord-notify.sh "[test message] `date '+%T %A %d-%B,%Y'` status done"

Tried, still can send normal messages but not $ ones

Can you show me the full code you are trying to send including the message content?? Was it the “date” output as shown on the tutorial?

Ok, skipped a thing above.
I get the date [test mesage] 13:56:49 Domenica 17-Maggio,2020 status done like this.
the command I’m trying to send is $dk and should be processed by another bot in the channel.
If I put it in the [test mesage] I get nothing but []

By another bot… erm… I’m not understanding, anyway, let’s check your $dk

Can you confirm that it returns the output by

echo $dk

I have a bot on the discord channel who takes some $commands as inputs. I want to make a script to give him some commands in sequence basically. Problem is I’m not managing to feed him those commands through this process.

Where do I write echo $dk

PS thanks for help

Oh maybe I got it wrong, sorry, and this script is supposed to work in a different way.

Ahhhhhhh I get you now

So you want to output “$dk” to your channel as it is.

We took it that you have a variable called $dk in Linux itself, not discord.

In that case you would do this to your message content:

your_text="white space and your '$dk' message"

You need to wrap the $dk with Single Quotes(not Double-Quotes), to special-escape that whole string, which is $dk

So if you only want to output “$dk” to your channel

my_msg='$dk'
or
my_msg="'$dk'"

Have a try at it, @doob187 tested it and it worked :slight_smile:

Let us know if it worked out :grin:

1 Like

OK thanks, using only single quotes (I did not use the my_msg=…, is it a problem?) I can have it. Unluckily seems like the other bot doesn’t recognize it, probably because it comes from another bot. Thanks for help though
Don’t want to bother you more, by chance do you know if I can send messages this way as user and not as bot?

None that I know of… not a Discord expert sorry

Thanks for your time and reply anyway! :smiley:

you can send over this bot automatic messages