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.

79 lines
2.2 KiB

  1. #!/usr/bin/env sh
  2. #Support Microsoft Teams webhooks
  3. #TEAMS_WEBHOOK_URL=""
  4. #TEAMS_THEME_COLOR=""
  5. #TEAMS_SUCCESS_COLOR=""
  6. #TEAMS_ERROR_COLOR=""
  7. #TEAMS_SKIP_COLOR=""
  8. teams_send() {
  9. _subject="$1"
  10. _content="$2"
  11. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  12. _debug "_statusCode" "$_statusCode"
  13. TEAMS_WEBHOOK_URL="${TEAMS_WEBHOOK_URL:-$(_readaccountconf_mutable TEAMS_WEBHOOK_URL)}"
  14. if [ -z "$TEAMS_WEBHOOK_URL" ]; then
  15. TEAMS_WEBHOOK_URL=""
  16. _err "You didn't specify a Microsoft Teams webhook url TEAMS_WEBHOOK_URL yet."
  17. return 1
  18. fi
  19. _saveaccountconf_mutable TEAMS_WEBHOOK_URL "$TEAMS_WEBHOOK_URL"
  20. TEAMS_THEME_COLOR="${TEAMS_THEME_COLOR:-$(_readaccountconf_mutable TEAMS_THEME_COLOR)}"
  21. if [ -n "$TEAMS_THEME_COLOR" ]; then
  22. _saveaccountconf_mutable TEAMS_THEME_COLOR "$TEAMS_THEME_COLOR"
  23. fi
  24. TEAMS_SUCCESS_COLOR="${TEAMS_SUCCESS_COLOR:-$(_readaccountconf_mutable TEAMS_SUCCESS_COLOR)}"
  25. if [ -n "$TEAMS_SUCCESS_COLOR" ]; then
  26. _saveaccountconf_mutable TEAMS_SUCCESS_COLOR "$TEAMS_SUCCESS_COLOR"
  27. fi
  28. TEAMS_ERROR_COLOR="${TEAMS_ERROR_COLOR:-$(_readaccountconf_mutable TEAMS_ERROR_COLOR)}"
  29. if [ -n "$TEAMS_ERROR_COLOR" ]; then
  30. _saveaccountconf_mutable TEAMS_ERROR_COLOR "$TEAMS_ERROR_COLOR"
  31. fi
  32. TEAMS_SKIP_COLOR="${TEAMS_SKIP_COLOR:-$(_readaccountconf_mutable TEAMS_SKIP_COLOR)}"
  33. if [ -n "$TEAMS_SKIP_COLOR" ]; then
  34. _saveaccountconf_mutable TEAMS_SKIP_COLOR "$TEAMS_SKIP_COLOR"
  35. fi
  36. export _H1="Content-Type: application/json"
  37. _subject=$(echo "$_subject" | _json_encode)
  38. _content=$(echo "$_content" | _json_encode)
  39. case "$_statusCode" in
  40. 0)
  41. _color="$TEAMS_SUCCESS_COLOR"
  42. ;;
  43. 1)
  44. _color="$TEAMS_ERROR_COLOR"
  45. ;;
  46. 2)
  47. _color="$TEAMS_SKIP_COLOR"
  48. ;;
  49. esac
  50. _color="$(echo "${_color:-$TEAMS_THEME_COLOR}" | tr -cd '[:xdigit:]')"
  51. _data="{\"title\": \"$_subject\","
  52. if [ -n "$_color" ]; then
  53. _data="$_data\"themeColor\": \"$_color\", "
  54. fi
  55. _data="$_data\"text\": \"$_content\"}"
  56. if _post "$_data" "$TEAMS_WEBHOOK_URL"; then
  57. # shellcheck disable=SC2154
  58. if ! _contains "$response" error; then
  59. _info "teams send success."
  60. return 0
  61. fi
  62. fi
  63. _err "teams send error."
  64. _err "$response"
  65. return 1
  66. }