Post to Slack from Terminal using slack-cli
Since I haven’t posted in a while, I decided to share a nice utility I discovered today; as usual, this is not a tutorial, but you’ll be able to follow along and fill in the gaps.
At work, I wanted the ability to send documents to a specific Slack channel, so that multiple people can see them in an unobtrusive way (email would be too much for these low-priority docs). I was also looking to create a highly-automated process, ideally scanning each document from an office copier, sending it “somewhere” from there and having it magically appear in Slack; this would be possible for a paid Slack plan using this email Slack app, but we’re on the free tier and can’t use that.
The solution was slack-cli and I have to say it was both pretty convenient and fun to set up:
-
Create a custom bot for Slack and save its API key for later; no need to configure hooks or anything else, just type a name, pick an icon and add an optional description.
-
Add the new bot to the Slack channel where you want it to post.
-
Install slack-cli via brew as described in the instructions on GitHub; please consider that you’ll have to quit and re-enter Terminal after installation, before initializing it.
-
Execute the Terminal command ‘slack init’ and paste the bot API key.
-
Almost there! Try the bot, both sending a message and uploading a file:
slack chat send ‘Hello from Terminal’ –channel ‘#mychannel’
slack file upload /Users/myuser/Desktop/file.pdf ‘#mychannel’ –comment ‘File uploaded from Terminal’ –title ‘FYI, this is the file for today’
Finally, apply some magic with Apple Script or with your automation app of choice: retrieve each email sent from the office copier, temporarily save the attachment and post it to Slack using slack-cli; we’re doing this using our 4D database, so I can’t post the actual steps for that because they are very much custom-tailored to our environment.
Hope this helps someone else along the way!