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.

81 lines
2.1 KiB

4 years ago
  1. #!/usr/bin/env sh
  2. #Support Microsoft Teams webhooks
  3. #TEAMS_WEBHOOK_URL=""
  4. teams_send() {
  5. _subject="$1"
  6. _content="$2"
  7. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  8. _debug "_statusCode" "$_statusCode"
  9. _color_success="Good"
  10. _color_danger="Attention"
  11. _color_muted="Accent"
  12. TEAMS_WEBHOOK_URL="${TEAMS_WEBHOOK_URL:-$(_readaccountconf_mutable TEAMS_WEBHOOK_URL)}"
  13. if [ -z "$TEAMS_WEBHOOK_URL" ]; then
  14. TEAMS_WEBHOOK_URL=""
  15. _err "You didn't specify a Microsoft Teams webhook url TEAMS_WEBHOOK_URL yet."
  16. return 1
  17. fi
  18. _saveaccountconf_mutable TEAMS_WEBHOOK_URL "$TEAMS_WEBHOOK_URL"
  19. export _H1="Content-Type: application/json"
  20. _subject=$(echo "$_subject" | _json_encode)
  21. _content=$(echo "$_content" | _json_encode)
  22. case "$_statusCode" in
  23. 0)
  24. _color="${TEAMS_SUCCESS_COLOR:-$_color_success}"
  25. ;;
  26. 1)
  27. _color="${TEAMS_ERROR_COLOR:-$_color_danger}"
  28. ;;
  29. 2)
  30. _color="${TEAMS_SKIP_COLOR:-$_color_muted}"
  31. ;;
  32. esac
  33. _data="{
  34. \"type\": \"message\",
  35. \"attachments\": [
  36. {
  37. \"contentType\": \"application/vnd.microsoft.card.adaptive\",
  38. \"contentUrl\": null,
  39. \"content\": {
  40. \"$schema\": \"http://adaptivecards.io/schemas/adaptive-card.json\",
  41. \"type\": \"AdaptiveCard\",
  42. \"version\": \"1.2\",
  43. \"body\": [
  44. {
  45. \"type\": \"TextBlock\",
  46. \"size\": \"large\",
  47. \"weight\": \"bolder\",
  48. \"wrap\": true,
  49. \"color\": \"$_color\",
  50. \"text\": \"$_subject\"
  51. },
  52. {
  53. \"type\": \"TextBlock\",
  54. \"text\": \"$_content\",
  55. \"wrap\": true
  56. }
  57. ]
  58. }
  59. }
  60. ]
  61. }"
  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. }