alert_telegram.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # LinuxGSM alert_telegram.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Sends Telegram Messenger alert.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. json=$(
  9. cat << EOF
  10. {
  11. "chat_id": "${telegramchatid}",
  12. "message_thread_id": "${telegramthreadid}",
  13. "parse_mode": "HTML",
  14. "disable_notification": "${telegramdisablenotification}",
  15. "text": "<b>${alerttitle}</b>\n\n<b>Server Name</b>\n${servername}\n\n<b>Information</b>\n${alertmessage}\n\n<b>Game</b>\n${gamename}\n\n<b>Server IP</b>\n${alertip}:${port}\n\n
  16. EOF
  17. )
  18. if [ -n "${querytype}" ]; then
  19. json+=$(
  20. cat << EOF
  21. <b>Is my Game Server Online?</b>\n<a href='https://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}'>Check here</a>\n\n
  22. EOF
  23. )
  24. fi
  25. if [ -n "${alerturl}" ]; then
  26. json+=$(
  27. cat << EOF
  28. <b>More info</b>\n<a href='${alerturl}'>${alerturl}</a>\n\n
  29. EOF
  30. )
  31. fi
  32. json+=$(
  33. cat << EOF
  34. <b>Server Time</b>\n$(date)",
  35. "disable_web_page_preview": "yes"
  36. }
  37. EOF
  38. )
  39. fn_print_dots "Sending Telegram alert"
  40. curlcustomargs=()
  41. if [ -n "${curlcustomstring}" ]; then
  42. read -r -a curlcustomargs <<< "${curlcustomstring}"
  43. fi
  44. telegramsend=$(curl --connect-timeout 3 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${curlcustomargs[@]}" "https://${telegramapi}/bot${telegramtoken}/sendMessage" | grep "error_code")
  45. if [ -n "${telegramsend}" ]; then
  46. fn_print_fail_nl "Sending Telegram alert: ${telegramsend}"
  47. fn_script_log_fail "Sending Telegram alert: ${telegramsend}"
  48. else
  49. fn_print_ok_nl "Sending Telegram alert"
  50. fn_script_log_pass "Sent Telegram alert"
  51. fi