-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4622 from defragatwork/mattermost
Add support for Mattermost notifications (Bot account)
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env sh | ||
|
||
# Support mattermost bots | ||
|
||
#MATTERMOST_API_URL="" | ||
#MATTERMOST_CHANNEL_ID="" | ||
#MATTERMOST_BOT_TOKEN="" | ||
|
||
mattermost_send() { | ||
_subject="$1" | ||
_content="$2" | ||
_statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped | ||
_debug "_statusCode" "$_statusCode" | ||
|
||
MATTERMOST_API_URL="${MATTERMOST_API_URL:-$(_readaccountconf_mutable MATTERMOST_API_URL)}" | ||
if [ -z "$MATTERMOST_API_URL" ]; then | ||
_err "You didn't specify a Mattermost API URL MATTERMOST_API_URL yet." | ||
return 1 | ||
fi | ||
_saveaccountconf_mutable MATTERMOST_API_URL "$MATTERMOST_API_URL" | ||
|
||
MATTERMOST_CHANNEL_ID="${MATTERMOST_CHANNEL_ID:-$(_readaccountconf_mutable MATTERMOST_CHANNEL_ID)}" | ||
if [ -z "$MATTERMOST_CHANNEL_ID" ]; then | ||
_err "You didn't specify a Mattermost channel id MATTERMOST_CHANNEL_ID yet." | ||
return 1 | ||
fi | ||
_saveaccountconf_mutable MATTERMOST_CHANNEL_ID "$MATTERMOST_CHANNEL_ID" | ||
|
||
MATTERMOST_BOT_TOKEN="${MATTERMOST_BOT_TOKEN:-$(_readaccountconf_mutable MATTERMOST_BOT_TOKEN)}" | ||
if [ -z "$MATTERMOST_BOT_TOKEN" ]; then | ||
_err "You didn't specify a Mattermost bot API token MATTERMOST_BOT_TOKEN yet." | ||
return 1 | ||
fi | ||
_saveaccountconf_mutable MATTERMOST_BOT_TOKEN "$MATTERMOST_BOT_TOKEN" | ||
|
||
_content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)" | ||
_data="{\"channel_id\": \"$MATTERMOST_CHANNEL_ID\", " | ||
_data="$_data\"message\": \"$_content\"}" | ||
|
||
export _H1="Authorization: Bearer $MATTERMOST_BOT_TOKEN" | ||
|
||
if _post "$_data" "$MATTERMOST_API_URL" "" "POST" "application/json; charset=utf-8"; then | ||
MATTERMOST_RESULT_OK=$(echo "$response" | _egrep_o 'create_at') | ||
if [ "$?" = "0" ] && [ "$MATTERMOST_RESULT_OK" ]; then | ||
_info "mattermost send success." | ||
return 0 | ||
fi | ||
fi | ||
_err "mattermost send error." | ||
_err "$response" | ||
return 1 | ||
} |