alert_pushbullet.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/bin/bash
  2. # LinuxGSM alert_pushbullet.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://gameservermanagers.com
  5. # Description: Sends Pushbullet alert including the server status.
  6. local commandname="ALERT"
  7. local commandaction="Alert"
  8. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  9. # converts text to ascii then passes to curl. allowing special characters to be sent e.g %
  10. # http://stackoverflow.com/a/10660730
  11. fn_rawurlencode() {
  12. local string="${1}"
  13. local strlen=${#string}
  14. local encoded=""
  15. local pos c o
  16. for (( pos=0 ; pos<strlen ; pos++ )); do
  17. c=${string:$pos:1}
  18. case "$c" in
  19. [-_.~a-zA-Z0-9] ) o="${c}" ;;
  20. * ) printf -v o '%%%02x' "'$c"
  21. esac
  22. encoded+="${o}"
  23. done
  24. echo "${encoded}" # You can either set a return variable (FASTER)
  25. REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
  26. }
  27. pbalertbody=$(fn_rawurlencode "${alertbody}"; echo ${REPLY})
  28. pbalertsubject=$(fn_rawurlencode "${alertsubject}"; echo ${REPLY})
  29. fn_print_dots "Sending Pushbullet alert"
  30. sleep 1
  31. pushbulletsend=$(curl --silent -u """${pushbullettoken}"":" -d channel_tag="${channeltag}" -d type="note" -d body="${pbalertbody}" -d title="${pbalertsubject}" 'https://api.pushbullet.com/v2/pushes'|grep -o invalid_access_token|uniq)
  32. if [ "${pushbulletsend}" == "invalid_access_token" ]; then
  33. fn_print_fail_nl "Sending Pushbullet alert: invalid_access_token"
  34. fn_script_log_fatal "Sending Pushbullet alert: invalid_access_token"
  35. else
  36. fn_print_ok_nl "Sending Pushbullet alert"
  37. fn_script_log_pass "Sent Pushbullet alert"
  38. fi