-
Notifications
You must be signed in to change notification settings - Fork 2
/
post_to_slack.sh
executable file
·66 lines (58 loc) · 1.55 KB
/
post_to_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
#!/usr/bin/env bash
function usage {
programName=$0
echo "description: use this program to post messages to Slack channel"
echo "usage: $programName [-t \"sample title\"] [-b \"message body\"] [-c \"mychannel\"] [-u \"slack url\"]"
echo " -t the title of the message you are posting"
echo " -b The message body"
echo " -c The channel you are posting to"
echo " -u The slack hook url to post to"
exit 1
}
while getopts ":t:b:c:u:h" opt; do
case ${opt} in
t) msgTitle="$OPTARG"
;;
u) slackUrl="$OPTARG"
;;
b) msgBody="$OPTARG"
;;
c) channelName="$OPTARG"
;;
h) usage
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
if [[ ! "${msgTitle}" || ! "${slackUrl}" || ! "${msgBody}" || ! "${channelName}" ]]; then
echo "all arguments are required"
usage
fi
read -d '' payLoad << EOF
{
"channel": "#${channelName}",
"username": "$(hostname)",
"icon_emoji": ":sunglasses:",
"attachments": [
{
"fallback": "${msgTitle}",
"color": "good",
"title": "${msgTitle}",
"fields": [{
"title": "message",
"value": "${msgBody}",
"short": false
}]
}
]
}
EOF
statusCode=$(curl \
--write-out %{http_code} \
--silent \
--output /dev/null \
-X POST \
-H 'Content-type: application/json' \
--data "${payLoad}" ${slackUrl})
echo ${statusCode}