I need send messages in slack channel when i deploy things, ou backups and restore dbs, so with this script i can keep the entire team updated when certain actions hapen.
Slack CLI
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#!/usr/bin/env bash
bindir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
etcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -n "${SLACK_CLI_TOKEN}" ]; then
token="${SLACK_CLI_TOKEN}"
elif [ -r "${etcdir}/.slack" ] && [ -f "${etcdir}/.slack" ]; then
token=$(sed -n '1p' < "${etcdir}/.slack")
elif [ -r "${HOME}/.slack" ] && [ -f "${HOME}/.slack" ]; then
token=$(sed -n '1p' < "${HOME}/.slack")
elif [ -r "/etc/.slack" ] && [ -f "/etc/.slack" ]; then
token=$(sed -n '1p' < "/etc/.slack")
fi
# COMMAND PARSING #################################################################################
cmd="${1}" ; shift
# SUBCOMMAND PARSING ##############################################################################
case "${cmd}${1}" in
chatdelete|chatsend|chatupdate|\
filedelete|fileinfo|filelist|fileupload|\
presenceactive|presenceaway|\
reminderadd|remindercomplete|reminderdelete|reminderinfo|reminderlist| \
snoozeend|snoozeinfo|snoozestart|\
statusclear|statusedit)
sub=${1} ; shift
;;
esac
# PIPE FRIENDLINESS ###############################################################################
case "${cmd}${sub}" in
chatsend|chatupdate|fileupload) [ -p /dev/stdin ] && stdin=$(cat <&0) ;;
esac
# ARGUMENT AND OPTION PARSING #####################################################################
while (( "$#" )); do
case "${1}" in
--actions=*) actions=${1/--actions=/''} ; shift ;;
--actions*|-a*) actions=${2} ; shift ; shift ;;
--author-icon=*) authoricon=${1/--author-icon=/''} ; shift ;;
--author-icon*|-ai*) authoricon=${2} ; shift ; shift ;;
--author-link=*) authorlink=${1/--author-link=/''} ; shift ;;
--author-link*|-al*) titlelink=${2} ; shift ; shift ;;
--author=*) author=${1/--author=/''} ; shift ;;
--author*|-at*) author=${2} ; shift ; shift ;;
--channels=*) channels=${1/--channels=/''} ; shift ;;
--channels*|-chs*) channels=${2} ; shift ; shift ;;
--channel=*) channel=${1/--channel=/''} ; shift ;;
--channel*|-ch*) channel=${2} ; shift ; shift ;;
--color=*) color=${1/--color=/''} ; shift ;;
--color*|-cl*) color=${2} ; shift ; shift ;;
--compact|-cp) compact='-c' ; shift ;;
--comment=*) comment=${1/--comment=/''} ; shift ;;
--comment*|-cm*) comment=${2} ; shift ; shift ;;
--count=*) count=${1/--count=/''} ; shift ;;
--count|-cn*) count=${2} ; shift ; shift ;;
--emoji=*) emoji=${1/--emoji=/''} ; shift ;;
--emoji*|-em*) emoji=${2} ; shift ; shift ;;
--fields=*) fields=${1/--fields=/''} ; shift ;;
--fields*|-flds*) fields=${2} ; shift ; shift ;;
--filename=*) filename=${1/--filename=/''} ; shift ;;
--filename*|-fln*) filename=${2} ; shift ; shift ;;
--filetype=*) filetype=${1/--filetype=/''} ; shift ;;
--filetype*|-flt*) filetype=${2} ; shift ; shift ;;
--file=*) file=${1/--file=/''} ; shift ;;
--file*|-fl*) file=${2} ; shift ; shift ;;
--filter=*) filter=${1/--filter=/''} ; shift ;;
--filter*|-f*) filter=${2} ; shift ; shift ;;
--footer-icon=*) footericon=${1/--footer-icon=/''} ; shift ;;
--footer-icon*|-fi*) footericon=${2} ; shift ; shift ;;
--footer=*) footer=${1/--footer=/''} ; shift ;;
--footer*|-ft*) footer=${2} ; shift ; shift ;;
--image=*) image=${1/--image-url=/''} ; shift ;;
--image*|-im*) image=${2} ; shift ; shift ;;
--monochrome|-m) monochrome='-M' ; shift ;;
--minutes=*) minutes=${1/--minutes=/''} ; shift ;;
--minutes*|-mn*) minutes=${2} ; shift ; shift ;;
--page=*) page=${1/--page=/''} ; shift ;;
--page|-pg*) page=${2} ; shift ; shift ;;
--pretext=*) pretext=${1/--pretext=/''} ; shift ;;
--pretext*|-pt*) pretext=${2} ; shift ; shift ;;
--reminder=*) reminder=${1/--reminder=/''} ; shift ;;
--reminder|-rm*) reminder=${2} ; shift ; shift ;;
--text=*) text=${1/--text=/''} ; shift ;;
--text*|-tx*) text=${2} ; shift ; shift ;;
--thumbnail=*) thumbnail=${1/--thumbnail=/''} ; shift ;;
--thumbnail*|-th*) thumbnail=${2} ; shift ; shift ;;
--time=*) _time=${1/--time=/''} ; shift ;;
--time|-tm*) _time=${2} ; shift ; shift ;;
--timestamp-from=*) timestampfrom=${1/--timestampfrom=/''} ; shift ;;
--timestamp-from|-tf*) timestampfrom=${2} ; shift ; shift ;;
--timestamp-to=*) timestampto=${1/--timestampto=/''} ; shift ;;
--timestamp-to|-tt*) timestampto=${2} ; shift ; shift ;;
--timestamp=*) timestamp=${1/--timestamp=/''} ; shift ;;
--timestamp*|-ts*) timestamp=${2} ; shift ; shift ;;
--title-link=*) titlelink=${1/--title-link=/''} ; shift ;;
--title-link*|-tl*) titlelink=${2} ; shift ; shift ;;
--title=*) title=${1/--title=/''} ; shift ;;
--title*|-ti*) title=${2} ; shift ; shift ;;
--token=*) token=${1/--token=/''} ; shift ;;
--token|-tk*) token=${2} ; shift ; shift ;;
--trace|-x) trace='set -x' ; shift ;;
--types=*) types=${1/--types=/''} ; shift ;;
--types|-ty*) types=${2} ; shift ; shift ;;
--user=*) user=${1/--user=/''} ; shift ;;
--user|-ur*) user=${2} ; shift ; shift ;;
*)
case "${cmd}${sub}" in
chatdelete)
[ -n "${1}" ] && [ -n "${timestamp}" ] && [ -z "${channel}" ] && channel=${1}
[ -n "${1}" ] && [ -z "${timestamp}" ] && timestamp=${1}
;;
chatsend)
[ -n "${1}" ] && [ -n "${text}" ] && [ -z "${channel}" ] && channel=${1}
[ -n "${1}" ] && [ -z "${text}" ] && text=${1}
;;
chatupdate)
[ -n "${1}" ] && [ -n "${text}" ] && [ -n "${timestamp}" ] && [ -z "${channel}" ] && channel=${1}
[ -n "${1}" ] && [ -n "${text}" ] && [ -z "${timestamp}" ] && timestamp=${1}
[ -n "${1}" ] && [ -z "${text}" ] && text=${1}
;;
filedelete|fileinfo)
[ -n "${1}" ] && [ -z "${file}" ] && file=${1}
;;
fileupload)
[ -n "${1}" ] && [ -n "${file}" ] && [ -z "${channels}" ] && channels=${1}
[ -n "${1}" ] && [ -z "${file}" ] && file=${1}
;;
snoozeinfo)
[ -n "${1}" ] && [ -z "${user}" ] && user=${1}
;;
snoozestart)
[ -n "${1}" ] && [ -z "${minutes}" ] && minutes=${1}
;;
statusedit)
[ -n "${1}" ] && [ -n "${text}" ] && [ -z "${emoji}" ] && emoji=${1}
[ -n "${1}" ] && [ -z "${text}" ] && text=${1}
;;
reminderadd)
[ -n "${1}" ] && [ -n "${text}" ] && [ -z "${_time}" ] && _time=${1}
[ -n "${1}" ] && [ -z "${text}" ] && text=${1}
;;
remindercomplete|reminderdelete|reminderinfo)
[ -n "${1}" ] && [ -z "${reminder}" ] && reminder=${1}
;;
esac
shift
;;
esac
done
# TRACING #########################################################################################
${trace}
# ARGUMENT AND OPTION PROMPTING ###################################################################
case "${cmd}${sub}" in
chatdelete)
[ -z "${channel}" ] && read -e -p 'Enter channel (e.g. #general): ' channel
[ -z "${timestamp}" ] && read -e -p 'Enter timestamp (e.g. 1405894322.002768): ' timestamp
;;
chatsend)
[ -z "${channel}" ] && [ -z "${text}" ] && prompt='true'
[ -n "${prompt}" ] && [ -z "${actions}" ] && read -e -p 'Enter actions (e.g. {"type": “button", "style": "primary", "text": "my text", "url": "http://example.com"}, ...): ' actions
[ -n "${prompt}" ] && [ -z "${author}" ] && read -e -p 'Enter author name (e.g. slackbot): ' author
[ -n "${prompt}" ] && [ -z "${authoricon}" ] && read -e -p 'Enter author icon (e.g. a URL): ' authoricon
[ -n "${prompt}" ] && [ -z "${authorlink}" ] && read -e -p 'Enter author link (e.g. a URL): ' authorlink
[ -z "${channel}" ] && read -e -p 'Enter channel (e.g. #general): ' channel
[ -n "${prompt}" ] && [ -z "${color}" ] && read -e -p 'Enter color (e.g. good): ' color
[ -n "${prompt}" ] && [ -z "${fields}" ] && read -e -p 'Enter fields (e.g. {"title": "My Field Title", "value": "My field value", "short": true}, ...): ' fields
[ -n "${prompt}" ] && [ -z "${footer}" ] && read -e -p 'Enter footer (e.g. Hello footer!): ' footer
[ -n "${prompt}" ] && [ -z "${footericon}" ] && read -e -p 'Enter footer icon (e.g. a URL): ' footericon
[ -n "${prompt}" ] && [ -z "${image}" ] && read -e -p 'Enter image (e.g. a URL): ' image
[ -n "${prompt}" ] && [ -z "${pretext}" ] && read -e -p 'Enter pretext (e.g. Hello pretext!): ' pretext
[ -z "${text}" ] && read -e -p 'Enter text (e.g. Hello text!): ' text
[ -n "${prompt}" ] && [ -z "${thumbnail}" ] && read -e -p 'Enter thumbnail (e.g. a URL): ' thumbnail
[ -n "${prompt}" ] && [ -z "${_time}" ] && read -e -p 'Enter time (e.g. 123456789): ' _time
[ -n "${prompt}" ] && [ -z "${title}" ] && read -e -p 'Enter title (e.g. Hello title!): ' title
[ -n "${prompt}" ] && [ -z "${titlelink}" ] && read -e -p 'Enter title link (e.g. a URL): ' titlelink
;;
chatupdate)
[ -z "${text}" ] && read -e -p 'Enter text (e.g. Hello World!): ' text
[ -z "${timestamp}" ] && read -e -p 'Enter timestamp (e.g. 1405894322.002768): ' timestamp
[ -z "${channel}" ] && read -e -p 'Enter channel (e.g. #general): ' channel
;;
filedelete)
[ -z "${file}" ] && read -e -p 'Enter file (e.g. F2147483862): ' file
;;
fileinfo)
[ -z "${file}" ] && read -e -p 'Enter file (e.g. F2147483862): ' file
;;
fileupload)
[ -z "${file}" ] && read -e -p 'Enter file (e.g. file.log): ' file
[ -z "${channels}" ] && read -e -p 'Enter channels (e.g. #general,C1234567890): ' channels
;;
init)
if [ -n "${SLACK_CLI_TOKEN}" ]; then
_token="${SLACK_CLI_TOKEN}"
elif [ -r "${etcdir}/.slack" ] && [ -f "${etcdir}/.slack" ]; then
_token=$(sed -n '1p' < "${etcdir}/.slack")
fi
if [ -z "${token}" ] || [ "${token}" == "${_token}" ]; then
read -e -p 'Enter Slack API token: ' token
fi
;;
reminderadd)
[ -z "${text}" ] && read -e -p 'Enter text (e.g. lunch): ' text
[ -z "${_time}" ] && read -e -p 'Enter time (e.g. 123456789): ' _time
;;
remindercomplete|reminderdelete|reminderinfo)
[ -z "${reminder}" ] && read -e -p 'Enter reminder (e.g. RmCT7QGVBF): ' reminder
;;
snoozestart)
[ -z "${minutes}" ] && read -e -p 'Enter minutes (e.g. 60): ' minutes
;;
statusedit)
[ -z "${text}" ] && read -e -p 'Enter text (e.g. lunch): ' text
[ -z "${emoji}" ] && read -e -p 'Enter emoji (e.g. :hamburger:): ' emoji
;;
esac
# COMMAND UTILITY FUNCTIONS #######################################################################
function attachify() {
[ -n "${actions}" ] && local _actions=", \"actions\": [${actions}]"
[ -n "${author}" ] && local _author=", \"author_name\": \"${author}\""
[ -n "${authoricon}" ] && local _authoricon=", \"author_icon\": \"${authoricon}\""
[ -n "${authorlink}" ] && local _authorlink=", \"author_link\": \"${authorlink}\""
[ -n "${color}" ] && local _color=", \"color\": \"${color}\""
[ -n "${fields}" ] && local _fields=", \"fields\": [${fields}]"
[ -n "${footer}" ] && local _footer=", \"footer\": \"${footer}\""
[ -n "${footericon}" ] && local _footericon=", \"footer_icon\": \"${footericon}\""
[ -n "${image}" ] && local _image=", \"image_url\": \"${image}\""
[ -n "${pretext}" ] && local _pretext=", \"pretext\": \"${pretext}\""
[ -n "${thumbnail}" ] && local _thumbnail=", \"thumb_url\": \"${thumbnail}\""
[ -n "${_time}" ] && local __time=", \"ts\": \"${_time}\""
[ -n "${title}" ] && local _title=", \"title\": \"${title}\""
[ -n "${titlelink}" ] && local _titlelink=", \"title_link\": \"${titlelink}\""
local _json="${_author}${_authoricon}${_authorlink}${_color}${_footer}${_footericon}${_image}${_pretext}${_thumbnail}${__time}${_title}${_titlelink}${_fields}${_actions}"
[ -z "${_json}" ] && local _attachments="[{\"mrkdwn_in\": [\"pretext\"], \"fallback\": \"${text}\", \"pretext\": \"${text}\"}]"
[ -n "${_json}" ] && local _attachments="[{\"mrkdwn_in\": [\"fields\", \"pretext\", \"text\"], \"fallback\": \"${text}\", \"text\": \"${text}\"${_json}}]"
echo "${_attachments}"
}
function jqify() {
case "$(echo ${1} | jq -r '.ok')" in
true) echo ${1} | jq -r ${compact} ${monochrome} "${filter:=.}" ;;
*) echo ${1} | jq -r ${compact} ${monochrome} "${filter:=.}" ; return 1 ;;
esac
}
function lchannel() {
case "${channel}" in
@*)
local _user=$(\
curl -s -X POST https://slack.com/api/users.list --data-urlencode "token=${token}" 2>&1 | \
jq -r ".members | map(select(.name == \"${channel/@/}\" or .profile.display_name == \"${channel/@/}\")) | .[0].id")
local _channel=$(\
curl -s -X POST https://slack.com/api/im.list --data-urlencode "token=${token}" 2>&1 | \
jq -r ".ims | map(select(.user == \"${_user}\")) | .[].id")
echo ${_channel}
;;
*) echo ${channel} ;;
esac
}
function luser() {
case "${user}" in
@*)
local _user=$(\
curl -s -X POST https://slack.com/api/users.list --data-urlencode "token=${token}" 2>&1 | \
jq -r ".members | map(select(.name == \"${user/@/}\" or .profile.display_name == \"${user/@/}\")) | .[0].id")
echo ${_user}
;;
*) echo ${user} ;;
esac
}
# COMMAND FUNCTIONS ###############################################################################
function chatdelete() {
local msg=$(\
curl -s -X POST https://slack.com/api/chat.delete \
--data-urlencode "as_user=true" \
--data-urlencode "channel=$(lchannel)" \
--data-urlencode "ts=${timestamp}" \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function chatsend() {
[ -n "${stdin}" ] && [ -z "${text}" ] && text=\`\`\`${stdin//$'\n'/'\n'}\`\`\`
local msg=$(\
curl -s -X POST https://slack.com/api/chat.postMessage \
--data-urlencode "as_user=true" \
--data-urlencode "attachments=$(attachify)" \
--data-urlencode "channel=$(lchannel)" \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function chatupdate() {
[ -n "${stdin}" ] && [ -z "${text}" ] && text=\`\`\`${stdin//$'\n'/'\n'}\`\`\`
local msg=$(\
curl -s -X POST https://slack.com/api/chat.update \
--data-urlencode "as_user=true" \
--data-urlencode "attachments=$(attachify)" \
--data-urlencode "channel=$(lchannel)" \
--data-urlencode "ts=${timestamp}" \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function help() {
local a=(${0//\// })
local bin=${a[${#a[@]}-1]}
echo 'Usage:'
echo " ${bin} chat delete [<timestamp> [channel]]"
echo ' [--channel|-ch <channel>] [--compact|-c] [--filter|-f <filter>] [--monochrome|-m]'
echo ' [--timestamp|-ts <timestamp>] [--trace|-x]'
echo
echo " ${bin} chat send [<text> [channel]]"
echo ' [--author|-at <author>] [--author-icon|-ai <author-icon-url>]'
echo ' [--author-link|-al <author-link>] [--channel|-ch <channel>] [--color|-cl <color>]'
echo ' [--compact|-cp] [--fields|-flds <fields>] [--filter|-f <filter>] [--footer|-ft <footer>]'
echo ' [--footer-icon|-fi <footer-icon-url>] [--image|-im <image-url>] [--monochrome|-m]'
echo ' [--pretext|-pt <pretext>] [--text|-tx <text>] [--thumbnail|-th <thumbnail-url>]'
echo ' [--time|-tm <time>] [--title|-ti <title>] [--title-link|-tl <title-link>]'
echo ' [--trace|-x]'
echo
echo " ${bin} chat update [<text> [<timestamp> [channel]]]"
echo ' [--author|-at <author>] [--author-icon|-ai <author-icon-url>]'
echo ' [--author-link|-al <author-link>] [--channel|-ch <channel>] [--color|-cl <color>]'
echo ' [--compact|-cp] [--fields|flds <fields>] [--filter|-f <filter>] [--footer|-ft <footer>]'
echo ' [--footer-icon|-fi <footer-icon-url>] [--image|-im <image-url>] [--monochrome|-m]'
echo ' [--pretext|-pt <pretext>] [--text|-tx <text>] [--thumbnail|-th <thumbnail-url>]'
echo ' [--time|-tm <time>] [--timestamp|-ts <timestamp>] [--title|-ti <title>]'
echo ' [--title-link|-tl <title-link>] [--trace|-x]'
echo
echo " ${bin} init"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--token|-tk <token>]'
echo ' [--trace|-x]'
echo
echo " ${bin} file delete [file]"
echo ' [--compact|-c] [--file|-fl <file>] [--filter|-f <filter>] [--monochrome|-m]'
echo ' [--trace|-x]'
echo
echo " ${bin} file info [file]"
echo ' [--count|-cn <count>] [--compact|-c] [--file|-fl <file>] [--filter|-f <filter>]'
echo ' [--monochrome|-m] [--page|-pg <page>] [--trace|-x]'
echo
echo " ${bin} file list"
echo ' [--channel|-ch <channel>] [--count|-cn <count>] [--compact|-c] [--filter|-f <filter>]'
echo ' [--monochrome|-m] [--page|-pg <page>] [--timestamp-from|-tf <timetamp>]'
echo ' [--timestamp-to|-tt <timestamp>] [--trace|-x] [--types|-ty <types>]'
echo ' [--user|-ur <user>]'
echo
echo " ${bin} file upload [<file> [channels]]"
echo ' [--channels|-chs <channels>] [--comment|-cm <comment>] [--compact|-c]'
echo ' [--file|fl <file>] [--filename|-fn <filename>] [--filetype|-ft <filetype>]'
echo ' [--filter|-f <filter>] [--monochrome|-m] [--title|-ti <title>] [--trace|-x]'
echo
echo " ${bin} presence active"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--trace|-x]'
echo
echo " ${bin} presence away"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--trace|-x]'
echo
echo " ${bin} reminder add [<text> [time]]"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--text|tx <text>]'
echo ' [--time|tm <time>] [--trace|-x] [--user|-ur <user>]'
echo
echo " ${bin} reminder complete [reminder]"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--reminder|rm <reminder>]'
echo ' [--trace|-x]'
echo
echo " ${bin} reminder delete [reminder]"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--reminder|rm <reminder>]'
echo ' [--trace|-x]'
echo
echo " ${bin} reminder info [reminder]"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--reminder|rm <reminder>]'
echo ' [--trace|-x]'
echo
echo " ${bin} reminder list"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--trace|-x]'
echo
echo " ${bin} snooze end"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--trace|-x]'
echo
echo " ${bin} snooze info [user]"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--trace|-x]'
echo ' [--user|-ur <user>]'
echo
echo " ${bin} snooze start [minutes]"
echo ' [--compact|-c] [--filter|-f <filter>] [--minutes|-mn <minutes>] [--monochrome|-m]'
echo ' [--trace|-x]'
echo
echo " ${bin} status clear"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--trace|-x]'
echo
echo " ${bin} status edit [<text> [<emoji>]]"
echo ' [--compact|-c] [--filter|-f <filter>] [--monochrome|-m] [--trace|-x]'
echo
echo 'Configuration Commands:'
echo ' init Initialize'
echo
echo 'Chat Commands:'
echo ' chat delete Delete chat message'
echo ' chat send Send chat message'
echo ' chat update Update chat message'
echo
echo 'File Commands:'
echo ' file delete Delete file'
echo ' file info Info about file'
echo ' file list List files'
echo ' file upload Upload file'
echo
echo 'Presence Commands:'
echo ' presence active Active presence'
echo ' presence away Away presence'
echo
echo 'Reminder Commands:'
echo ' reminder add Add reminder'
echo ' reminder complete Complete reminder'
echo ' reminder delete Delete reminder'
echo ' reminder info Info about reminder'
echo ' reminder list List reminders'
echo
echo 'Snooze Commands:'
echo ' snooze end End snooze'
echo ' snooze info Info about snooze'
echo ' snooze start Start snooze'
echo
echo 'Status Commands:'
echo ' status clear Clear status'
echo ' status edit Edit status'
echo
echo 'More Information:'
echo ' repo https://github.com/rockymadden/slack-cli'
}
function filedelete() {
local msg=$(\
curl -s -X POST https://slack.com/api/files.delete \
--data-urlencode "file=${file}" \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function fileinfo() {
local msg=$(\
curl -s -X POST https://slack.com/api/files.info \
${count:+ --data-urlencode "count=${count}"} \
--data-urlencode "file=${file}" \
${page:+ --data-urlencode "page=${page}"} \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function filelist() {
local msg=$(\
curl -s -X POST https://slack.com/api/files.list \
${channel:+ --data-urlencode "channel=${channel}"} \
${count:+ --data-urlencode "count=${count}"} \
${page:+ --data-urlencode "page=${page}"} \
${timestampfrom:+ --data-urlencode "ts_from=${timestampfrom}"} \
${timestampto:+ --data-urlencode "ts_to=${timestampto}"} \
${types:+ --data-urlencode "types=${types}"} \
${user:+ --data-urlencode "user=$(luser)"} \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function fileupload() {
[ -n "${stdin}" ] && [ -z "${file}" ] && file=${stdin}
if [ -f "${file}" ]; then
local _file=${file}
case "${filename}" in
'') local _filename=$(basename ${file}) ;;
*) local _filename=${filename} ;;
esac
else
local _content=${file}
case "${filename}" in
'') local _filename='stdin' ;;
*) local _filename=${filename} ;;
esac
fi
case "${_file}" in
'')
local msg=$(\
curl -s -X POST https://slack.com/api/files.upload \
${channels:+ --data-urlencode "channels=${channels}"} \
${comment:+ --data-urlencode "initial_comment=${comment}"} \
${_content:+ --data-urlencode "content=${_content}"} \
${_filename:+ --data-urlencode "filename=${_filename}"} \
${filetype:+ --data-urlencode "filetype=${filetype}"} \
${title:+ --data-urlencode "title=${title}"} \
--data-urlencode "token=${token}")
;;
*)
local msg=$(\
curl -s -X POST https://slack.com/api/files.upload \
${channels:+ --form-string "channels=${channels}"} \
${comment:+ --form "initial_comment=${comment}"} \
${_file:+ --form "file=@${_file}"} \
${_filename:+ --form "filename=${_filename}"} \
${filetype:+ --form "filetype=${filetype}"} \
${title:+ --form "title=${title}"} \
--form "token=${token}")
;;
esac
jqify "${msg}"
}
function init() {
echo "${token}" > "${etcdir}/.slack"
case "${?}" in
0) echo '{"ok": true}' | jq -r ${compact} ${monochrome} "${filter:=.}" ;;
*)
echo '{"ok": false, "error": "not_writable"}' |
jq -r ${compact} ${monochrome} "${filter:=.}" ; return 1
;;
esac
}
# Setting presence=auto + setActive works more consistently than just setActive.
function presenceactive() {
local msg0=$(\
curl -s -X POST https://slack.com/api/users.setPresence \
--data-urlencode "presence=auto" \
--data-urlencode "token=${token}")
local msg1=$(\
curl -s -X POST https://slack.com/api/users.setActive \
--data-urlencode "token=${token}")
jqify "${msg0}" && jqify "${msg1}" >/dev/null 2>&1
}
function presenceaway() {
local msg=$(\
curl -s -X POST https://slack.com/api/users.setPresence \
--data-urlencode "presence=away" \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function reminderadd() {
local msg=$(\
curl -s -X POST https://slack.com/api/reminders.add \
--data-urlencode "text=${text}" \
--data-urlencode "time=${_time}" \
${user:+ --data-urlencode "user=$(luser)"} \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function remindercomplete() {
local msg=$(\
curl -s -X POST https://slack.com/api/reminders.complete \
--data-urlencode "reminder=${reminder}" \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function reminderdelete() {
local msg=$(\
curl -s -X POST https://slack.com/api/reminders.delete \
--data-urlencode "reminder=${reminder}" \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function reminderinfo() {
local msg=$(\
curl -s -X POST https://slack.com/api/reminders.info \
--data-urlencode "reminder=${reminder}" \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function reminderlist() {
local msg=$(\
curl -s -X POST https://slack.com/api/reminders.list \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function snoozeend() {
local msg=$(\
curl -s -X POST https://slack.com/api/dnd.endSnooze \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function snoozeinfo() {
local msg=$(\
curl -s -X POST https://slack.com/api/dnd.info \
${user:+ --data-urlencode "user=$(luser)"} \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function snoozestart() {
local msg=$(\
curl -s -X POST https://slack.com/api/dnd.setSnooze \
--data-urlencode "num_minutes=${minutes}" \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function statusclear() {
local msg=$(\
curl -s -X POST https://slack.com/api/users.profile.set \
--data-urlencode 'profile={"status_text":"", "status_emoji":""}' \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function statusedit() {
local msg=$(\
curl -s -X POST https://slack.com/api/users.profile.set \
--data-urlencode "profile={\"status_text\":\"${text}\", \"status_emoji\":\"${emoji}\"}" \
--data-urlencode "token=${token}")
jqify "${msg}"
}
function version() {
echo '0.18.0'
}
# COMMAND ROUTING #################################################################################
case "${cmd}${sub}" in
--help|-h) help ; exit 0 ;;
--version|-v) version ; exit 0 ;;
init) init ; exit $? ;;
chatdelete|chatsend|chatupdate|\
filedelete|fileinfo|filelist|fileupload|\
presenceactive|presenceaway|\
reminderadd|remindercomplete|reminderdelete|reminderinfo|reminderlist|\
snoozeend|snoozeinfo|snoozestart|\
statusclear|statusedit)
if [ -z "${token}" ]; then
echo '{"ok": false, "error": "not_inited"}' |
jq -r ${compact} ${monochrome} "${filter:=.}" ; exit 1
fi
${cmd}${sub} ; exit $?
;;
*) help ; exit 1 ;;
esac
Copy this content and create a file called slack-cli and give him permission to execute.
1
chomd +x slack-cli
Generate a token in Slack app and put him into a file .slack in same directory of slack-cli
How to use
1
./slack-cli chat send "Testing message" '##slack-channel-name'