Skip to content

Instantly share code, notes, and snippets.

@smokey42
Last active April 16, 2020 08:50
Show Gist options
  • Save smokey42/07b019e74cfb2844b229bc6989209958 to your computer and use it in GitHub Desktop.
Save smokey42/07b019e74cfb2844b229bc6989209958 to your computer and use it in GitHub Desktop.
Set Slack status when locking screen under Linux
# put under ~/.config/screen-lock-notify
#
# Obtain one here: https://api.slack.com/legacy/custom-integrations/legacy-tokens
#
# Beware: New tokens will only be issued until May.
#
SLACK_LEGACY_TOKEN="xoxp-..."
# Figure out the interface by issuing:
#
# dbus-monitor --session "type='signal'"
#
# in a terminal, and then locking and unlocking your screen.
#
DBUS_INTERFACE=org.cinnamon.ScreenSaver
# DBUS_INTERFACE=org.gnome.ScreenSaver
# DBUS_INTERFACE=org.mate.ScreenSaver
# DBUS_INTERFACE=org.xfce.ScreenSaver
#
# Can be any channel or user.
#
HISTORY_CHANNEL="..."
AWAY_STATUS="away from keyboard"
AWAY_ICON=":coffee:"
PRESENT_STATUS="working from home"
PRESENT_ICON=":house_with_garden:"
#!/bin/bash
#
# Script to set status on Slack.
#
# Load
#
# SLACK_LEGACY_TOKEN
# DBUS_INTERFACE
# HISTORY_CHANNEL
# AWAY_STATUS
# AWAY_ICON
# PRESENT_STATUS
# PRESENT_ICON
source ~/.config/screen-lock-notify
api_call() {
curl \
-X POST -H "Authorization: Bearer $SLACK_LEGACY_TOKEN" \
-H 'Content-Type: application/json; charset="utf-8"' \
--data "$2" https://slack.com/api/"$1"
echo
}
slack_set_status() {
local presence="$1"
local status_text="$2"
local status_emoji="$3"
api_call users.setPresence "{\"presence\":\"$presence\"}"
api_call users.profile.set \
"{\"profile\": {\"status_text\": \"$status_text\", \"status_emoji\": \"$status_emoji\"}}"
if [ ! -z $HISTORY_CHANNEL ]; then
local msg
if [ -z "$status_text" ]; then
msg="I'm back"
else
msg=$status_text
fi
api_call chat.postMessage \
"{\"channel\":\"$HISTORY_CHANNEL\", \"text\":\"$msg\", \"as_user\":\"true\"}"
fi
}
screen_locked() {
echo SCREEN_LOCKED
slack_set_status away "$AWAY_STATUS" "$AWAY_ICON"
}
screen_unlocked() {
echo SCREEN_UNLOCKED
slack_set_status auto "$PRESENT_STATUS" "$PRESENT_ICON"
}
dbus-monitor --session "type='signal',interface='$DBUS_INTERFACE'" |
while read signal; do
case "$signal" in
*"boolean true"*) screen_locked;;
*"boolean false"*) screen_unlocked;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment