Created
January 25, 2016 04:14
-
-
Save fat/5abf05ca3e0147a6eb22 to your computer and use it in GitHub Desktop.
a lambda file to teach alexa is it lit
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
exports.handler = function (event, context) { | |
try { | |
console.log("event.session.application.applicationId=" + event.session.application.applicationId); | |
if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.b67c3ab5-40ec-4345-856e-21c281cfded7") { | |
context.fail("Invalid Application ID"); | |
} | |
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") { | |
context.succeed(); | |
} | |
} catch (e) { | |
context.fail("Exception"+ e.stack); | |
} | |
}; | |
/** | |
* 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 ("IsItLitOrNahIntent" === intentName) { | |
isItLitOrNah(intent, false, callback); | |
} else if ("IsItLitOrNahPluralIntent" === intentName) { | |
isItLitOrNah(intent, true, callback); | |
} else if ("HelpIntent" === intentName) { | |
help(callback); | |
} else { | |
throw "Invalid intent"; | |
} | |
} | |
// --------------- Functions that control the skill's behavior ----------------------- | |
function isItLitOrNah(intent, isPlural, callback) { | |
// If we wanted to initialize the session to have some attributes we could add those here. | |
var sessionAttributes = {}; | |
var cardTitle = "Is It lit?"; | |
var random = Math.random() | |
var article = isPlural ? "are" : "is" | |
if (intent.slots.Thing.value === "it's" || intent.slots.Thing.value === "it") { | |
var speechOutput = "Ayy, it is so fucking lit."; | |
} else if (random < 0.5) { | |
var speechOutput = "Ayy, " + intent.slots.Thing.value + " " + article + " fucking lit."; | |
} else { | |
var speechOutput = "Nah man. " + intent.slots.Thing.value + " " + article + " not lit."; | |
} | |
var shouldEndSession = true; | |
callback(sessionAttributes, | |
buildSpeechletResponse(cardTitle, speechOutput, shouldEndSession)); | |
} | |
function help(callback) { | |
// If we wanted to initialize the session to have some attributes we could add those here. | |
var sessionAttributes = {}; | |
var cardTitle = "Help"; | |
var speechOutput = "I'll let you know if it's lit today. Ask is it lit."; | |
var shouldEndSession = true; | |
callback(sessionAttributes, | |
buildSpeechletResponse(cardTitle, speechOutput, shouldEndSession)); | |
} | |
// --------------- Helpers that build all of the responses ----------------------- | |
function buildSpeechletResponse(title, output, shouldEndSession) { | |
return { | |
outputSpeech: { | |
type: "PlainText", | |
text: output | |
}, | |
card: { | |
type: "Simple", | |
title: "SessionSpeechlet - " + title, | |
content: "SessionSpeechlet - " + output | |
}, | |
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