Skip to content

Instantly share code, notes, and snippets.

@caseyfw
Created July 12, 2018 00:10
Show Gist options
  • Save caseyfw/30c38d036c90c419b530caa9b132b694 to your computer and use it in GitHub Desktop.
Save caseyfw/30c38d036c90c419b530caa9b132b694 to your computer and use it in GitHub Desktop.
Send slack message from shell script
#!/bin/bash
# Slack bot token to use for auth.
token=xoxb-1234567890-ABCDEFGHIJK1234567890
# Channel ID or user ID to post to.
channel=U12345678
# Name of bot user to post message as.
username=warnbot
# Emoji to use as icon for message.
emoji=":warning:"
function send_message {
curl --silent --output /dev/null \
--data "token=$token" \
--data "channel=$channel" \
--data "as_user=false" \
--data "username=$username" \
--data "icon_emoji=$emoji" \
--data-urlencode "text=$1" \
https://slack.com/api/chat.postMessage
}
# If run non-interactively, send from stdin.
if [ ! -t 0 ]; then
text=$(cat)
send_message "$text"
exit
fi
# If run interactively and no argument is set, show usage.
if [ $# -ne 1 ]; then
echo "Usage:"
echo "slack-me \"<message to send>\""
echo "or"
echo "echo \"blah\" | slack-me"
exit 1
fi
# Otherwise send message passed in argument.
send_message "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment