-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.sh
executable file
·142 lines (128 loc) · 3.85 KB
/
slack.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-w|--webhook)
url="$2"
shift # past argument
shift # past value
;;
-m|--message)
message="$2"
shift # past argument
shift # past value
;;
-s|--severity)
severity="$2"
shift # past argument
shift # past value
;;
-t|--title)
title="$2"
shift # past argument
shift # past value
;;
-d|--description)
description="$2"
shift # past argument
shift # past value
;;
-u|--username)
username_from_params="$2"
shift # past argument
shift # past value
;;
-c|--color)
color_name="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
if [ ${#POSITIONAL[@]} -eq 0 ]; then
echo "WEB HOOK = ${url}"
echo "USER NAME = ${username_from_params}"
echo "SEVERITY = ${severity}"
echo "MESSAGE = ${message}"
echo "TITLE = ${title}"
echo "DESCRIPTION = ${description}"
echo "COLOR = ${color_name}"
elif [[ " ${POSITIONAL[@]} " =~ "-h" ]]
then
echo "
Please use following arguments:
--webhook (-w) Web-hook URL
--message (-m) Message text
--severity (-s) Trigger severity (1-5)
--title (-t) Message title
--description (-d) Message description
--username (-u) Username to be displayed
--color (-c) Message color: blue, yellow, orange, light_red, red, green
"
exit 1
else
echo "Unknown argument: \"$POSITIONAL\""
echo "
Please use following arguments:
--webhook (-w) Web-hook URL
--message (-m) Message text
--severity (-s) Numerical trigger severity. Possible values: 0 - Not classified, 1 - Information, 2 - Warning, 3 - Average, 4 - High, 5 - Disaster.
--title (-t) Message title
--description (-d) Message description
--username (-u) Username to be displayed
--color (-c) Message color: blue, yellow, orange, light_red, red, green, purple
"
exit 1
fi
# Username
default_username='Incoming WebHooks Script'
username=${username_from_params:-"${default_username}"}
# Colors
blue='#7499FF'
yellow='#FFC859'
orange='#FFA059'
light_red='#E97659'
red='#E45959'
green='#118919'
purple='#FF38C4'
[[ "$title" =~ ^Resolved.* ]] && color=${green}
[[ "$title" =~ ^Information.* ]] && color=${blue}
[[ "$title" =~ ^Warning.* ]] && color=${yellow}
[[ "$title" =~ ^Average.* ]] && color=${orange}
[[ "$title" =~ ^High.* ]] && color=${light_red}
[[ "$title" =~ ^Disaster.* ]] && color=${red}
[[ "$title" =~ ^Acknowledged.* ]] && color=${purple}
[ "$severity" == '0' ] && color=''
[ "$severity" == '1' ] && color=${blue}
[ "$severity" == '2' ] && color=${yellow}
[ "$severity" == '3' ] && color=${orange}
[ "$severity" == '4' ] && color=${light_red}
[ "$severity" == '5' ] && color=${red}
[ "$color_name" == 'blue' ] && color=${blue}
[ "$color_name" == 'yellow' ] && color=${yellow}
[ "$color_name" == 'orange' ] && color=${orange}
[ "$color_name" == 'light_red' ] && color=${light_red}
[ "$color_name" == 'red' ] && color=${red}
[ "$color_name" == 'green' ] && color=${green}
payload="payload={
\"username\":\"${username//\"/\\\"}\",
\"attachments\":[{
\"fallback\":\"${message//\"/\\\"}\",
\"pretext\":\"${message//\"/\\\"}\",
\"color\":\"${color//\"/\\\"}\",
\"fields\":[{
\"title\":\"${title//\"/\\\"}\",
\"value\":\"${description//\"/\\\"}\",
\"short\":false
}]}]}\"}"
result=`curl -m 5 --data-urlencode "${payload}" ${url}`
RED='\033[0;31m'
NC='\033[0m' # No Color
[ "$result" == 'No team' ] && echo -e "${RED}ERROR. Check that webhook URL is correct${NC}" && exit 1
echo SUCCESS!