-
-
Save jgamblin/9701ed50398d138c65ead316b5d11b26 to your computer and use it in GitHub Desktop.
#!/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 |
Here's a way to have a channel that shares the now playing for any arbitrary number of users. This is really nice for medium-sized organizations, but requires you to enable a webhook. https://github.com/RasPat1/spotify-slack-integration-webhook
# These can be passed in as args:
#
# #########################################
# osascript spotify.scpt WEBHOOK_URL USERNAME CHANNEL_NAME
# #########################################
#
property webhookURL : "<WEBHOOK URL>"
property userName : "<USERNAME>"
property channelName : "#musical-chairs"
# globals
property emojiName : "musical_note"
property possibleAppList : {"Spotify", "iTunes", "Rdio"}
property installedAppList : {"Spotify"}
property chosenApp : "Spotify"
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
# content
tell application "Spotify"
set current_track to null
set current_artist to null
set current_album to null
repeat until application "Spotify" is not running
if player state is playing then
set track_name to name of current track
set track_artist to artist of current track
set track_album to album of current track
if track_name ≠ current_track then
set current_track to name of current track
set current_artist to artist of current track
set current_album to album of current track
set message to current_track & " - " & current_artist & " - " & userName
set encodedMessage to my replace_chars(message, "\"", "\\\"")
# do shell script "curl -X POST --data-urlencode payload={\"channel\": \"#music\", \"username\": \"nowplaying\", \"attachments\": [ { \"fallback\": \"$USERNAME is now playing: " & encodedMessage & "\", \"pretext\": \"$USERNAME is now playing:\", \"text\": \"" & encodedMessage & "\", \"color\": \"#7CD197\"} ], \"icon_emoji\": \":musical_note:\"} https://hooks.slack.com/services/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"
do shell script "curl -X POST --data-urlencode 'payload={\"channel\": \"" & channelName & "\", \"text\": \"" & encodedMessage & "\", \"icon_emoji\": \":" & emojiName & ":\"}' " & webhookURL
end if
delay 5
end if
end repeat
end tell
end script
on run argv
if (count of argv) > 0 then
tell myScript
set its webhookURL to item 1 of argv
end tell
end if
if (count of argv) > 1 then
tell myScript
set its userName to item 2 of argv
end tell
end if
if (count of argv) > 2 then
tell myScript
set its channelName to item 3 of argv
end tell
end if
run myScript
end run```
Does anyone have an update for this now that legacy tokens are deprecated in slack? I was planning to follow @tiagocasanovapt 's readme/guide, but it seems like that will no longer work.
@crevulus this might interest you https://github.com/giorgimode/SpotMyStatus/
@crevulus this might interest you https://github.com/giorgimode/SpotMyStatus/
Thanks! Looks soooo good!
@crevulus this might interest you https://github.com/giorgimode/SpotMyStatus/
Works good, ty :)
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
For anyone looking to use this behind an enterprise slack. I created a script based off @agologan that utilizes a custom osascript from @samknight (https://github.com/samknight/slack_applescript)
@fastfrwrd , you might like this.