| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #!/bin/bash
- # LinuxGSM alert_matrix.sh module
- # Author: Daniel Gibbs (Original Structure)
- # Contributors: https://linuxgsm.com/contrib
- # Website: https://linuxgsm.com
- # Description: Sends Matrix alert.
- moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
- matrixbase="${matrixhomeserver}"
- if [[ "${matrixbase}" != http://* ]] && [[ "${matrixbase}" != https://* ]]; then
- matrixbase="https://${matrixbase}"
- fi
- matrixbase="${matrixbase%/}"
- # matrixroom must be the internal room ID (e.g. !abc123:matrix.org), not the room alias (#room:matrix.org).
- matrixroomencoded="$(jq -rn --arg room "${matrixroom}" '$room|@uri')"
- matrixtxnid="$(date +%s%N)"
- matrixurl="${matrixbase}/_matrix/client/v3/rooms/${matrixroomencoded}/send/m.room.message/${matrixtxnid}"
- alerttime="$(date)"
- message="${alerttitle}
- Server Name
- ${servername}
- Information
- ${alertmessage}
- Game
- ${gamename}
- Server IP
- ${alertip}:${port}
- Server Time
- ${alerttime}"
- if [ -n "${querytype}" ]; then
- imgsourl="https://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}"
- message+="
- Is my Game Server Online?
- ${imgsourl}"
- fi
- if [ -n "${alerturl}" ]; then
- message+="
- More info
- ${alerturl}"
- fi
- json="$(jq -cn \
- --arg body "${message}" \
- --arg title "${alerttitle}" \
- --arg servername "${servername}" \
- --arg information "${alertmessage}" \
- --arg game "${gamename}" \
- --arg serverip "${alertip}:${port}" \
- --arg servertime "${alerttime}" \
- --arg imgsourl "${imgsourl}" \
- --arg alerturl "${alerturl}" \
- '(@html "<strong>\($title)</strong><br><br><strong>Server Name</strong><br>\($servername)<br><br><strong>Information</strong><br>\($information)<br><br><strong>Game</strong><br>\($game)<br><br><strong>Server IP</strong><br>\($serverip)<br><br><strong>Server Time</strong><br>\($servertime)")
- + (if $imgsourl != "" then @html "<br><br><strong>Is my Game Server Online?</strong><br><a href=\"\($imgsourl)\">Check here</a>" else "" end)
- + (if $alerturl != "" then @html "<br><br><strong>More info</strong><br><a href=\"\($alerturl)\">\($alerturl)</a>" else "" end)
- | gsub("'"; "'")
- | {msgtype: "m.notice", body: $body, format: "org.matrix.custom.html", formatted_body: .}')"
- fn_print_dots "Sending Matrix alert"
- matrixsend=$(curl --connect-timeout 10 -sSL -X PUT \
- -H "Authorization: Bearer ${matrixtoken}" \
- -H "Content-Type: application/json" \
- -d "${json}" \
- "${matrixurl}")
- exitcode=$?
- matrixeventid="$(echo "${matrixsend}" | jq -r '.event_id // empty' 2> /dev/null)"
- if [ "${exitcode}" -eq 0 ] && [ -n "${matrixeventid}" ]; then
- fn_print_ok_nl "Sending Matrix alert"
- fn_script_log_pass "Sending Matrix alert"
- else
- fn_print_fail_nl "Sending Matrix alert: ${matrixsend}"
- fn_script_log_fail "Sending Matrix alert: ${matrixsend}"
- fi
|