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.

57 lines
1.6 KiB

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