-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.sh
172 lines (158 loc) · 3.93 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
# send message to slack channel
# define the boundaries after which the message will be sent will be sent as an attachment
# count "c" characters or "l" lines from stdin
stdin_check="l"
# define number of characters or lines from stdin
stdin_count="2"
# define usage function
function usage {
echo "Basic connection parameters:"
echo " -h hook_url"
echo " -c channel"
echo " -u username"
echo " -i icon"
echo " -p enable|disable (markdown)"
echo
echo "Define message using file contents:"
echo " -F \"file\""
echo " -C color"
echo " -T \"title\""
echo
echo "Define message using parameter:"
echo " -m \"message\""
echo
echo "Otherwise, define message using standard input."
echo
}
# curl is an essential application used to send message
if ! command -v curl &>/dev/null; then
echo "Error: curl is missing"
exit 1
fi
# display usage if no arguments are passed
if [ "$#" = "0" ];then
usage;
exit;
fi
# parse arguments
while getopts "h:c:u:m:i:C:F:T:p:" option; do
case $option in
"h")
slack_hook_url=${OPTARG}
;;
"c")
slack_channel=${OPTARG}
;;
"u")
slack_username=${OPTARG}
;;
"i")
slack_icon=${OPTARG}
;;
"C")
slack_color=${OPTARG}
;;
"F")
slack_file=${OPTARG}
;;
"T")
slack_title=$(echo -e "${OPTARG}" | sed 's/\"/\\"/g' | sed "s/'/\'/g" | sed 's/`/\`/g')
;;
"p")
if [ "${OPTARG}" == "disable" ]; then
slack_markdown="false"
else
slack_markdown="true"
fi
;;
"m")
slack_message=${OPTARG}
;;
\?|:)
usage
exit
;;
esac
done
# set default icon if not defined
if [ -z "$slack_icon" ]; then
slack_icon="ghost"
fi
# read message
if [ -n "$slack_file" ]; then
if [ -f "$slack_file" ]; then
# read message using file (-F)
file=$(cat $slack_file)
else
echo "Error: File not found"
exit 1
fi
fi
if [ -n "$slack_message" ]; then
# read message (-m) message parameter
message="$slack_message"
else
if [ ! -t 0 ]; then
# read message from stdin
message=$(cat /dev/stdin)
fi
fi
# define header and color
if [ -n "$slack_title" ]; then
title="\"title\":\"${slack_title}\" ,"
if [ -z "$slack_file" ]; then
file="$message"
slack_file="/dev/null"
fi
else
title=""
fi
if [ -n "$slack_color" ]; then
color="\"color\":\"${slack_color}\" ,"
if [ -z "$slack_file" ]; then
file="$message"
slack_file="/dev/null"
fi
else
color=""
fi
# do not send empty message
if [ -z "${message}" ] && [ -z "${file}" ]; then
echo "Error: There is nothing to send"
exit 1
fi
# send message as attachment if it is bigger then defined values so it can be folded up
if [ -z "$slack_file" ]; then
count=$(echo -e "${message}" | wc -${stdin_check})
if [ "$count" -gt "$stdin_count" ]; then
# activate small cheat
file="$message"
slack_file="/dev/null"
fi
fi
# escape message and prepare text property
if [ -n "${message}" ]; then
escaped_message=$(echo -e "${message}" | sed 's/\"/\\"/g' | sed "s/'/\'/g" | sed 's/`/\`/g')
text="\"text\": \"${escaped_message}\""
fi
# escape file contents and prepare text property
if [ -n "${file}" ]; then
escaped_file=$(echo -e "${file}" | sed 's/\"/\\"/g' | sed "s/'/\'/g" | sed 's/`/\`/g')
file="\"text\": \"${escaped_file}\""
fi
# define json
markdown="\"mrkdwn\": false,"
if [ -n "$slack_file" ]; then
if [ "$slack_markdown" == "true" ]; then
markdown="\"mrkdwn_in\": [\"text\"],"
fi
json="{\"channel\": \"${slack_channel}\", \"username\":\"${slack_username}\", \"icon_emoji\":\":${slack_icon}:\", \"attachments\":[{ ${color} ${title} ${markdown} ${file}}]}"
else
if [ "$slack_markdown" == "true" ]; then
markdown="\"mrkdwn\": true,"
fi
json="{\"channel\": \"${slack_channel}\", \"username\":\"${slack_username}\", \"icon_emoji\":\":${slack_icon}:\", ${markdown} ${text}}"
fi
# send message
curl -s -d "payload=$json" $slack_hook_url