Last active
January 13, 2016 14:54
-
-
Save rochoa/d5491fdf64542f5304ba to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Configuration ############################################# | |
# For Slack tokens go to https://api.slack.com/#auth | |
SLACK_TOKEN="" | |
# In seconds | |
AWAY_AFTER=120 | |
CHECK_EVERY=10 | |
# End configuration ######################################### | |
LAST_PRESENCE='' | |
function presence_set { | |
if [[ ${1} -eq 1 ]] | |
then | |
PRESENCE='away' | |
else | |
PRESENCE='active' | |
fi | |
if [ "${LAST_PRESENCE}" != "${PRESENCE}" ] | |
then | |
curl -s "https://slack.com/api/presence.set?token=${SLACK_TOKEN}&presence=${PRESENCE}&pretty=1" > /tmp/slack_presence.log | |
RESULT=$? | |
if [[ ${CODERESULT} -ne 0 ]] | |
then | |
echo "Failed to change Slack presence to ${PRESENCE}" > /tmp/slack_presence.log | |
else | |
echo "Slack presence to ${PRESENCE}" > /tmp/slack_presence.log | |
LAST_PRESENCE=${PRESENCE} | |
fi | |
fi | |
} | |
while true; | |
do | |
LAST_INPUT_TIME_ELAPSED=`ioreg -c IOHIDSystem | perl -lane 'if(/Idle/) {print $F[-1]/1000000000; last;}'` | |
IS_AWAY=`bc <<< "$LAST_INPUT_TIME_ELAPSED > ${AWAY_AFTER}"` | |
presence_set ${IS_AWAY} | |
sleep ${CHECK_EVERY} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment