Skip to content

Instantly share code, notes, and snippets.

@noook
Last active October 22, 2018 22:36
Show Gist options
  • Select an option

  • Save noook/88673469c112211a0172c413a2e4fe8a to your computer and use it in GitHub Desktop.

Select an option

Save noook/88673469c112211a0172c413a2e4fe8a to your computer and use it in GitHub Desktop.
Slack integration to display current song playing in Spotify
#!/bin/bash
APIKEY="TOKEN HERE" # https://api.slack.com/custom-integrations/legacy-tokens
trap onexit INT
function reset() {
echo 'Resetting status'
curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%22status_text%22%3A%22%22%2C%22status_emoji%22%3A%22%22%7D" > /dev/null
}
function onexit() {
echo 'Exitting'
reset
exit
}
declare -i loop=0
while true; do
state=$(osascript -e 'tell application "Spotify" to player state')
date
echo "Spotify: "$state
if [[ "$state" != "playing" ]]; then
((loop++))
if (($loop > 18)) ; then
reset
else
curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$APIKEY"&profile=%7B%22status_text%22%3A%22La%20musique%20est%20en%20pause%22%2C%22status_emoji%22%3A%22%3Adouble_vertical_bar%3A%22%7D" > /dev/null
fi
else
((loop=0))
SONG=$(osascript -e 'tell application "Spotify" to artist of current track & " - " & name of current track')
URLSONG=$(echo "$SONG" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')
echo $SONG
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
fi
sleep 10
done
@noook
Copy link
Author

noook commented Sep 25, 2018

  1. Download the file somewhere on your computer
  2. Replace the token thanks to the provided link at line 2
  3. Considering the script is in the current repertory, make the script executable: chmod +X ./slack-spotify.sh
  4. Still considering the script is in the current repertory, run ./slack-spotify.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment