You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.0 KiB

  1. #!/usr/bin/env sh
  2. #Support ifttt.com webhooks api
  3. #IFTTT_API_KEY="xxxx"
  4. #IFTTT_EVENT_NAME="yyyy"
  5. #IFTTT_SUBJECT_KEY="value1|value2|value3" #optional, use "value1" as default
  6. #IFTTT_CONTENT_KEY="value1|value2|value3" #optional, use "value2" as default
  7. _IFTTT_AVAIL_MSG_KEYS="value1,value2,value3"
  8. # subject content statusCode
  9. ifttt_send() {
  10. _subject="$1"
  11. _content="$2"
  12. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  13. _debug "_subject" "$_subject"
  14. _debug "_content" "$_content"
  15. _debug "_statusCode" "$_statusCode"
  16. IFTTT_API_KEY="${IFTTT_API_KEY:-$(_readaccountconf_mutable IFTTT_API_KEY)}"
  17. if [ -z "$IFTTT_API_KEY" ]; then
  18. IFTTT_API_KEY=""
  19. _err "You didn't specify a ifttt webhooks api key IFTTT_API_KEY yet."
  20. _err "You can get yours from https://ifttt.com"
  21. return 1
  22. fi
  23. _saveaccountconf_mutable IFTTT_API_KEY "$IFTTT_API_KEY"
  24. IFTTT_EVENT_NAME="${IFTTT_EVENT_NAME:-$(_readaccountconf_mutable IFTTT_EVENT_NAME)}"
  25. if [ -z "$IFTTT_EVENT_NAME" ]; then
  26. IFTTT_EVENT_NAME=""
  27. _err "You didn't specify a ifttt webhooks event name IFTTT_EVENT_NAME yet."
  28. return 1
  29. fi
  30. _saveaccountconf_mutable IFTTT_EVENT_NAME "$IFTTT_EVENT_NAME"
  31. IFTTT_SUBJECT_KEY="${IFTTT_SUBJECT_KEY:-$(_readaccountconf_mutable IFTTT_SUBJECT_KEY)}"
  32. if [ -z "$IFTTT_SUBJECT_KEY" ]; then
  33. IFTTT_SUBJECT_KEY="value1"
  34. _info "The IFTTT_SUBJECT_KEY is not set, so use the default value1 as key."
  35. elif ! _hasfield "$_IFTTT_AVAIL_MSG_KEYS" "$IFTTT_SUBJECT_KEY"; then
  36. _err "The IFTTT_SUBJECT_KEY \"$IFTTT_SUBJECT_KEY\" is not available, should be one of $_IFTTT_AVAIL_MSG_KEYS"
  37. IFTTT_SUBJECT_KEY=""
  38. return 1
  39. else
  40. _saveaccountconf_mutable IFTTT_SUBJECT_KEY "$IFTTT_SUBJECT_KEY"
  41. fi
  42. IFTTT_CONTENT_KEY="${IFTTT_CONTENT_KEY:-$(_readaccountconf_mutable IFTTT_CONTENT_KEY)}"
  43. if [ -z "$IFTTT_CONTENT_KEY" ]; then
  44. IFTTT_CONTENT_KEY="value2"
  45. _info "The IFTTT_CONTENT_KEY is not set, so use the default value2 as key."
  46. elif ! _hasfield "$_IFTTT_AVAIL_MSG_KEYS" "$IFTTT_CONTENT_KEY"; then
  47. _err "The IFTTT_CONTENT_KEY \"$IFTTT_CONTENT_KEY\" is not available, should be one of $_IFTTT_AVAIL_MSG_KEYS"
  48. IFTTT_CONTENT_KEY=""
  49. return 1
  50. else
  51. _saveaccountconf_mutable IFTTT_CONTENT_KEY "$IFTTT_CONTENT_KEY"
  52. fi
  53. if [ "$IFTTT_SUBJECT_KEY" = "$IFTTT_CONTENT_KEY" ]; then
  54. IFTTT_SUBJECT_KEY=""
  55. IFTTT_CONTENT_KEY=""
  56. _err "The IFTTT_SUBJECT_KEY must not be same as IFTTT_CONTENT_KEY."
  57. return 1
  58. fi
  59. IFTTT_API_URL="https://maker.ifttt.com/trigger/$IFTTT_EVENT_NAME/with/key/$IFTTT_API_KEY"
  60. _content=$(echo "$_content" | _json_encode)
  61. _subject=$(echo "$_subject" | _json_encode)
  62. _data="{\"$IFTTT_SUBJECT_KEY\": \"$_subject\", \"$IFTTT_CONTENT_KEY\": \"$_content\"}"
  63. response="" #just make shellcheck happy
  64. if _post "$_data" "$IFTTT_API_URL" "" "POST" "application/json"; then
  65. if _contains "$response" "Congratulations"; then
  66. _info "IFTTT webhooks event fired success."
  67. return 0
  68. fi
  69. fi
  70. _err "IFTTT webhooks event fired error."
  71. _err "$response"
  72. return 1
  73. }