Simple slack post interface from terminal with webhook.
This script use jq
command to modify json text.
Installjq
if you don't have jq
.
sudo apt install jq
Follow the official configuration manual below to obtain the webhook URL. https://api.slack.com/messaging/webhooks#getting_started
Webhook URL looks something like this: https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
,
and export the URL to $SLACK_WHURL
with the command below.
export SLACK_WHURL=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Copy slack_post.sh
to your local directory and give the file execute permission.
chmod +x slack_post.sh
By default, this script will post to default channel selected when you generate webhook URL. You can change default channel from here. https://slack.com/apps/A0F7XDUAZ
- Send message
./slack_post.sh -t 'Hello from terminal!'
- Send a message received in a pipe.
echo 'Message via pipe' | ./slack_post.sh # if you want to keep your stdout form script, echo 'Message via pipe' | tee >(./slack_post.sh)
- Customize message
# Send different channel ./slack_post.sh --channel '#random' --username 'Your boss' --icon_emoji ':sunglasses:'
If you want to post full-customized message, generate json text and post it with following command.
curl -X POST -H 'Content-type: application/json' --data '{"text":"Done!"}' $SLACK_WHURL