Skip to content

Instantly share code, notes, and snippets.

@andy-berry-dev
Forked from yoshinari-nomura/ame
Last active September 3, 2018 20:26
Show Gist options
  • Save andy-berry-dev/c04dba086ddabc7f69d97fc8c6169dd9 to your computer and use it in GitHub Desktop.
Save andy-berry-dev/c04dba086ddabc7f69d97fc8c6169dd9 to your computer and use it in GitHub Desktop.
Set your status in Slack with CLI & auto update it based on wifi SSID
# save to ~/.config/slackstatus/config.sh
export DEFAULT_TEAM="SomeTeam"
export TOKEN_SOMETEAM="<TOKEN>"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!--
Adapted from https://www.schiff.io/blog/2017/08/31/automating-slack-status-launchd
Load with: launchctl load ~/Library/LaunchAgents/local.slackstatus.plist
Save to: ~/Library/LaunchAgents/local.slackstatus.plist
-->
<key>Label</key>
<string>local.slackstatus</string>
<key>LowPriorityIO</key>
<true/>
<key>Program</key>
<string>/Users/andy/Library/Application Support/local.slackstatus/wifi-slackstatus.sh</string>
<key>WatchPaths</key>
<array>
<string>/etc/resolv.conf</string>
<string>/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist</string>
<string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
#!/bin/bash
# adapted from https://gist.github.com/yoshinari-nomura/37366e913db502672f5de70553c5cc8e
# save to ~/bin/slackstatus
################################################################
## Usage
usage() {
echo "slackstatus [-c CONFIG_FILE] [-t TEAM] [-e EXPIRY] ICON TEXT"
echo " -c Set config file name."
echo " -t Set Slack team name."
echo " -e Set status expiry (seconds from now)"
echo "examples:"
echo " slackstatus :office: \"I'm at office\""
echo " slackstatus :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 expiration="$4"
local response=$(curl -s -S -X POST \
-d "token=$token" \
--data-urlencode "profile={\"status_text\": \"$stext\", \"status_emoji\": \"$emoji\", \"status_expiration\": $expiration}" \
https://slack.com/api/users.profile.set \
| sed -n 's/{"ok":false,"error":"\([^"]*\)".*/\1/p')
echo "$response" | tr '_' ' '
}
################################################################
## Parse option
OPT_CONF="$HOME/.config/slackstatus/config.sh"
TOKEN_TTL=$(expr 60 \* 60 \* 24)
while getopts "c:dt:e:" 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"
;;
e) OPT_EXPIRY="$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
if [ -n "$OPT_EXPIRY" ]; then
TOKEN_TTL=${OPT_EXPIRY}
fi
TOKEN_EXPIRY_TIME=$(expr $(date '+%s') + ${TOKEN_TTL})
################################################################
## Main
if [ -n "$OPT_DEBUG" ]; then
echo "update_slack_status \"$TOKEN\" \"$1\" \"$2\" \"$TOKEN_EXPIRY_TIME\""
exit 0
fi
error_message=$(update_slack_status "$TOKEN" "$1" "$2" "$TOKEN_EXPIRY_TIME")
if [ -n "$error_message" ]; then
print_error "$error_message."
exit 1
fi
exit 0
#!/bin/bash
# adapted from https://www.schiff.io/blog/2017/08/31/automating-slack-status-launchd
# save to ~/Library/Application Support/local.slackstatus/wifi-slackstatus.sh
pushd $(dirname $0)/.. >/dev/null
SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'`
STATUS_TEXT=""
STATUS_EMOJI=""
if [ "${SSID}" == "WORK_SSID" ]; then
STATUS_EMOJI=":classical_building:"
STATUS_TEXT="Working from the office"
elif [ "${SSID}" == "HOME_SSID" ]; then
STATUS_EMOJI=":house:"
STATUS_TEXT="Working from home"
elif [ "${SSID}" == "COFFEE_SHOP_SSID_1" ] || [ "${SSID}" == "COFFEE_SHOP_SSID_2" ]; then
STATUS_EMOJI=":coffee: "
STATUS_TEXT="Working from coffee shopz"
elif [ -n "${SSID}" ]; then
echo "Not setting any Slack status"
fi
if [ -n "${STATUS_TEXT}" ];
then
echo "Setting slack status to ${STATUS_EMOJI} \"${STATUS_TEXT}\""
slackstatus -e $(expr 60 \* 60 \* 8) ${STATUS_EMOJI} "${STATUS_TEXT}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment