Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My Lambda Code for Alexa with support for the AudioPlayer
exports.handler = function (event, context)
{
try
{
if (event.session.application.applicationId !== "YOUR APP ID")
{
context.fail("Invalid Application ID");
}
if (event.request.type === "LaunchRequest")
{
onLaunch(event.request,
event.session,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
});
}
else if (event.request.type === "IntentRequest")
{
onIntent(event.request,
event.session,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
}, event );
}
else if (event.request.type === "SessionEndedRequest")
{
onSessionEnded(event.request, event.session);
context.succeed();
}
else if( event.request.type.substring(0,11) === 'AudioPlayer' || event.request.type.substring(0,18) === 'PlaybackController' )
{
pingBTwithAlexaEvent( event.request.type, event, callback );
}
}
catch (e)
{
context.fail("Exception: " + e);
}
};
/**
* Called when the user launches the skill without specifying what they want.
*/
function onLaunch(launchRequest, session, callback)
{
getWelcomeResponse(callback);
}
/**
* Called when the user specifies an intent for this skill.
*/
function onIntent(intentRequest, session, callback, event)
{
var intentName = intentRequest.intent.name;
var userId = '';
if( event.context )
{
userId = event.context.System.user.userId;
}
else if(event.session)
{
userId = event.session.user.userId;
}
if ( "AMAZON.HelpIntent" === intentName )
{
getWelcomeResponse(callback);
}
else if ("AMAZON.StopIntent" === intentName || "AMAZON.CancelIntent" === intentName)
{
callback({}, buildAudioStopResponse(true));
}
else if ("AMAZON.PauseIntent" === intentName )
{
var offsetInMilliseconds = 0;
if( event.context && event.context.AudioPlayer)
{
offsetInMilliseconds = event.context.AudioPlayer.offsetInMilliseconds;
}
pingReturn("https://bibletalk.tv/api/alexa.php?intent=AMAZON.PauseIntent&offset=" + offsetInMilliseconds + "&userId=" + userId, callback);
}
else if (
"AMAZON.ResumeIntent" === intentName ||
"AMAZON.NextIntent" === intentName ||
"AMAZON.PreviousIntent" === intentName ||
"AMAZON.StartOverIntent" === intentName
)
{
pingReturn("https://bibletalk.tv/api/alexa.php?intent=" + intentName + "&userId=" + userId, callback);
}
else if( "AMAZON.ShuffleOnIntent" === intentName || "AMAZON.ShuffleOffIntent" === intentName )
{
simpleResponse("Sorry, I can’t shuffle lessons yet", callback);
}
else if( "AMAZON.LoopOnIntent" === intentName || "AMAZON.LoopOffIntent" === intentName )
{
simpleResponse("Sorry, I can’t loop lessons yet", callback);
}
else if( "PlayAnItemInSeries" === intentName )
{
pingReturn("https://bibletalk.tv/api/alexa.php?intent=" + intentName + "&series=" + intentRequest.intent.slots["Series"].value + "&number=" + intentRequest.intent.slots["Lesson"].value + "&userId=" + userId, callback);
}
else if( "PlayAnything" === intentName )
{
pingReturn("https://bibletalk.tv/api/alexa.php?intent=PlayAnything&keyword=" + intentRequest.intent.slots["Keyword"].value + "&userId=" + userId, callback);
}
else
{
pingReturn("https://bibletalk.tv/api/alexa.php?intent=" + intentName + "&userId=" + userId, callback);
}
}
function pingReturn( url, callback )
{
var http = require("https");
var request = http.get(url, function (response)
{
var buffer = "", data, route;
response.on("data", function (chunk) { buffer += chunk; });
response.on("end", function (err)
{
callback({}, JSON.parse(buffer) );
});
});
}
function pingBTwithAlexaEvent( eventName, event, callback )
{
var userId = '';
var offsetInMilliseconds = 0;
if( event.context )
{
userId = event.context.System.user.userId;
if( event.context.AudioPlayer )
{
offsetInMilliseconds = event.context.AudioPlayer.offsetInMilliseconds;
}
}
else if (event.session)
{
userId = event.session.user.userId;
}
// PING BT
var http = require("https");
var url = "https://bibletalk.tv/api/alexa.php?event_name=" + eventName + "&offset=" + offsetInMilliseconds + "&userId=" + userId;
var request = http.get(url, function (response)
{
var buffer = "", data, route;
response.on("data", function (chunk) { buffer += chunk; });
response.on("end", function (err)
{
callback({}, JSON.parse(buffer) );
});
});
}
/**
* Called when the user ends the session.
* Is not called when the skill returns shouldEndSession=true.
*/
function onSessionEnded(sessionEndedRequest, session)
{
//console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId + ", sessionId=" + session.sessionId);
}
// --------------- Functions that control the skill's behavior -----------------------
function simpleResponse(text, callback)
{
callback({}, buildSpeechletResponse(text, text, text, false));
}
function getWelcomeResponse(callback)
{
var sessionAttributes = {};
var cardTitle = "Welcome";
var speechOutput = "Welcome to BibleTalk. You can ask me to play any lesson on our website. What would you like to do?";
var repromptText = "You can ask me things like 'play the latest sermon', 'play a random devo' or even 'play lesson number four in Hebrews'. What would you like to do?";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, false));
}
function handleSessionEndRequest(callback) {
var cardTitle = "Session Ended";
var speechOutput = "Thank you for using the BibleTalk.tv skill. Have a nice day.";
callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, true));
}
// --------------- Helpers that build all of the responses -----------------------
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
return {
outputSpeech: {
type: "PlainText",
text: output
},
card: {
type: "Simple",
title: title,
content: output
},
reprompt: {
outputSpeech: {
type: "PlainText",
text: repromptText
}
},
shouldEndSession: shouldEndSession
};
}
function buildAudioStopResponse(shouldEndSession)
{
return {
directives: [
{
"type": "AudioPlayer.Stop"
}],
shouldEndSession: shouldEndSession
};
}
function buildResponse(sessionAttributes, speechletResponse) {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment