Auto pause streaming media when the phone rings

[See post #4 for a working solution]

Short #FridayFun this week inspired by Mr. White. Not the character in Reservoir Dogs, but the slightly less lethal dude here at Sangoma, @mwhite. This started out with him providing this link and asking how to pause Spotify audio when the phone rings: Pause Playback | Spotify for Developers

Which as it turns out is not all that difficult. If you have Spotify premium, you can step thru the process to get an API token, grant privileges for controlling audio, and the end result is a crazy long curl expression that looks something like this:

curl -X "PUT" "https://api.spotify.com/v1/me/player/pause" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer Long_Random_Token_String" -H "content-length: 0"

The linked page above documents this, but I ended up having to add the content-length header to make it work correctly. Translating to dialplan, the following placed in extensions_custom.conf

[from-internal-custom]
exten => 4508,1,Set(token=Long_Random_Token_String)    ;edit to use actual token
exten => 4508,n,System(curl -X "PUT" "https://api.spotify.com/v1/me/player/pause" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer ${token}" -H "content-length: 0")
exten => 4508,n,Hangup

So now you have a sequence of digits (4508, edit to suit) that won’t answer the channel, such that whenever they get dialed, it will pause audio for that specific Spotify user. You can add this dial string to ring groups, queues or FMFM settings on the extension such then whenever something causes a phone to ring, the pause API call is sent. For testing, you just dial 4508 directly. I don’t see an API for unpause, which is just as well, the dialplan to make that work would add considerable complexity.

As an aside, I tried to translate the above system curl to a proper Asterisk Curl Application, but I don’t know how (or if) one can do a PUT using Asterisk Curl. Would love for someone to educate me if that’s possible.

This would be trivially easy to add to a Superfecta Send-to module. I don’t think it would have broad appeal, but would eliminate the necessity of custom dialplan. There is already one for pausing media player, the name of which escapes me at the moment.

1 Like

This is great – thanks! However, I used it this morning for a bit and it worked great. Came back after lunch and it wasn’t working. Did it using curl manually and it dumped out an error: “The access token expired.”

How long is the token valid for? Is there a way to get a permanent token?

I generated a new token and it is working again – but this doesn’t work well if the token is only valid during a current playing session, of course. Am I missing something?

EDIT: Confirmed that the tokens are only valid for 1 hour. Kinda makes this not great unless we can find a way to regenerate valid tokens…

1 Like

I’m seeing the same, and now get to admit my ignorance here, I didn’t read up on how the API works at all. It looks like there are different API flows you can employ depending on application; I may have chose the wrong one. I will review it over the weekend to see if I can get it working properly.

Looks like the token time is one hour. This may be better as an agi/ari script. With other projects I used guzzle middleware that takes the 401 and tries to refresh. You do need to store the token and refresh token

So the general flow:

Oauth token → 401 → send refresh, get new tokens…

2 Likes

I had a chance to review the various Spotify API options, and I’m not seeing any practical way of renewing an auth token with pure dialplan, as James noted this is a task for an AGI script. But building on James’ idea of using middleware, it’s can be done pretty easily with IFTTT.com (if this then that), a free tier is available. There is already an IFTTT module for Superfecta.

In IFTTT, you want to create an applet that links a webhook to Spotify pause. I’m using the same google auth for both Spotify and for IFTTT, so Spotify applet config is automatic. I’m not sure if that is a requirement for this method to work or not. Once the applet is created, you just need to determine the ifttt webhook, which will take this form:

https://maker.ifttt.com/trigger/<applet_name>/json/with/key/<user_key>

And the Asterisk dialplan for the feature code to pause audio is this:

exten => 4508,1,Set(key=redacted)               ; key provided by IFTTT
exten => 4508,1,Set(applet=spotify%20pause)     ; applet name urlencoded
exten => 4508,n,Set(result=${CURL(https://maker.ifttt.com/trigger/${applet}/json/with/key/${key})})       ; generic URL for ifttt webhook
exten => 4508,n,Hangup

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.