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.

69 lines
2.2 KiB

  1. #!/usr/bin/env sh
  2. #Support dingtalk webhooks api
  3. #DINGTALK_WEBHOOK="xxxx"
  4. #optional
  5. #DINGTALK_KEYWORD="yyyy"
  6. #DINGTALK_SIGNING_KEY="SEC08ffdbd403cbc3fc8a65xxxxxxxxxxxxxxxxxxxx"
  7. # subject content statusCode
  8. dingtalk_send() {
  9. _subject="$1"
  10. _content="$2"
  11. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  12. _debug "_subject" "$_subject"
  13. _debug "_content" "$_content"
  14. _debug "_statusCode" "$_statusCode"
  15. DINGTALK_WEBHOOK="${DINGTALK_WEBHOOK:-$(_readaccountconf_mutable DINGTALK_WEBHOOK)}"
  16. if [ -z "$DINGTALK_WEBHOOK" ]; then
  17. DINGTALK_WEBHOOK=""
  18. _err "You didn't specify a dingtalk webhooks DINGTALK_WEBHOOK yet."
  19. _err "You can get yours from https://dingtalk.com"
  20. return 1
  21. fi
  22. _saveaccountconf_mutable DINGTALK_WEBHOOK "$DINGTALK_WEBHOOK"
  23. DINGTALK_KEYWORD="${DINGTALK_KEYWORD:-$(_readaccountconf_mutable DINGTALK_KEYWORD)}"
  24. if [ "$DINGTALK_KEYWORD" ]; then
  25. _saveaccountconf_mutable DINGTALK_KEYWORD "$DINGTALK_KEYWORD"
  26. fi
  27. # DINGTALK_SIGNING_KEY="${DINGTALK_SIGNING_KEY:-$(_readaccountconf_mutable DINGTALK_SIGNING_KEY)}"
  28. # if [ -z "$DINGTALK_SIGNING_KEY" ]; then
  29. # DINGTALK_SIGNING_KEY="value1"
  30. # _info "The DINGTALK_SIGNING_KEY is not set, so use the default value1 as key."
  31. # elif ! _hasfield "$_IFTTT_AVAIL_MSG_KEYS" "$DINGTALK_SIGNING_KEY"; then
  32. # _err "The DINGTALK_SIGNING_KEY \"$DINGTALK_SIGNING_KEY\" is not available, should be one of $_IFTTT_AVAIL_MSG_KEYS"
  33. # DINGTALK_SIGNING_KEY=""
  34. # return 1
  35. # else
  36. # _saveaccountconf_mutable DINGTALK_SIGNING_KEY "$DINGTALK_SIGNING_KEY"
  37. # fi
  38. # if [ "$DINGTALK_SIGNING_KEY" = "$IFTTT_CONTENT_KEY" ]; then
  39. # DINGTALK_SIGNING_KEY=""
  40. # IFTTT_CONTENT_KEY=""
  41. # _err "The DINGTALK_SIGNING_KEY must not be same as IFTTT_CONTENT_KEY."
  42. # return 1
  43. # fi
  44. _content=$(echo "$_content" | _json_encode)
  45. _subject=$(echo "$_subject" | _json_encode)
  46. _data="{\"msgtype\": \"text\", \"text\": {\"content\": \"[$DINGTALK_KEYWORD]\n$_subject\n$_content\"}}"
  47. response="$(_post "$_data" "$DINGTALK_WEBHOOK" "" "POST" "application/json")"
  48. if [ "$?" = "0" ] && _contains "$response" "errmsg\":\"ok"; then
  49. _info "dingtalk webhooks event fired success."
  50. return 0
  51. fi
  52. _err "dingtalk webhooks event fired error."
  53. _err "$response"
  54. return 1
  55. }