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
2.4 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. _color_success="2cbe4e" # green
  14. _color_danger="cb2431" # red
  15. _color_muted="586069" # gray
  16. TEAMS_WEBHOOK_URL="${TEAMS_WEBHOOK_URL:-$(_readaccountconf_mutable TEAMS_WEBHOOK_URL)}"
  17. if [ -z "$TEAMS_WEBHOOK_URL" ]; then
  18. TEAMS_WEBHOOK_URL=""
  19. _err "You didn't specify a Microsoft Teams webhook url TEAMS_WEBHOOK_URL yet."
  20. return 1
  21. fi
  22. _saveaccountconf_mutable TEAMS_WEBHOOK_URL "$TEAMS_WEBHOOK_URL"
  23. TEAMS_THEME_COLOR="${TEAMS_THEME_COLOR:-$(_readaccountconf_mutable TEAMS_THEME_COLOR)}"
  24. if [ -n "$TEAMS_THEME_COLOR" ]; then
  25. _saveaccountconf_mutable TEAMS_THEME_COLOR "$TEAMS_THEME_COLOR"
  26. fi
  27. TEAMS_SUCCESS_COLOR="${TEAMS_SUCCESS_COLOR:-$(_readaccountconf_mutable TEAMS_SUCCESS_COLOR)}"
  28. if [ -n "$TEAMS_SUCCESS_COLOR" ]; then
  29. _saveaccountconf_mutable TEAMS_SUCCESS_COLOR "$TEAMS_SUCCESS_COLOR"
  30. fi
  31. TEAMS_ERROR_COLOR="${TEAMS_ERROR_COLOR:-$(_readaccountconf_mutable TEAMS_ERROR_COLOR)}"
  32. if [ -n "$TEAMS_ERROR_COLOR" ]; then
  33. _saveaccountconf_mutable TEAMS_ERROR_COLOR "$TEAMS_ERROR_COLOR"
  34. fi
  35. TEAMS_SKIP_COLOR="${TEAMS_SKIP_COLOR:-$(_readaccountconf_mutable TEAMS_SKIP_COLOR)}"
  36. if [ -n "$TEAMS_SKIP_COLOR" ]; then
  37. _saveaccountconf_mutable TEAMS_SKIP_COLOR "$TEAMS_SKIP_COLOR"
  38. fi
  39. export _H1="Content-Type: application/json"
  40. _subject=$(echo "$_subject" | _json_encode)
  41. _content=$(echo "$_content" | _json_encode)
  42. case "$_statusCode" in
  43. 0)
  44. _color="${TEAMS_SUCCESS_COLOR:-$_color_success}"
  45. ;;
  46. 1)
  47. _color="${TEAMS_ERROR_COLOR:-$_color_danger}"
  48. ;;
  49. 2)
  50. _color="${TEAMS_SKIP_COLOR:-$_color_muted}"
  51. ;;
  52. esac
  53. _color=$(echo "$_color" | tr -cd 'a-fA-F0-9')
  54. if [ -z "$_color" ]; then
  55. _color=$(echo "${TEAMS_THEME_COLOR:-$_color_muted}" | tr -cd 'a-fA-F0-9')
  56. fi
  57. _data="{\"title\": \"$_subject\","
  58. if [ -n "$_color" ]; then
  59. _data="$_data\"themeColor\": \"$_color\", "
  60. fi
  61. _data="$_data\"text\": \"$_content\"}"
  62. if response=$(_post "$_data" "$TEAMS_WEBHOOK_URL"); then
  63. if ! _contains "$response" error; then
  64. _info "teams send success."
  65. return 0
  66. fi
  67. fi
  68. _err "teams send error."
  69. _err "$response"
  70. return 1
  71. }