-
-
Save yoshinari-nomura/37366e913db502672f5de70553c5cc8e to your computer and use it in GitHub Desktop.
Set your status in Slack with CLI
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
#!/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
Put ~/.config/ame/config.sh like:
and do:
Note: TOKEN_YOURTEAM should be upper-case while the value of DEFAULT_TEAM is lower-case.
Your token can be taken from:
https://api.slack.com/custom-integrations/legacy-tokens
Japanese blog article about this gist:
http://quickhack.net/nom/blog/2017-04-19-set-your-status-in-slack-with-cli.html