-
Notifications
You must be signed in to change notification settings - Fork 9
/
slackr
executable file
·94 lines (84 loc) · 2.38 KB
/
slackr
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
#!/bin/bash
WEBHOOK_URL=""
usage() { echo "Usage: $(basename "$0") [options] <text>
Options:
-r <channel name|channel ID>
-i <bot icon emoji>
-n <bot name>
-c <good|warning|danger|#hex color>
-a <author name>
-t <title text>
-l <title link URL>
-f <footer text>
-w <webhook URL>
Examples:
$(basename "$0") some text
$(basename "$0") -c good -n friendlybot -i :cat: hello
$(basename "$0") -r general < logfile.txt
ls -la /etc/ | $(basename "$0") -r D024BE91L -f \"\$(history 1)\"
tail /var/log/syslog | $(basename "$0") -a \"\$(id -un) \$(hostname -f)\"
" 1>&2; exit 1; }
json_escape () {
printf '%s' "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))'
}
recipient=""
boticon=""
botname=""
color=""
authorname=""
title=""
titlelink=""
footer=""
while getopts ":r:i:n:c:a:t:l:f:w:" o; do
case "${o}" in
r)
recipient="\"channel\":$(json_escape "${OPTARG}"),"
;;
i)
boticon="\"icon_emoji\":$(json_escape "${OPTARG}"),"
;;
n)
botname="\"username\":$(json_escape "${OPTARG}"),"
;;
c)
color="\"color\":$(json_escape "${OPTARG}"),"
;;
a)
authorname="\"author_name\":$(json_escape "${OPTARG}"),"
;;
t)
title="\"title\":$(json_escape "${OPTARG}"),"
;;
l)
titlelink="\"title_link\":$(json_escape "${OPTARG}"),"
;;
f)
footer="\"footer\":$(json_escape "${OPTARG}"),"
;;
w)
WEBHOOK_URL="${OPTARG}"
;;
*)
echo "Illegal option: ${OPTARG}"$'\n'
usage
;;
esac
done
shift $((OPTIND-1))
if [[ "$WEBHOOK_URL" == "" ]]; then
echo "No webhook URL specified. Please set WEBHOOK_URL in $(readlink -f "$0") or use the -w option."$'\n'
usage
fi
text=$*
if [[ "$text" == "" ]]; then
if [ -t 0 ]; then
echo "No text specified"$'\n'
usage
else
while IFS= read -r line || [ -n "$line" ]; do
text="$text$line"$'\n'
done
fi
fi
payload="payload={$recipient$boticon$botname\"attachments\":[{$color$authorname$title$titlelink$footer\"text\":$(json_escape "\`\`\`$text\`\`\`"),\"mrkdwn_in\":[\"text\"]}]}"
echo "$(curl -s -S --data-urlencode "$payload" "$WEBHOOK_URL" 2>&1)"