Skip to content

Instantly share code, notes, and snippets.

@patmandenver
Created December 24, 2015 04:30
Show Gist options
  • Select an option

  • Save patmandenver/633c309b01363d88826e to your computer and use it in GitHub Desktop.

Select an option

Save patmandenver/633c309b01363d88826e to your computer and use it in GitHub Desktop.
/**
* This code is just a self help guide :)
*
* Author: Patrick Bailey
* Company: WhiteboardCoder
*/
/*
Intent Schema
{
"intents": [
{
"intent": "MyHelpIntent"
}
]
}
Sample Uterances...
MyHelpIntent help
MyHelpIntent for help
MyHelpIntent help me
*/
// 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);
/**
* 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) {
console.log("onLaunch requestId=" + launchRequest.requestId +
", sessionId=" + session.sessionId);
// Dispatch to your skill's launch.
getWelcomeResponse(callback);
}
/**
* Called when the user specifies an intent for this skill.
*/
function onIntent(intentRequest, session, callback) {
console.log("onIntent requestId=" + intentRequest.requestId +
", sessionId=" + session.sessionId);
var intent = intentRequest.intent,
intentName = intentRequest.intent.name;
if ("AMAZON.HelpIntent" === intentName || "MyHelpIntent" === intentName) {
getHelpResponse(callback);
}else {
getErrorResponse(callback);
}
}
/**
* 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) {
var sessionAttributes = {};
var cardTitle = "Welcome to ask Patrick";
var speechOutput = "Welcome to ask Patrick. " +
"To use this say, Alexa ask Patrick for Help.";
var cardOutput = "Welcome to ask Patrick. \n" +
"To use this say, Alexa ask Patrick for Help.";;
var shouldEndSession = true;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, cardOutput, speechOutput, shouldEndSession));
}
function getHelpResponse(callback) {
var sessionAttributes = {};
var cardTitle = "Ask Patrick";
var speechOutput = "To Play AM 7 60 say, Alexa, play 7 60. " +
"To play an audible book, say Alexa, play the book killing reagan. " +
"To set an alarm, say Alexa, set an alarm for 5 min, or, Alexa, set an alarm for 3 O'clock"
var cardOutput = "Ask Patrick,\n" +
"To Play AM 760 say, Alexa, play 760.\n" +
"To play an audible book, say Alexa, play the book killing reagan.\n" +
"To set an alarm, say Alexa, set an for 5 min, or set an alarm for 3 O'clock\n"
var shouldEndSession = true;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, cardOutput, speechOutput, shouldEndSession));
}
function getErrorResponse(callback) {
var sessionAttributes = {};
var cardTitle = "Sorry, I couldn't understand";
var speechOutput = "Sorry, I couldn't understand your request. ";
var cardOutput = speechOutput;
var shouldEndSession = true;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, cardOutput, speechOutput, shouldEndSession));
}
//
//// --------------- Helpers that build all of the responses -----------------------
//
function buildSpeechletResponse(title, cardOutput, output, shouldEndSession) {
return {
outputSpeech: {
type: "PlainText",
text: output
},
card: {
type: "Simple",
title: title,
content: cardOutput
},
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