alert_matrix.sh 1.6 KB

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