alert_matrix.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. message="${alerttitle}
  18. Server Name
  19. ${servername}
  20. Information
  21. ${alertmessage}
  22. Game
  23. ${gamename}
  24. Server IP
  25. ${alertip}:${port}
  26. Server Time
  27. $(date)"
  28. if [ -n "${querytype}" ]; then
  29. message+="
  30. Is my Game Server Online?
  31. https://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}"
  32. fi
  33. if [ -n "${alerturl}" ]; then
  34. message+="
  35. More info
  36. ${alerturl}"
  37. fi
  38. json="$(jq -cn --arg body "${message}" '{msgtype: "m.notice", body: $body}')"
  39. fn_print_dots "Sending Matrix alert"
  40. matrixsend=$(curl --connect-timeout 10 -sSL -X PUT \
  41. -H "Authorization: Bearer ${matrixtoken}" \
  42. -H "Content-Type: application/json" \
  43. -d "${json}" \
  44. "${matrixurl}")
  45. exitcode=$?
  46. matrixeventid="$(echo "${matrixsend}" | jq -r '.event_id // empty' 2> /dev/null)"
  47. if [ "${exitcode}" -eq 0 ] && [ -n "${matrixeventid}" ]; then
  48. fn_print_ok_nl "Sending Matrix alert"
  49. fn_script_log_pass "Sending Matrix alert"
  50. else
  51. fn_print_fail_nl "Sending Matrix alert: ${matrixsend}"
  52. fn_script_log_fail "Sending Matrix alert: ${matrixsend}"
  53. fi