In this tutorial, we’ll learn about how we can send messages to Slack via the Command Line. Slack is an extremely popular collaboration tool due to its amazing set of integrations. The tool we’ll use today does not need any integration however, it only requires your personal Slack Token. Let’s get started.
Installation
The tool we’ll be using is call Slack-Cli, If you have NPM or Node installed, you can get this tool with a simple
npm install slack-cli -g
note the -g global flag, this will make the slackcli command available globally anywhere from the command line.
Usage
First we need to get our slack token from https://api.slack.com/custom-integrations/legacy-tokens, this is the only thing you’ll need to make this work. Now head over to the command line and send a message to Slack with:
slackcli -h "general" -t "12312312313123123121231312312" -u "Pentacode" -m "Hello Youtube"
replace your token for the -t value. You should now see a message from user name Pentacode in your Slack channel! It’s that simple. Now I don’t want to send/type my token every time I send a message, so I am going to store my token as a value somewhere, for the sake of simplicity, I will store it as a command line variable:
SLACK_TOKEN=12312312313123123121231312312
now you can simplify the above command as:
slackcli -h "general" -t $SLACK_TOKEN -u "Pentacode" -m "Hello Youtube"
looks much better!
This tool has more options for you to customize your message, you can even attach a file to it, this means you can build literately anything you want with this tool, perhaps a bot? 🙂
# slackcli --help
USAGE: node <SOMEWHERE>\npm\node_modules\slack-cli\bin\cmd.js [OPTION1] [OPTION2]... arg1 arg2...
The following options are supported:
-u, --username <ARG1> Specify the name of the bot.
-i, --icon_url <ARG1> Specify the URL to an image to use as the icon for this message.
-e, --icon_emoji <ARG1> Specify the emoji to use as the icon for this message. This cannot be used together with icon_url.
-m, --message <ARG1> Specify the text of the message to send.
-g, --group <ARG1> Specify the group name.
-h, --channel <ARG1> Specify the channel name.
-f, --file <ARG1> Specify the name of the file to send.
-t, --token <ARG1> Specify the Slack API token.
-v, --verbose Set to verbose mode.
-c, --console Use console to input message.
-w, --waitForText <ARG1> Specify the text message to wait. Default timeout is 30 seconds.
-s, --timeout <ARG1> Specify the seconds to timeout when using --waitForText.
-l, --linkNames Link names in messages.
-r, --read Read to stdout.
-a, --asUser Post message as a user for which API Token belongs to.
-p, --pin Pin message after sending.
I hope you enjoy this quick little tutorial and find the tool useful.
If you enjoyed this tutorial, make sure to subscribe to our Youtube Channel and follow us on Twitter @pentacodevids for latest updates!