Skip to content

Instantly share code, notes, and snippets.

@IndrekV
Forked from yoshinari-nomura/ame
Created July 4, 2019 12:21
Show Gist options
  • Save IndrekV/483e47ad5d5e96d0ec5fa6541804cc43 to your computer and use it in GitHub Desktop.
Save IndrekV/483e47ad5d5e96d0ec5fa6541804cc43 to your computer and use it in GitHub Desktop.
Set your status in Slack with CLI
#!/bin/bash
################################################################
## Usage
usage() {
echo "ame [-c CONFIG_FILE] [-t TEAM] ICON TEXT"
echo " -c Set config file name."
echo " -t Set Slack team name."
echo "examples:"
echo " ame :office: \"I'm at office\""
echo " ame :house: \"I'm home\""
} >&2
print_error() {
printf "Error: $@\n"
} >&2
################################################################
## Update Slack Status
update_slack_status () {
local token="$1"
local emoji="$2"
local stext="$3"
local response=$(curl -s -S -X POST \
-d "token=$token" \
--data-urlencode "profile={\"status_text\": \"$stext\", \"status_emoji\": \"$emoji\"}" \
https://slack.com/api/users.profile.set \
| sed -n 's/{"ok":false,"error":"\([^"]*\)".*/\1/p')
echo "$response" | tr '_' ' '
}
################################################################
## Parse option
OPT_CONF="$HOME/.config/ame/config.sh"
while getopts "c:dt:" flag
do
case $flag in
# getopts sets '?' to flag on error.
\?|h) OPT_ERROR=1
;;
c) OPT_CONF="$OPTARG"
;;
d) OPT_DEBUG="true"
;;
t) OPT_TEAM="$OPTARG"
;;
esac
done
shift $(( $OPTIND - 1 ))
# unknown option check
if [ "$OPT_ERROR" = 1 -o $# -ne 2 ]; then
usage
exit -1
fi
################################################################
## Read Config
#
# config.sh Example:
#
# DEFAULT_TEAM="yourteam"
# TOKEN_YOURTEAM="xoxp-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxx-xxxxxx"
# TOKEN_ANOTERTEAM="xoxp-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxx-xxxxxx"
#
if [ -f "$OPT_CONF" ]; then
source "$OPT_CONF"
else
print_error "config $OPT_CONF not found."
exit -1
fi
################################################################
## Set Slack API Token
if [ -z "$OPT_TEAM" ]; then
OPT_TEAM="$DEFAULT_TEAM"
fi
TOKEN_NAME=TOKEN_$(echo "$OPT_TEAM" | tr a-z A-Z)
TOKEN=${!TOKEN_NAME}
if [ -z "$TOKEN" ]; then
print_error "No API Token found for $OPT_TEAM in $OPT_CONF."
exit -1
fi
################################################################
## Main
if [ -n "$OPT_DEBUG" ]; then
echo "update_slack_status \"$TOKEN\" \"$1\" \"$2\""
exit 0
fi
error_message=$(update_slack_status "$TOKEN" "$1" "$2")
if [ -n "$error_message" ]; then
print_error "$error_message."
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment