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.

55 lines
1.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. #Support Slack webhooks
  3. #SLACK_WEBHOOK_URL=""
  4. #SLACK_CHANNEL=""
  5. #SLACK_USERNAME=""
  6. slack_send() {
  7. _subject="$1"
  8. _content="$2"
  9. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  10. _debug "_statusCode" "$_statusCode"
  11. SLACK_WEBHOOK_URL="${SLACK_WEBHOOK_URL:-$(_readaccountconf_mutable SLACK_WEBHOOK_URL)}"
  12. if [ -z "$SLACK_WEBHOOK_URL" ]; then
  13. SLACK_WEBHOOK_URL=""
  14. _err "You didn't specify a Slack webhook url SLACK_WEBHOOK_URL yet."
  15. return 1
  16. fi
  17. _saveaccountconf_mutable SLACK_WEBHOOK_URL "$SLACK_WEBHOOK_URL"
  18. SLACK_CHANNEL="${SLACK_CHANNEL:-$(_readaccountconf_mutable SLACK_CHANNEL)}"
  19. if [ -n "$SLACK_CHANNEL" ]; then
  20. _saveaccountconf_mutable SLACK_CHANNEL "$SLACK_CHANNEL"
  21. fi
  22. SLACK_USERNAME="${SLACK_USERNAME:-$(_readaccountconf_mutable SLACK_USERNAME)}"
  23. if [ -n "$SLACK_USERNAME" ]; then
  24. _saveaccountconf_mutable SLACK_USERNAME "$SLACK_USERNAME"
  25. fi
  26. export _H1="Content-Type: application/json"
  27. _content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)"
  28. _data="{\"text\": \"$_content\", "
  29. if [ -n "$SLACK_CHANNEL" ]; then
  30. _data="$_data\"channel\": \"$SLACK_CHANNEL\", "
  31. fi
  32. if [ -n "$SLACK_USERNAME" ]; then
  33. _data="$_data\"username\": \"$SLACK_USERNAME\", "
  34. fi
  35. _data="$_data\"mrkdwn\": \"true\"}"
  36. if _post "$_data" "$SLACK_WEBHOOK_URL"; then
  37. # shellcheck disable=SC2154
  38. if [ "$response" = "ok" ]; then
  39. _info "slack send success."
  40. return 0
  41. fi
  42. fi
  43. _err "slack send error."
  44. _err "$response"
  45. return 1
  46. }