8
8
output=/dev/null
9
9
thread_ts=" "
10
10
broadcast=0
11
+ fail_on_error=0
11
12
args=()
12
13
13
14
for arg; do
@@ -20,6 +21,8 @@ for arg; do
20
21
thread_ts=" ${arg#* =} " ;;
21
22
--broadcast)
22
23
broadcast=1;;
24
+ --fail-on-error)
25
+ fail_on_error=1;;
23
26
* )
24
27
args+=(" $arg " );;
25
28
esac
@@ -29,28 +32,60 @@ set -- "${args[@]}"
29
32
30
33
text=" ${1:? Some message text is required.} "
31
34
32
- if [[ " $upload " == 1 ]]; then
33
- echo " Uploading data to Slack with the message: $text "
34
- curl https://slack.com/api/files.upload \
35
- --header " Authorization: Bearer $SLACK_TOKEN " \
36
- --form-string channels=" $SLACK_CHANNELS " \
37
- --form-string title=" $text " \
38
- --form-string filename=" $text " \
39
- --form-string thread_ts=" $thread_ts " \
40
- --form file=@/dev/stdin \
41
- --form filetype=text \
42
- --fail --silent --show-error \
43
- --http1.1 \
44
- --output " $output "
45
- else
46
- echo " Posting Slack message: $text "
47
- curl https://slack.com/api/chat.postMessage \
48
- --header " Authorization: Bearer $SLACK_TOKEN " \
49
- --form-string channel=" $SLACK_CHANNELS " \
50
- --form-string text=" $text " \
51
- --form-string thread_ts=" $thread_ts " \
52
- --form-string reply_broadcast=" $broadcast " \
53
- --fail --silent --show-error \
54
- --http1.1 \
55
- --output " $output "
35
+ send_slack_message () {
36
+ if [[ " $upload " == 1 ]]; then
37
+ echo " Uploading data to Slack with the message: $text "
38
+
39
+ upload_file=" $( mktemp -t upload-file-XXXXXX) "
40
+ trap " rm -f '$upload_file '" EXIT
41
+
42
+ cat /dev/stdin > " $upload_file "
43
+ length=$( printf ' %d' " $( < " $upload_file " wc -c) " )
44
+
45
+ upload_info=$( curl https://slack.com/api/files.getUploadURLExternal \
46
+ --header " Authorization: Bearer $SLACK_TOKEN " \
47
+ --form-string filename=" $text " \
48
+ --form-string length=" $length " \
49
+ --fail --silent --show-error \
50
+ --http1.1 )
51
+
52
+ upload_url=" $( jq -r .upload_url <<< " $upload_info" ) "
53
+ curl " $upload_url " \
54
+ --form-string filename=" $text " \
55
+ --form file=" @$upload_file " \
56
+ --fail --silent --show-error \
57
+ --http1.1 > /dev/null
58
+
59
+ files_uploaded=" $( jq -r " [{id: .file_id}]" <<< " $upload_info" ) "
60
+ curl -X POST https://slack.com/api/files.completeUploadExternal \
61
+ --header " Authorization: Bearer $SLACK_TOKEN " \
62
+ --form-string channel_id=" $SLACK_CHANNELS " \
63
+ --form-string thread_ts=" $thread_ts " \
64
+ --form-string files=" $files_uploaded " \
65
+ --fail --silent --show-error \
66
+ --http1.1 \
67
+ --output " $output "
68
+
69
+ else
70
+ echo " Posting Slack message: $text "
71
+ curl https://slack.com/api/chat.postMessage \
72
+ --header " Authorization: Bearer $SLACK_TOKEN " \
73
+ --form-string channel=" $SLACK_CHANNELS " \
74
+ --form-string text=" $text " \
75
+ --form-string thread_ts=" $thread_ts " \
76
+ --form-string reply_broadcast=" $broadcast " \
77
+ --fail --silent --show-error \
78
+ --http1.1 \
79
+ --output " $output "
80
+ fi
81
+ }
82
+
83
+ if ! send_slack_message; then
84
+ if [[ " $fail_on_error " == 1 ]]; then
85
+ echo " Sending Slack message failed"
86
+ exit 1
87
+ else
88
+ echo " Sending Slack message failed, but exiting with success anyway."
89
+ exit 0
90
+ fi
56
91
fi
0 commit comments