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.

52 lines
1.8 KiB

1 year ago
  1. #!/usr/bin/env sh
  2. # Support mattermost bots
  3. #MATTERMOST_API_URL=""
  4. #MATTERMOST_CHANNEL_ID=""
  5. #MATTERMOST_BOT_TOKEN=""
  6. mattermost_send() {
  7. _subject="$1"
  8. _content="$2"
  9. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  10. _debug "_statusCode" "$_statusCode"
  11. MATTERMOST_API_URL="${MATTERMOST_API_URL:-$(_readaccountconf_mutable MATTERMOST_API_URL)}"
  12. if [ -z "$MATTERMOST_API_URL" ]; then
  13. _err "You didn't specify a Mattermost API URL MATTERMOST_API_URL yet."
  14. return 1
  15. fi
  16. _saveaccountconf_mutable MATTERMOST_API_URL "$MATTERMOST_API_URL"
  17. MATTERMOST_CHANNEL_ID="${MATTERMOST_CHANNEL_ID:-$(_readaccountconf_mutable MATTERMOST_CHANNEL_ID)}"
  18. if [ -z "$MATTERMOST_CHANNEL_ID" ]; then
  19. _err "You didn't specify a Mattermost channel id MATTERMOST_CHANNEL_ID yet."
  20. return 1
  21. fi
  22. _saveaccountconf_mutable MATTERMOST_CHANNEL_ID "$MATTERMOST_CHANNEL_ID"
  23. MATTERMOST_BOT_TOKEN="${MATTERMOST_BOT_TOKEN:-$(_readaccountconf_mutable MATTERMOST_BOT_TOKEN)}"
  24. if [ -z "$MATTERMOST_BOT_TOKEN" ]; then
  25. _err "You didn't specify a Mattermost bot API token MATTERMOST_BOT_TOKEN yet."
  26. return 1
  27. fi
  28. _saveaccountconf_mutable MATTERMOST_BOT_TOKEN "$MATTERMOST_BOT_TOKEN"
  29. _content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)"
  30. _data="{\"channel_id\": \"$MATTERMOST_CHANNEL_ID\", "
  31. _data="$_data\"message\": \"$_content\"}"
  32. export _H1="Authorization: Bearer $MATTERMOST_BOT_TOKEN"
  33. response=""
  34. if _post "$_data" "$MATTERMOST_API_URL" "" "POST" "application/json; charset=utf-8"; then
  35. MATTERMOST_RESULT_OK=$(echo "$response" | _egrep_o 'create_at')
  36. if [ "$?" = "0" ] && [ "$MATTERMOST_RESULT_OK" ]; then
  37. _info "mattermost send success."
  38. return 0
  39. fi
  40. fi
  41. _err "mattermost send error."
  42. _err "$response"
  43. return 1
  44. }