Skip to content

Instantly share code, notes, and snippets.

@BinaryShrub
Last active May 10, 2020 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinaryShrub/72f9668efb40f67cfb33a35771240487 to your computer and use it in GitHub Desktop.
Save BinaryShrub/72f9668efb40f67cfb33a35771240487 to your computer and use it in GitHub Desktop.
Alexa, play white noise
/**
* Alexa Skill Lambda Function that will play an audio file
* on loop, indefinitely - no libraries required! 🎉
*/
const debug = false
exports.handler = async(event) => {
const session = { event, response: error() }
if (event.request.type === "LaunchRequest" ||
(event.request.type === "IntentRequest" &&
event.request.intent.name === "AMAZON.ResumeIntent"))
session.response = play(true)
else if (event.request.type === "AudioPlayer.PlaybackNearlyFinished")
session.response = play()
else if (event.request.type === "IntentRequest" && event.request.intent.name === "AMAZON.PauseIntent")
session.response = off()
if (debug)
console.log(JSON.stringify(session))
return session.response
}
const play = (newSession = false) => {
const playBehavior = newSession ? "REPLACE_ALL" : "REPLACE_ENQUEUED"
return {
"version": "1.0",
"response": {
"directives": [{
type: "AudioPlayer.Play",
playBehavior,
audioItem: {
stream: {
url: "https://srv-file7.gofile.io/download/i0CFz6/whitenoise.mp3",
token: "0",
expectedPreviousToken: null,
offsetInMilliseconds: 0,
}
}
}],
"shouldEndSession": true
}
}
}
const error = () => ({
"version": "1.0",
"response": {
"outputSpeech": {
"type": "SSML",
"ssml": "<speak>uh oh, something went wrong.</speak>"
},
"shouldEndSession": true
}
})
const off = () => ({
"version": "1.0",
"response": {
"directives": [{
type: "AudioPlayer.Stop"
}],
"shouldEndSession": true
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment