Skip to content

Instantly share code, notes, and snippets.

@nguyenhoang2009
Forked from andkirby/slack.sh
Created June 25, 2021 18:29
Show Gist options
  • Save nguyenhoang2009/5b253fb83cda94f8dce628cf45c763b9 to your computer and use it in GitHub Desktop.
Save nguyenhoang2009/5b253fb83cda94f8dce628cf45c763b9 to your computer and use it in GitHub Desktop.

Revisions

  1. @andkirby andkirby revised this gist Jun 19, 2020. 1 changed file with 2 additions and 8 deletions.
    10 changes: 2 additions & 8 deletions slack.sh
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ init_params() {
    fi

    APP_SLACK_USERNAME=${APP_SLACK_USERNAME:-$(hostname | cut -d '.' -f 1)}

    APP_SLACK_ICON_EMOJI=${APP_SLACK_ICON_EMOJI:-:slack:}
    if [ -z "${1:-}" ]; then
    echo 'error: Missed required arguments.' > /dev/stderr
    @@ -64,13 +64,7 @@ send_message() {
    local channel=${1}
    echo 'Sending to '${channel}'...'
    curl --silent --data-urlencode \
    "$(printf 'payload={"text":\
    {"type": "mrkdwn", "text": "%s"}, \
    "channel": "%s", \
    "username": "%s", \
    "as_user": "true", \
    "link_names": "true", \
    "icon_emoji": "%s" }' \
    "$(printf 'payload={"text": "%s", "channel": "%s", "username": "%s", "as_user": "true", "link_names": "true", "icon_emoji": "%s" }' \
    "${slack_message}" \
    "${channel}" \
    "${APP_SLACK_USERNAME}" \
  2. @andkirby andkirby revised this gist Jun 19, 2020. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions slack.sh
    Original file line number Diff line number Diff line change
    @@ -64,7 +64,13 @@ send_message() {
    local channel=${1}
    echo 'Sending to '${channel}'...'
    curl --silent --data-urlencode \
    "$(printf 'payload={"text": "%s", "channel": "%s", "username": "%s", "as_user": "true", "link_names": "true", "icon_emoji": "%s" }' \
    "$(printf 'payload={"text":\
    {"type": "mrkdwn", "text": "%s"}, \
    "channel": "%s", \
    "username": "%s", \
    "as_user": "true", \
    "link_names": "true", \
    "icon_emoji": "%s" }' \
    "${slack_message}" \
    "${channel}" \
    "${APP_SLACK_USERNAME}" \
    @@ -104,6 +110,3 @@ else
    slack ${@}
    exit $?
    fi



  3. @andkirby andkirby revised this gist Aug 11, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion slack.sh
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,8 @@ init_params() {
    exit 2
    fi

    APP_SLACK_USERNAME=${APP_SLACK_USERNAME:-$(hostname | cut --delimiter=. --fields=1)}
    APP_SLACK_USERNAME=${APP_SLACK_USERNAME:-$(hostname | cut -d '.' -f 1)}

    APP_SLACK_ICON_EMOJI=${APP_SLACK_ICON_EMOJI:-:slack:}
    if [ -z "${1:-}" ]; then
    echo 'error: Missed required arguments.' > /dev/stderr
  4. @andkirby andkirby revised this gist Oct 18, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions slack.sh
    Original file line number Diff line number Diff line change
    @@ -9,13 +9,13 @@
    # USAGE
    # Send message to slack channel/user
    # Send a message to the channel #ch-01
    # $ bash slack.sh '#ch-01' 'Some message here.'
    # $ slack '#ch-01' 'Some message here.'
    #
    # Send a message to the channel #ch-01 and user @me.
    # $ bash slack.sh '#ch-01,@me' MESSAGE
    # $ slack '#ch-01,@me' MESSAGE
    #
    # Send a message to the default channel (it must be declared in APP_SLACK_CHANNEL).
    # $ bash slack.sh MESSAGE
    # $ slack MESSAGE
    #
    # VARIABLES
    #
  5. @andkirby andkirby revised this gist Jun 20, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion slack.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,8 @@
    # Slack Bash console script for sending messages.
    ####################################################################################
    # Installation
    # $ curl -s https://gist.github.com/andkirby/67a774513215d7ba06384186dd441d9e/raw --output slack.sh
    # $ curl -s https://gist.github.com/andkirby/67a774513215d7ba06384186dd441d9e/raw --output /usr/bin/slack
    # $ chmod +x /usr/bin/slack
    ####################################################################################
    # USAGE
    # Send message to slack channel/user
  6. @andkirby andkirby revised this gist Jun 20, 2017. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions slack.sh
    Original file line number Diff line number Diff line change
    @@ -5,22 +5,25 @@
    # Installation
    # $ curl -s https://gist.github.com/andkirby/67a774513215d7ba06384186dd441d9e/raw --output slack.sh
    ####################################################################################
    # USAGE
    # Send message to slack channel/user
    # Usage:
    # Send a message to channel #ch-01
    # $ bash slack.sh '#ch-01' 'Some message here.'
    # Send a message to the channel #ch-01
    # $ bash slack.sh '#ch-01' 'Some message here.'
    #
    # Send a message to channel #ch-01 and user @me.
    # $ bash slack.sh '#ch-01,@me' MESSAGE
    # Send a message to the channel #ch-01 and user @me.
    # $ bash slack.sh '#ch-01,@me' MESSAGE
    #
    # Send a message to the default channel (it must be declared in APP_SLACK_CHANNEL).
    # $ bash slack.sh MESSAGE
    # Send a message to the default channel (it must be declared in APP_SLACK_CHANNEL).
    # $ bash slack.sh MESSAGE
    #
    # VARIABLES
    #
    # Please declare environment variables:
    # - APP_SLACK_WEBHOOK
    # - APP_SLACK_CHANNEL (optional)
    # - APP_SLACK_USERNAME (optional)
    # - APP_SLACK_ICON_EMOJI (optional)
    # You may also declare them in ~/.slackrc file.
    ####################################################################################

    set -o pipefail
  7. @andkirby andkirby revised this gist Jun 20, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions slack.sh
    Original file line number Diff line number Diff line change
    @@ -83,8 +83,8 @@ slack() {

    cd ${__dir}

    if [ -f $(cd; pwd)/.slackshrc ]; then
    . $(cd; pwd)/.slackshrc
    if [ -f $(cd; pwd)/.slackrc ]; then
    . $(cd; pwd)/.slackrc
    fi

    declare -a slack_channels
  8. @andkirby andkirby revised this gist Jun 20, 2017. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions slack.sh
    Original file line number Diff line number Diff line change
    @@ -28,19 +28,6 @@ set -o errexit
    set -o nounset
    #set -o xtrace

    # Set magic variables for current file & dir
    __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
    readonly __dir __file

    cd ${__dir}

    if [ -f $(cd; pwd)/.slackshrc ]; then
    . $(cd; pwd)/.slackshrc
    fi

    declare -a slack_channels

    init_params() {
    # you may declare ENV vars in /etc/profile.d/slack.sh
    if [ -z "${APP_SLACK_WEBHOOK:-}" ]; then
    @@ -89,6 +76,19 @@ send_message_to_channels() {
    }

    slack() {
    # Set magic variables for current file & dir
    __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
    readonly __dir __file

    cd ${__dir}

    if [ -f $(cd; pwd)/.slackshrc ]; then
    . $(cd; pwd)/.slackshrc
    fi

    declare -a slack_channels

    init_params ${@}
    send_message_to_channels
    }
  9. @andkirby andkirby revised this gist Jun 20, 2017. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions slack.sh
    Original file line number Diff line number Diff line change
    @@ -88,8 +88,17 @@ send_message_to_channels() {
    done
    }

    init_params ${@}
    send_message_to_channels
    slack() {
    init_params ${@}
    send_message_to_channels
    }

    if [ "${BASH_SOURCE[0]:-}" != "${0}" ]; then
    export -f slack
    else
    slack ${@}
    exit $?
    fi



  10. @andkirby andkirby revised this gist Jun 20, 2017. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion slack.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    #!/usr/bin/env bash

    ####################################################################################
    # Slack Bash console script for sending messages.
    ####################################################################################
    # Installation
    # $ curl -s https://gist.github.com/andkirby/67a774513215d7ba06384186dd441d9e/raw --output slack.sh
    ####################################################################################
    # Send message to slack channel/user
    # Usage:
  11. @andkirby andkirby revised this gist Jun 19, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion slack.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    # Send a message to channel #ch-01 and user @me.
    # $ bash slack.sh '#ch-01,@me' MESSAGE
    #
    # Send a message to the default channel.
    # Send a message to the default channel (it must be declared in APP_SLACK_CHANNEL).
    # $ bash slack.sh MESSAGE
    #
    # Please declare environment variables:
  12. @andkirby andkirby revised this gist Jun 19, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion slack.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/usr/bin/env bash

    ####################################################################################
    # Notify about deployment to slack channel
    # Send message to slack channel/user
    # Usage:
    # Send a message to channel #ch-01
    # $ bash slack.sh '#ch-01' 'Some message here.'
  13. @andkirby andkirby revised this gist Jun 19, 2017. No changes.
  14. @andkirby andkirby created this gist Jun 18, 2017.
    91 changes: 91 additions & 0 deletions slack.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    #!/usr/bin/env bash

    ####################################################################################
    # Notify about deployment to slack channel
    # Usage:
    # Send a message to channel #ch-01
    # $ bash slack.sh '#ch-01' 'Some message here.'
    #
    # Send a message to channel #ch-01 and user @me.
    # $ bash slack.sh '#ch-01,@me' MESSAGE
    #
    # Send a message to the default channel.
    # $ bash slack.sh MESSAGE
    #
    # Please declare environment variables:
    # - APP_SLACK_WEBHOOK
    # - APP_SLACK_CHANNEL (optional)
    # - APP_SLACK_USERNAME (optional)
    # - APP_SLACK_ICON_EMOJI (optional)
    ####################################################################################

    set -o pipefail
    set -o errexit
    set -o nounset
    #set -o xtrace

    # Set magic variables for current file & dir
    __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
    readonly __dir __file

    cd ${__dir}

    if [ -f $(cd; pwd)/.slackshrc ]; then
    . $(cd; pwd)/.slackshrc
    fi

    declare -a slack_channels

    init_params() {
    # you may declare ENV vars in /etc/profile.d/slack.sh
    if [ -z "${APP_SLACK_WEBHOOK:-}" ]; then
    echo 'error: Please configure Slack environment variable: ' > /dev/stderr
    echo ' APP_SLACK_WEBHOOK' > /dev/stderr
    exit 2
    fi

    APP_SLACK_USERNAME=${APP_SLACK_USERNAME:-$(hostname | cut --delimiter=. --fields=1)}
    APP_SLACK_ICON_EMOJI=${APP_SLACK_ICON_EMOJI:-:slack:}
    if [ -z "${1:-}" ]; then
    echo 'error: Missed required arguments.' > /dev/stderr
    echo 'note: Please follow this example:' > /dev/stderr
    echo ' $ slack.sh "#CHANNEL1,CHANNEL2" Some message here. ' > /dev/stderr
    exit 3
    fi

    slack_channels=(${APP_SLACK_CHANNEL:-})
    if [ "${1::1}" == '#' ] || [ "${1::1}" == '@' ]; then
    # explode by comma
    IFS=',' read -r -a slack_channels <<< "${1}"
    shift
    fi
    slack_message=${@}
    }


    send_message() {
    local channel=${1}
    echo 'Sending to '${channel}'...'
    curl --silent --data-urlencode \
    "$(printf 'payload={"text": "%s", "channel": "%s", "username": "%s", "as_user": "true", "link_names": "true", "icon_emoji": "%s" }' \
    "${slack_message}" \
    "${channel}" \
    "${APP_SLACK_USERNAME}" \
    "${APP_SLACK_ICON_EMOJI}" \
    )" \
    ${APP_SLACK_WEBHOOK} || true
    echo
    }

    send_message_to_channels() {
    for channel in "${slack_channels[@]:-}"; do
    send_message "${channel}"
    done
    }

    init_params ${@}
    send_message_to_channels