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.

45 lines
1.3 KiB

  1. #!/usr/bin/env sh
  2. #Support Slack APP notifications
  3. #SLACK_APP_CHANNEL=""
  4. #SLACK_APP_TOKEN=""
  5. slack_app_send() {
  6. _subject="$1"
  7. _content="$2"
  8. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  9. _debug "_statusCode" "$_statusCode"
  10. SLACK_APP_CHANNEL="${SLACK_APP_CHANNEL:-$(_readaccountconf_mutable SLACK_APP_CHANNEL)}"
  11. if [ -n "$SLACK_APP_CHANNEL" ]; then
  12. _saveaccountconf_mutable SLACK_APP_CHANNEL "$SLACK_APP_CHANNEL"
  13. fi
  14. SLACK_APP_TOKEN="${SLACK_APP_TOKEN:-$(_readaccountconf_mutable SLACK_APP_TOKEN)}"
  15. if [ -n "$SLACK_APP_TOKEN" ]; then
  16. _saveaccountconf_mutable SLACK_APP_TOKEN "$SLACK_APP_TOKEN"
  17. fi
  18. _content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)"
  19. _data="{\"text\": \"$_content\", "
  20. if [ -n "$SLACK_APP_CHANNEL" ]; then
  21. _data="$_data\"channel\": \"$SLACK_APP_CHANNEL\", "
  22. fi
  23. _data="$_data\"mrkdwn\": \"true\"}"
  24. export _H1="Authorization: Bearer $SLACK_APP_TOKEN"
  25. SLACK_APP_API_URL="https://slack.com/api/chat.postMessage"
  26. if _post "$_data" "$SLACK_APP_API_URL" "" "POST" "application/json; charset=utf-8"; then
  27. # shellcheck disable=SC2154
  28. SLACK_APP_RESULT_OK=$(echo "$response" | _egrep_o 'ok" *: *true')
  29. if [ "$?" = "0" ] && [ "$SLACK_APP_RESULT_OK" ]; then
  30. _info "slack send success."
  31. return 0
  32. fi
  33. fi
  34. _err "slack send error."
  35. _err "$response"
  36. return 1
  37. }