alert_matrix.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. # LinuxGSM alert_matrix.sh module
  3. # Author: Daniel Gibbs (Original Structure)
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Sends Matrix alert.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. matrixbase="${matrixhomeserver}"
  9. if [[ "${matrixbase}" != http://* ]] && [[ "${matrixbase}" != https://* ]]; then
  10. matrixbase="https://${matrixbase}"
  11. fi
  12. matrixbase="${matrixbase%/}"
  13. # matrixroom must be the internal room ID (e.g. !abc123:matrix.org), not the room alias (#room:matrix.org).
  14. matrixroomencoded="$(jq -rn --arg room "${matrixroom}" '$room|@uri')"
  15. matrixtxnid="$(date +%s%N)"
  16. matrixurl="${matrixbase}/_matrix/client/v3/rooms/${matrixroomencoded}/send/m.room.message/${matrixtxnid}"
  17. alerttime="$(date)"
  18. message="${alerttitle}
  19. Server Name
  20. ${servername}
  21. Information
  22. ${alertmessage}
  23. Game
  24. ${gamename}
  25. Server IP
  26. ${alertip}:${port}
  27. Server Time
  28. ${alerttime}"
  29. if [ -n "${querytype}" ]; then
  30. imgsourl="https://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}"
  31. message+="
  32. Is my Game Server Online?
  33. ${imgsourl}"
  34. fi
  35. if [ -n "${alerturl}" ]; then
  36. message+="
  37. More info
  38. ${alerturl}"
  39. fi
  40. json="$(jq -cn \
  41. --arg body "${message}" \
  42. --arg title "${alerttitle}" \
  43. --arg servername "${servername}" \
  44. --arg information "${alertmessage}" \
  45. --arg game "${gamename}" \
  46. --arg serverip "${alertip}:${port}" \
  47. --arg servertime "${alerttime}" \
  48. --arg imgsourl "${imgsourl}" \
  49. --arg alerturl "${alerturl}" \
  50. '(@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)")
  51. + (if $imgsourl != "" then @html "<br><br><strong>Is my Game Server Online?</strong><br><a href=\"\($imgsourl)\">Check here</a>" else "" end)
  52. + (if $alerturl != "" then @html "<br><br><strong>More info</strong><br><a href=\"\($alerturl)\">\($alerturl)</a>" else "" end)
  53. | gsub("&apos;"; "&#39;")
  54. | {msgtype: "m.notice", body: $body, format: "org.matrix.custom.html", formatted_body: .}')"
  55. fn_print_dots "Sending Matrix alert"
  56. matrixsend=$(curl --connect-timeout 10 -sSL -X PUT \
  57. -H "Authorization: Bearer ${matrixtoken}" \
  58. -H "Content-Type: application/json" \
  59. -d "${json}" \
  60. "${matrixurl}")
  61. exitcode=$?
  62. matrixeventid="$(echo "${matrixsend}" | jq -r '.event_id // empty' 2> /dev/null)"
  63. if [ "${exitcode}" -eq 0 ] && [ -n "${matrixeventid}" ]; then
  64. fn_print_ok_nl "Sending Matrix alert"
  65. fn_script_log_pass "Sending Matrix alert"
  66. else
  67. fn_print_fail_nl "Sending Matrix alert: ${matrixsend}"
  68. fn_script_log_fail "Sending Matrix alert: ${matrixsend}"
  69. fi