Created
October 20, 2015 17:35
-
-
Save jraines/476dae7017632df4a4b1 to your computer and use it in GitHub Desktop.
Minimal AWS Lambda Alexa js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This sample demonstrates a simple skill built with the Amazon Alexa Skills Kit. | |
* For additional samples, visit the Alexa Skills Kit developer documentation at | |
* https://developer.amazon.com/appsandservices/solutions/alexa/alexa-skills-kit/getting-started-guide | |
*/ | |
// Route the incoming request based on type (LaunchRequest, IntentRequest, | |
// etc.) The JSON body of the request is provided in the event parameter. | |
exports.handler = function (event, context) { | |
try { | |
console.log("event.session.application.applicationId=" + event.session.application.applicationId); | |
console.log(event); | |
console.log(context); | |
/** | |
* Uncomment this if statement and populate with your skill's application ID to | |
* prevent someone else from configuring a skill that sends requests to this function. | |
*/ | |
/* | |
if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.[unique-value-here]") { | |
context.fail("Invalid Application ID"); | |
} | |
*/ | |
if (event.session.new) { | |
onSessionStarted({requestId: event.request.requestId}, event.session); | |
} | |
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)); | |
}); | |
} else if (event.request.type === "SessionEndedRequest") { | |
onSessionEnded(event.request, event.session); | |
context.succeed(); | |
} | |
} catch (e) { | |
context.fail("Exception: " + e); | |
} | |
}; | |
/** | |
* Called when the session starts. | |
*/ | |
function onSessionStarted(sessionStartedRequest, session) { | |
console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId | |
+ ", sessionId=" + session.sessionId); | |
} | |
/** | |
* Called when the user launches the skill without specifying what they want. | |
*/ | |
function onLaunch(launchRequest, session, callback) { | |
// Dispatch to your skill's launch. | |
getWelcomeResponse(callback); | |
} | |
/** | |
* Called when the user specifies an intent for this skill. | |
*/ | |
function onIntent(intentRequest, session, callback) { | |
var intent = intentRequest.intent, | |
intentName = intentRequest.intent.name; | |
// Dispatch to your skill's intent handlers | |
if ("TestIntent" === intentName) { | |
testResponse(intent, session, callback); | |
} else { | |
throw "Invalid intent"; | |
} | |
} | |
/** | |
* 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); | |
// Add cleanup logic here | |
} | |
// --------------- Functions that control the skill's behavior ----------------------- | |
function getWelcomeResponse(callback) { | |
// If we wanted to initialize the session to have some attributes we could add those here. | |
var sessionAttributes = {}; | |
var cardTitle = "Welcome"; | |
var speechOutput = "Welcome to Stylitics test" | |
// If the user either does not reply to the welcome message or says something that is not | |
// understood, they will be prompted again with this text. | |
var repromptText = "Ask me for a sign"; | |
var shouldEndSession = false; | |
callback(sessionAttributes, | |
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); | |
} | |
/** | |
* Sets the color in the session and prepares the speech to reply to the user. | |
*/ | |
function testResponse(intent, session, callback) { | |
var cardTitle = intent.name; | |
var repromptText = null; | |
var sessionAttributes = {}; | |
var shouldEndSession = true; | |
var speechOutput = "Hello from Stylitics"; | |
// Setting repromptText to null signifies that we do not want to reprompt the user. | |
// If the user does not respond or says something that is not understood, the session | |
// will end. | |
callback(sessionAttributes, | |
buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession)); | |
} | |
// --------------- Helpers that build all of the responses ----------------------- | |
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) { | |
return { | |
outputSpeech: { | |
type: "PlainText", | |
text: output | |
}, | |
card: { | |
type: "Simple", | |
title: "SessionSpeechlet - " + title, | |
content: "SessionSpeechlet - " + output | |
}, | |
reprompt: { | |
outputSpeech: { | |
type: "PlainText", | |
text: repromptText | |
} | |
}, | |
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