-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathslack-upload.sh
executable file
·95 lines (83 loc) · 2.07 KB
/
slack-upload.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
#!/usr/bin/env bash
# This bash script makes use of the Slack API to upload files.
# I found this useful due to the fact that the attachement option
# available in incoming webhooks seems to have an upper limit of
# content size, which is way too small.
#
# See also: https://api.slack.com/methods/files.upload
# safety first
set -euf -o pipefail
echo='echo -e'
Usage() {
${echo}
${echo} "\tusage:\n\t\t$0 [OPTIONS]"
${echo}
${echo} "Required:"
${echo} " -c CHANNEL\tSlack channel to post to"
${echo} " -f FILENAME\tName of file to upload"
${echo} " -s SLACK_TOKEN\tAPI auth token"
${echo}
${echo} "Optional:"
${echo} " -u API_URL\tSlack API endpoint to use (default: https://slack.com/api/files.upload)"
${echo} " -h \tPrint help"
${echo} " -m TYPE\tFile type (see https://api.slack.com/types/file#file_types)"
${echo} " -n TITLE\tTitle for slack post"
${echo} " -v \tVerbose mode"
${echo} " -x COMMENT\tAdd a comment to the file"
${echo}
exit ${1:-$USAGE}
}
# Exit Vars
: ${HELP:=0}
: ${USAGE:=1}
# Default Vars
API_URL='https://slack.com/api/files.upload'
CURL_OPTS='-s'
# main
while getopts :c:f:s:u:hm:n:vx: OPT; do
case ${OPT} in
c)
CHANNEL="$OPTARG"
;;
f)
FILENAME="$OPTARG"
SHORT_FILENAME=$(basename ${FILENAME})
;;
s)
SLACK_TOKEN="$OPTARG"
;;
u)
API_URL="$OPTARG"
;;
h)
Usage ${HELP}
;;
m)
CURL_OPTS="${CURL_OPTS} -F filetype=${OPTARG}"
;;
n)
CURL_OPTS="${CURL_OPTS} -F title='${OPTARG}'"
;;
v)
CURL_OPTS="${CURL_OPTS} -v"
;;
x)
CURL_OPTS="${CURL_OPTS} -F initial_comment='${OPTARG}'"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
Usage ${USAGE}
;;
esac
done
if [[ ( "${CHANNEL}" != "#"* ) && ( "${CHANNEL}" != "@"* ) ]]; then
CHANNEL="#${CHANNEL}"
fi
# had to use eval to avoid strange whitespace behavior in options
eval curl $CURL_OPTS \
--form-string channels=${CHANNEL} \
-F file=@${FILENAME} \
-F filename=${SHORT_FILENAME} \
-F token=${SLACK_TOKEN} \
${API_URL}
exit 0