Last active
October 23, 2017 12:26
-
-
Save x1unix/11d4ec0ee58c1dd27cfe461da8a67648 to your computer and use it in GitHub Desktop.
Slack bash library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # Slack Support Library (v1.0.0) | |
| # System colors | |
| YELLOW='\033[1;33m' | |
| BLUE='\033[1;34m' | |
| RED='\033[0;31m' | |
| NC='\033[0m' | |
| # Main Slack hostname | |
| SLACK_HOST="slack.com" | |
| # Slack protocol | |
| SLACK_PROTO="https" | |
| # Slack subdomain (xxxx.slack.com | |
| SLACK_SUBDOMAIN="" | |
| # Slack API token | |
| SLACK_TOKEN="" | |
| # Slack service hook name | |
| SLACK_SERVICE_NAME="incoming-webhook" | |
| # Default slack channel | |
| SLACK_CHANNEL="" | |
| SLACK_LOG_MASK="[SLACKBOT]" | |
| SLACK_FALSE="FALSE" | |
| SLACK_TRUE="TRUE" | |
| SLACK_OK="ok" | |
| ################################### | |
| # Write message to console | |
| # | |
| # Arguments: | |
| # $1 - Message | |
| ################################### | |
| slack_log() { | |
| echo "$SLACK_LOG_MASK: $1" | |
| } | |
| ################################### | |
| # Write info message to console | |
| # | |
| # Arguments: | |
| # $1 - Message | |
| ################################### | |
| slack_info() { | |
| printf "${BLUE}${SLACK_LOG_MASK} ${1}${NC}\n" | |
| } | |
| ################################### | |
| # Write warn message to console | |
| # | |
| # Arguments: | |
| # $1 - Message | |
| ################################### | |
| slack_warn() { | |
| printf "${YELLOW}${SLACK_LOG_MASK} ${1}${NC}\n" | |
| } | |
| ################################### | |
| # Write warn message to console | |
| # | |
| # Arguments: | |
| # $1 - Message | |
| ################################### | |
| slack_error() { | |
| printf "${RED}${SLACK_LOG_MASK} ${1}${NC}\n" | |
| } | |
| ################################### | |
| # Check slack config | |
| ################################### | |
| slack_check_config() { | |
| if [[ $SLACK_TOKEN == "" ]] | |
| then | |
| slack_warn "Slack token is not specified" | |
| return; | |
| fi | |
| } | |
| ################################### | |
| # Gets Slack API request url | |
| ################################### | |
| slack_get_url() { | |
| echo "$SLACK_PROTO://$SLACK_SUBDOMAIN.$SLACK_HOST/services/hooks/$SLACK_SERVICE_NAME?token=$SLACK_TOKEN" | |
| } | |
| ################################### | |
| # Post message to specific channel | |
| # | |
| # Arguments: | |
| # $1 - Channel name | |
| # $2 - Message | |
| ################################### | |
| slack_post_to() { | |
| local channel=$1 | |
| local text=$2 | |
| local escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" ) | |
| local json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}" | |
| slack_info "Sending message to $channel..." | |
| local url=$(slack_get_url) | |
| local output=$(curl -s -d "payload=$json" "$url") | |
| if [[ $output != "ok" ]]; then | |
| slack_warn "$output" | |
| fi | |
| } | |
| ################################### | |
| # Post message to the default channe; | |
| # | |
| # Arguments: | |
| # $1 - Message | |
| ################################### | |
| slack_post() { | |
| local text=$1 | |
| local channel="$SLACK_CHANNEL" | |
| if [[ $channel == "" ]]; then | |
| slack_warn "Slack default channel is not defined. Message will be posted to #general" | |
| channel="#general" | |
| fi | |
| slack_post_to "$channel" "$text" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| source 'libslack.sh' | |
| source 'slackconfig.sh' | |
| # Post to specific channel | |
| slack_post_to "#general" "Hello World!" | |
| # Post to the default channel | |
| slack_post "Test" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # Slack configuration sample | |
| # Slack subdomain (xxxx.slack.com | |
| SLACK_SUBDOMAIN="myteam" | |
| # Slack API token (this token is fake) | |
| SLACK_TOKEN="heeHtm3wHF59arVtzFZ2e23b" | |
| # Default slack channel | |
| SLACK_CHANNEL="#general" | |
| # Use Jenkins service hook | |
| SLACK_SERVICE_NAME="jenkins-ci" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment