Created
April 19, 2017 01:10
-
-
Save jgamblin/9701ed50398d138c65ead316b5d11b26 to your computer and use it in GitHub Desktop.
A Script To Set Current Spotify Song As Slack Status
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 | |
APIKEY="From Here https://api.slack.com/custom-integrations/legacy-tokens" | |
SONG=$(osascript -e 'tell application "Spotify" to name of current track as string') | |
URLSONG=$(echo "$SONG" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"') | |
while true | |
do | |
curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%22status_text%22%3A%22"$URLSONG"%22%2C%22status_emoji%22%3A%22%3Amusical_note%3A%22%7D" > /dev/null | |
sleep 60 | |
done |
My contribution using the new token format and hopefully very clear and readable:
# Get this token by:
# - Creating a Slack app https://api.slack.com/apps
# - Select Permissions > Scopes > User Token Scopes and add `users.profile:write`
# - Scroll up and select Install to Workspace under OAuth Tokens for Your Workspace
# - Copy User OAuth Token under OAuth Tokens for Your Workspace
TOKEN=$(cat slack.pat)
while true
do
echo "Stamp:"
date -Iseconds
RUNNING=$(pgrep -lf Spotify)
# Bail if Spotify is not running to prevent `osascript` from launching it
if [ -z "$RUNNING" ];
then
echo "Spotify is off, waiting a minute…"
sleep 60
continue
fi
# See `/Applications/Spotify.app/Contents/Resources/Spotify.sdef`
ARTIST=$(osascript -e 'tell application "Spotify" to artist of current track')
SONG=$(osascript -e 'tell application "Spotify" to name of current track')
# Bail if Spotify is not playing anything (but is running)
if [ -z "$ARTIST" ] || [ -z "$SONG" ];
then
echo "Spotify is not playing anything, waiting a minute…"
sleep 60
continue
fi
STAMP=$(date -v"+5M" +%s)
JSON=$(echo '{}' | jq --arg SONG "$SONG" --arg ARTIST "$ARTIST" --arg STAMP $STAMP '.profile.status_text=$ARTIST+" - "+$SONG | .profile.status_emoji=":headphones:" | .profile.status_expiration=($STAMP|tonumber)')
echo "Request:"
echo "$JSON" | jq '.'
# See https://api.slack.com/docs/presence-and-status#custom_status
echo "Response:"
curl -X POST --data "$JSON" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json; charset=utf-8" --no-progress-meter https://slack.com/api/users.profile.set | jq 'del(.profile)'
# Wait for a minute in between the status updates
sleep 60
echo ""
done
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works good, ty :)