forked from openrelayxyz/plugeth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-post.sh
executable file
·87 lines (73 loc) · 1.94 KB
/
slack-post.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env bash
# Usage: slackpost -w <webhook_url> -c <channel> -u <username> -m <message> [-a <alert_type>]
# Usage: echo <message> | slackpost -w <webhook_url> -c <channel> -u <username> [-a <alert_type>]
# exit immediately if a command exits with a non-zero status
set -e
# error if variable referenced before being set
set -u
# produce failure return code if any command fails in pipe
set -o pipefail
# accepted values: good, warning, danger
alert_type=""
channel=""
message=""
username=""
webhook_url=""
# colon after var means it has a value rather than it being a bool flag
while getopts 'a:c:m:u:w:' OPTION; do
case "$OPTION" in
a)
alert_type="$OPTARG"
;;
c)
channel="$OPTARG"
;;
m)
message="$OPTARG"
;;
u)
username="$OPTARG"
;;
w)
webhook_url="$OPTARG"
;;
?)
echo "script usage: $(basename $0) {-c channel} {-m message} {-u username} {-w webhook} [-a alert_type]" >&2
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
# # exit if channel not provided
# if [[ -z "$channel" ]]
# then
# echo "No channel specified"
# exit 1
# fi
# read piped data as message if message argument is not provided
if [[ -z "$message" ]]
then
message=$*
while IFS= read -r line; do
message="$message$line\n"
done
fi
# # exit if username not provided
# if [[ -z "$username" ]]
# then
# echo "No username specified"
# exit 1
# fi
# exit if webhook not provided
if [[ -z "$webhook_url" ]]
then
echo "No webhook_url specified"
exit 1
fi
# escape message text
escapedText=$(echo $message | sed 's/"/\"/g' | sed "s/'/\'/g")
# create JSON payload
# json="{\"channel\": \"$channel\", \"username\":\"$username\", \"icon_emoji\":\"ghost\", \"attachments\":[{\"color\":\"$alert_type\" , \"text\": \"$escapedText\"}]}"
json="{\"attachments\":[{\"color\":\"$alert_type\" , \"text\": \"$escapedText\"}]}"
# fire off slack message post
curl -s -d "payload=$json" "$webhook_url"