]>
Commit | Line | Data |
---|---|---|
7625d662 HH |
1 | #!/usr/bin/env sh |
2 | ||
3 | #Support Slack webhooks | |
4 | ||
5 | #SLACK_WEBHOOK_URL="" | |
73bbe25d HH |
6 | #SLACK_CHANNEL="" |
7 | #SLACK_USERNAME="" | |
7625d662 HH |
8 | |
9 | slack_send() { | |
10 | _subject="$1" | |
11 | _content="$2" | |
12 | _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped | |
13 | _debug "_statusCode" "$_statusCode" | |
14 | ||
15 | SLACK_WEBHOOK_URL="${SLACK_WEBHOOK_URL:-$(_readaccountconf_mutable SLACK_WEBHOOK_URL)}" | |
16 | if [ -z "$SLACK_WEBHOOK_URL" ]; then | |
17 | SLACK_WEBHOOK_URL="" | |
18 | _err "You didn't specify a Slack webhook url SLACK_WEBHOOK_URL yet." | |
19 | return 1 | |
20 | fi | |
21 | _saveaccountconf_mutable SLACK_WEBHOOK_URL "$SLACK_WEBHOOK_URL" | |
22 | ||
73bbe25d HH |
23 | SLACK_CHANNEL="${SLACK_CHANNEL:-$(_readaccountconf_mutable SLACK_CHANNEL)}" |
24 | if [ -n "$SLACK_CHANNEL" ]; then | |
25 | _saveaccountconf_mutable SLACK_CHANNEL "$SLACK_CHANNEL" | |
26 | fi | |
27 | ||
28 | SLACK_USERNAME="${SLACK_USERNAME:-$(_readaccountconf_mutable SLACK_USERNAME)}" | |
29 | if [ -n "$SLACK_USERNAME" ]; then | |
30 | _saveaccountconf_mutable SLACK_USERNAME "$SLACK_USERNAME" | |
31 | fi | |
7625d662 | 32 | |
73bbe25d | 33 | export _H1="Content-Type: application/json" |
7625d662 | 34 | |
73bbe25d HH |
35 | _content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)" |
36 | _data="{\"text\": \"$_content\", " | |
37 | if [ -n "$SLACK_CHANNEL" ]; then | |
38 | _data="$_data\"channel\": \"$SLACK_CHANNEL\", " | |
39 | fi | |
40 | if [ -n "$SLACK_USERNAME" ]; then | |
41 | _data="$_data\"username\": \"$SLACK_USERNAME\", " | |
42 | fi | |
43 | _data="$_data\"mrkdwn\": \"true\"}" | |
7625d662 HH |
44 | |
45 | if _post "$_data" "$SLACK_WEBHOOK_URL"; then | |
46 | # shellcheck disable=SC2154 | |
73bbe25d HH |
47 | if [ "$response" = "ok" ]; then |
48 | _info "slack send success." | |
7625d662 HH |
49 | return 0 |
50 | fi | |
51 | fi | |
52 | _err "slack send error." | |
53 | _err "$response" | |
54 | return 1 | |
7625d662 | 55 | } |