Skip to content

feat(alerts): add Matrix support to alerts #3507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lgsm/functions/alert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,22 @@ elif [ -z "${slacktoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
echo -e "* https://docs.linuxgsm.com/alerts/slack"
fn_script_error "Slack token not set"
fi

if [ "${matrixalert}" == "on" ]&&[ -n "${matrixtoken}" ]; then
alert_matrix.sh
elif [ "${matrixalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
fn_print_warn_nl "Matrix alerts not enabled"
fn_script_log_warn "Matrix alerts not enabled"
elif [ -z "${matrixhomeserver}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
fn_print_error_nl "Matrix homeserver not set."
echo -e "* https://docs.linuxgsm.com/alerts/matrix"
fn_script_error "Matrix homeserver not set."
elif [ -z "${matrixtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
fn_print_error_nl "Matrix token not set."
echo -e "* https://docs.linuxgsm.com/alerts/matrix"
fn_script_error "Matrix token not set."
elif [ -z "${matrixroom}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
fn_print_error_nl "Matrix room not set."
echo -e "* https://docs.linuxgsm.com/alerts/matrix"
fn_script_error "Matrix room not set."
fi
29 changes: 29 additions & 0 deletions lgsm/functions/alert_matrix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
# LinuxGSM alert_matrix.sh module
# Contributors: http://linuxgsm.com/contrib
# Website: https://linuxgsm.com
# Description: Sends Matrix alert.

functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"

json=$(cat <<EOF
{
"msgtype":"m.notice",
"body":"**${alertemoji} ${alertsubject} ${alertemoji}**\n\n**Server name**\n${servername}\n\n**Message**\n${alertbody}\n\n**Game**\n${gamename}\n\n**Server IP**\n[${alertip}:${port}](https://www.gametracker.com/server_info/${alertip}:${port})\n\n**Hostname**\n${HOSTNAME}\n\n**More info**\n[${alerturl}](${alerturl})",
"format": "org.matrix.custom.html",
"formatted_body": "<p><b>${alertemoji} ${alertsubject} ${alertemoji}</b></p>\n<p><b>Server name</b></p><p>${servername}</p>\n<p><b>Message</b></p><p>${alertbody}</p>\n<p><b>Game</b></p><p>${gamename}</p>\n<p><b>Server IP</b></p><p><a href=\'https://www.gametracker.com/server_info/${alertip}:${port}\'>${alertip}:${port}</a></p>\n<p><b>Hostname</b></p><p>${HOSTNAME}</p>\n<p><b>More info</b></p><a href=\'${alerturl}\'>${alerturl}</a>"
}
EOF
)

fn_print_dots "Sending Matrix alert"
matrixsend=$(curl --connect-timeout 10 -sSL -H "Content-Type: application/json" -X POST -d """${json}""" "https://${matrixhomeserver}/_matrix/client/r0/rooms/${matrixroom}/send/m.room.message?access_token=${matrixtoken} | grep "error")


if [ -n "${matrixsend}" ]; then
fn_print_fail_nl "Sending Matrix alert: ${matrixsend}"
fn_script_log_fatal "Sending Matrix alert: ${matrixsend}"
else
fn_print_ok_nl "Sending Matrix alert"
fn_script_log_pass "Sent Matrix alert"
fi