Skip to content

Instantly share code, notes, and snippets.

@aroyrev
Forked from mattak/slackpost.sh
Created April 26, 2019 00:19
Show Gist options
  • Save aroyrev/3f1e4c4ea05156d33932c9455d39b829 to your computer and use it in GitHub Desktop.
Save aroyrev/3f1e4c4ea05156d33932c9455d39b829 to your computer and use it in GitHub Desktop.

Revisions

  1. @mattak mattak revised this gist Feb 20, 2015. 1 changed file with 12 additions and 14 deletions.
    26 changes: 12 additions & 14 deletions slackpost.sh
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,21 @@
    #!bash -e

    TOKEN=
    CHANNEL=
    MESSAGE=
    NICK=
    TOKEN= # slack token is generate from: https://api.slack.com/web
    CHANNEL= # name of channels or group
    MESSAGE= # message
    NICK= # bot name
    IS_PRIVATE= # 1 or 0

    # base info

    AUTH_RESPONSE=$(curl https://slack.com/api/auth.test)
    TEAM=$(echo $AUTH_RESPONSE | jq '.team')
    TEAM_ID=$(echo $AUTH_RESPONSE | jq '.team_id')
    USER_ID=$(echo $AUTH_RESPONSE | jq '.user_id')
    URL=$(echo $AUTH_RESPONSE | jq '.url')
    USER=$(echo $AUTH_RESPONSE | jq '.user')
    if [ $IS_PRIVATE -eq 1 ]; then
    API_TYPE=groups
    else
    API_TYPE=channels
    fi

    # channel info

    CHANNELS_RESPONSE=$(curl "https://slack.com/api/groups.list?token=$TOKEN")
    CHANNEL_ID=$(echo $CHANNELS_RESPONSE | jq '.groups[]' | jq "if .name == \"$CHANNEL\" then .id else null end" -M -c -r | grep -v null)
    CHANNELS_RESPONSE=$(curl "https://slack.com/api/$API_TYPE.list?token=$TOKEN")
    CHANNEL_ID=$(echo $CHANNELS_RESPONSE | jq ".${API_TYPE}[]" | jq "if .name == \"$CHANNEL\" then .id else null end" -M -c -r | grep -v null)

    # post message

  2. @mattak mattak created this gist Feb 20, 2015.
    29 changes: 29 additions & 0 deletions slackpost.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!bash -e

    TOKEN=
    CHANNEL=
    MESSAGE=
    NICK=

    # base info

    AUTH_RESPONSE=$(curl https://slack.com/api/auth.test)
    TEAM=$(echo $AUTH_RESPONSE | jq '.team')
    TEAM_ID=$(echo $AUTH_RESPONSE | jq '.team_id')
    USER_ID=$(echo $AUTH_RESPONSE | jq '.user_id')
    URL=$(echo $AUTH_RESPONSE | jq '.url')
    USER=$(echo $AUTH_RESPONSE | jq '.user')

    # channel info

    CHANNELS_RESPONSE=$(curl "https://slack.com/api/groups.list?token=$TOKEN")
    CHANNEL_ID=$(echo $CHANNELS_RESPONSE | jq '.groups[]' | jq "if .name == \"$CHANNEL\" then .id else null end" -M -c -r | grep -v null)

    # post message

    curl -X POST \
    -F token=$TOKEN \
    -F channel=$CHANNEL_ID \
    -F "text=$MESSAGE" \
    -F username=$NICK \
    https://slack.com/api/chat.postMessage