-
-
Save Xmonpl/3c8585d34c83459ac6a3be1f8cb5d129 to your computer and use it in GitHub Desktop.
| #!/bin/bash | |
| #CREATED BY XMON FOR OpenMediaVault | |
| DISCORD_WEBHOOK='PUT YOUR WEBHOOK LINK HERE' | |
| generate_post_data() { | |
| cat <<EOF | |
| { | |
| "content": "", | |
| "embeds": [{ | |
| "title": "${OMV_NOTIFICATION_SUBJECT}", | |
| "description": "$(cat ${OMV_NOTIFICATION_MESSAGE_FILE})", | |
| "color": "45973", | |
| "footer": { | |
| "text": "${OMV_NOTIFICATION_DATE}", | |
| "icon_url": "" | |
| } | |
| }] | |
| } | |
| EOF | |
| } | |
| curl -H "Content-Type: application/json" -X POST -d "$(generate_post_data)" $DISCORD_WEBHOOK |
Thanks for the script! Seems to work well. I hit "Test" in openmediavault ad I get the message in Discord almost instantly. However, I don't get any of the regular system messages from openmediavault (all messages are received by email just fine). What could be blocking this?
If you want to show the output of a file or stdin in bash, and your file includes special characters such as backticks (`) and ", then you can't simply cat filename. You will need to properly escape special characters.
One of the easiest methods to output properly escaped text from a file is to use jq, cut, and rev
Prerequisites
[jq](https://stedolan.github.io/jq/) - Character escaping
[cut](https://linux.die.net/man/1/cut) - Cut characters from string (part of [coreutils](https://www.gnu.org/software/coreutils/coreutils.html), included by default on most systems)
[rev](https://linux.die.net/man/1/rev) - Reversing of characters (part of [util-linux](https://en.wikipedia.org/wiki/Util-linux), included by default on most systems)
Usage:
Simply pass filename to the following command to escape the file contents to stdout:
jq -Rs . <filename | cut -c 2- | rev | cut -c 2- | rev
So, instead of
$(cat ${OMV_NOTIFICATION_MESSAGE_FILE})
use this
$(jq -Rs . <${OMV_NOTIFICATION_MESSAGE_FILE} | cut -c 2- | rev | cut -c 2- | rev)
So the working bash script to put in /usr/share/openmediavault/notification/sink.d/20discord is here:
#!/bin/bash
DISCORD_WEBHOOK='PUT YOUR WEBHOOK LINK HERE'
generate_post_data() {
cat <<EOF
{
"content": "",
"embeds": [{
"title": "${OMV_NOTIFICATION_SUBJECT}",
"description": "$(jq -Rs . <${OMV_NOTIFICATION_MESSAGE_FILE} | cut -c 2- | rev | cut -c 2- | rev)",
"color": "45973",
"footer": {
"text": "${OMV_NOTIFICATION_DATE}",
"icon_url": ""
}
}]
}
EOF
}
curl -H "Content-Type: application/json" -X POST -d "$(generate_post_data)" $DISCORD_WEBHOOK
@beppefiocco Tysm, your solution worked great.
yes. I keep email on, just disable authentication so that emails don't actually go out. The Test button should work to test the discord notification at this point.