Skip to content

Instantly share code, notes, and snippets.

@iamnewton
Forked from jessfraz/slack_send_pm.sh
Last active August 1, 2018 05:27
Show Gist options
  • Select an option

  • Save iamnewton/e4d62828df6a8e72bee2 to your computer and use it in GitHub Desktop.

Select an option

Save iamnewton/e4d62828df6a8e72bee2 to your computer and use it in GitHub Desktop.

Revisions

  1. @jfrazelle jfrazelle revised this gist Aug 26, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion slack_send_pm.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ usage(){
    local name=$(basename "$0")

    cat >&2 <<-EOF
    ./${name} [user] [message]
    ${name} [user] [message]
    You will also need to set \$SLACK_TOKEN for authentication.
    You can generate a Web Api Token here: https://api.slack.com/web
  2. @jfrazelle jfrazelle created this gist Aug 26, 2015.
    93 changes: 93 additions & 0 deletions slack_send_pm.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    #!/bin/bash
    set -e

    # Send a private message to someone on slack
    # from the command line.

    # Print a usage message and exit.
    usage(){
    local name=$(basename "$0")

    cat >&2 <<-EOF
    ./${name} [user] [message]
    You will also need to set \$SLACK_TOKEN for authentication.
    You can generate a Web Api Token here: https://api.slack.com/web
    EOF
    exit 1
    }

    [ "$SLACK_TOKEN" ] || usage

    # Print usage with --help or -h
    if [ "$#" -lt 2 ]; then
    usage;
    fi

    nick_to_userid(){
    local nick=$1

    # get the users
    local users=$(curl -sSL -X POST \
    --data-urlencode "token=${SLACK_TOKEN}" \
    --data-urlencode "presence=1" \
    https://slack.com/api/users.list | jq '.members')

    # find the user we want
    local user=$(echo $users | jq '.[] | select(.name == "'${nick}'")')

    if [[ -z "$user" ]]; then
    echo "Could not find user with nick ${nick}."
    return 1
    fi

    # set presence and userid
    presence=$(echo $user | jq --raw-output '.presence')
    userid=$(echo $user | jq --raw-output '.id')

    echo "Sending private message to:"
    echo " User: ${nick}"
    echo " Status: ${presence}"
    }

    user_to_channelid(){
    local user=$1

    # get the im channels
    local channels=$(curl -sSL -X POST \
    --data-urlencode "token=${SLACK_TOKEN}" \
    https://slack.com/api/im.list | jq '.ims')

    # find the user we want
    local channel=$(echo $channels | jq '.[] | select(.user == "'${user}'")')

    if [[ -z "$channel" ]]; then
    echo "Could not find im channel with user id ${user}."
    return 1
    fi

    # set channelid
    channelid=$(echo $channel | jq --raw-output '.id')
    }

    send_message(){
    local nick=$1
    local message=${*:2}

    # get the users id
    nick_to_userid ${nick}

    # get the channel id for the im with this user
    user_to_channelid ${userid}

    local postresp=$(curl -sSL -X POST \
    --data-urlencode "token=${SLACK_TOKEN}" \
    --data-urlencode "channel=${channelid}" \
    --data-urlencode "text=${message}" \
    --data-urlencode "as_user=1" \
    https://slack.com/api/chat.postMessage)

    echo $postresp | jq .
    }

    send_message $@