-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathslack-echo
More file actions
executable file
·100 lines (89 loc) · 2.19 KB
/
Copy pathslack-echo
File metadata and controls
executable file
·100 lines (89 loc) · 2.19 KB
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
#!/usr/bin/env bash
set -euo pipefail
YP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)"
source ${YP_DIR}/sh/common.inc.sh
#- slack-echo 1.0
## Usage: slack-echo [OPTION] -- TEXT...
## Echo message via a Slack webhook.
##
## -f, --from Text to display as sender. Defaults to $(whoami)@$(hostname)
## -i, --icon Emoji to display as sender's icon. Defaults to ":shipit:"
## -t, --to Recipient as #channel or @username. Defaults to ${SLACK_CHANNEL}
## -w, --webhook Webhook URL. Defaults to ${SLACK_WEBHOOK}
##
## -h, --help Display this help and exit
## -v, --version Output version information and exit
TMP_SLACK_PAYLOAD=$(mktemp -t yplatform.XXXXXXXXXX)
function on_exit() {
rm -rf ${TMP_SLACK_PAYLOAD}
}
trap on_exit EXIT
FROM="$(whoami)@$(hostname)"
ICON=":shipit:"
TO=
[[ -z ${SLACK_CHANNEL:-} ]] || {
TO="#${SLACK_CHANNEL}"
}
if { getopt --test >/dev/null 2>&1 && false; } || [[ "$?" = "4" ]] || false; then
ARGS=$(getopt -o hvf:i:t:w: -l help,version,from:,icon:,to:,webhook: -n $(basename ${BASH_SOURCE[0]}) -- "$@") || sh_script_usage # editorconfig-checker-disable-line
eval set -- "${ARGS}"
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-f|--from)
FROM=$2
shift 2
;;
-i|--icon)
ICON=$2
shift 2
;;
-t|--to)
TO=$2
shift 2
;;
-w|--webhook)
SLACK_WEBHOOK="$2"
shift 2
;;
-h|--help)
sh_script_usage
;;
-v|--version)
sh_script_version
;;
--)
shift
break
;;
-*)
sh_script_usage
;;
*)
break
;;
esac
done
# [[ $# -eq 0 ]] || sh_script_usage
CHANNEL_TO=
[[ -z ${TO} ]] || {
CHANNEL_TO="\"channel\": \"${TO}\","
}
TEXT="$*"
TEXT="${TEXT//\"/\\\"}"
TEXT="${TEXT//$'\n'/\\\n}"
DATA=$(cat <<EOF
{
"username": "${FROM}",
${CHANNEL_TO}
"icon_emoji": "${ICON}",
"text": "${TEXT}"
}
EOF
)
echo "${DATA}"
curl -qfsSL \
-X POST \
-H "content-type: application/json" \
-d "${DATA}" \
${SLACK_WEBHOOK}