Auto pause streaming media when the phone rings

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