This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shlack.sh
executable file
·169 lines (154 loc) · 4.48 KB
/
shlack.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
#!/bin/bash
# Shlack, a Bash script to send messages to Slack
# Copyright (C) 2016 Enrico Lamperti
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
post_to_slack () {
# Parse arguments using getopt
local long_options="message:,text:,channel:,botname:,icon:,icon_url:,hook:,debug,silent,no-markdown"
# shellcheck disable=SC2155
local parsed_opts=$(getopt --longoptions="${long_options}" --name="Shlack" -- "$0" "$@")
if [[ $? -ne 0 ]]; then
echo "Error parsing arguments"
return 1 # error parsing
fi
eval set -- "${parsed_opts}"
# Declare variables we'll need later
local debug_mode=false
local silent_mode=false
local slack_markdown=""
local slack_message=""
local slack_channel=""
local slack_bot_name=""
local slack_icon=""
local slack_icon_url=""
local slack_hook_url=""
# Go through the parsed opts until '--' is found
while true; do
case "$1" in
--text|--message)
slack_message="$2"
shift 2
;;
--channel)
slack_channel="$2"
shift 2
;;
--botname)
slack_bot_name="$2"
shift 2
;;
--icon)
slack_icon="$2"
shift 2
;;
--icon_url)
slack_icon_url="$2"
shift 2
;;
--hook)
slack_hook_url="$2"
shift 2
;;
--no-markdown)
slack_markdown="false"
shift
;;
--debug)
debug_mode=true
shift
;;
--silent)
silent_mode=true
shift
;;
--)
shift
break
;;
esac
done
# Check if there's a hook already defined
if [ -z "${slack_hook_url}" ]; then
# Try to use env variable SLACK_URL
if [ -n "${SLACK_URL}" ]; then
slack_hook_url="${SLACK_URL}"
else
# Last try: a .slack-hook file with the hook in it
if [ -f ".slack-hook" ]; then
slack_hook_url=$(cat .slack-hook)
else
echo "Slack hook is undefined."
echo "Check shlack documentation at https://github.com/elamperti/shlack#setup"
return 1
fi
fi
fi
# The message is a required parameter
if [ -z "${slack_message}" ]; then
# Try to read message from stdin
local tmp_stdin=""
if read -r -t 0; then
tmp_stdin="$(cat)"
fi
while read -t 2 -r line; do
tmp_stdin="${tmp_stdin}${line}\n"
done < /dev/stdin
if [ -n "${tmp_stdin}" ]; then
slack_message="${tmp_stdin}"
else
echo "Payload must contain a message."
return 1
fi
fi
if [ -n "${slack_icon}" ]; then
# Normalize icon string so it has colons around it
# shellcheck disable=SC2001
slack_icon=":$(echo "${slack_icon}"|sed 's/^://;s/:$//'):"
# Resets icon_url (it won't be used)
slack_icon_url=""
fi
# Fill payload with data
read -r -d '' slack_payload <<EOM
{
"channel": "${slack_channel}",
"username": "${slack_bot_name}",
"icon_emoji": "${slack_icon}",
"icon_url": "${slack_icon_url}",
"mrkdwn": "${slack_markdown}",
"text": "${slack_message}"
}
EOM
# Remove empty data (everything that ends up with "")
# shellcheck disable=SC2001
slack_payload=$(echo "${slack_payload}"|sed 's/\"[a-z_]*\":\ \"\",//gi')
# Convert payload to one line
slack_payload=${slack_payload//$'\n'/}
slack_payload=${slack_payload// /}
# Debug mode prints payload without actually posting to slack
if [ "${debug_mode}" = true ]; then
echo "Slack hook URL: ${slack_hook_url}"
echo "Payload: ${slack_payload}"
return 0
fi
# Post to slack!
# shellcheck disable=SC2155
local response=$(curl -X POST --silent --data "payload=${slack_payload}" "${slack_hook_url}" 2>&1)
if [ "${silent_mode}" = false ]; then
echo "${response}"
fi
}
# If the script is called directly invoke post_to_slack
[[ "${BASH_SOURCE[0]}" != "$0" ]] || post_to_slack "$@"