Skip to content

Instantly share code, notes, and snippets.

@muttoni
Created September 28, 2019 08:43
Show Gist options
  • Save muttoni/53a066685faf1d83323ec618124ae6e5 to your computer and use it in GitHub Desktop.
Save muttoni/53a066685faf1d83323ec618124ae6e5 to your computer and use it in GitHub Desktop.
// This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2).
// Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,
// session persistence, api calls, and more.
const Alexa = require('ask-sdk-core');
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
handle(handlerInput) {
const speakOutput = 'Welcome to Conference Buddy. How can I be of assistance?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
},
handle(handlerInput) {
const speakOutput = 'Cześć! How can I help?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('I did not get that, how can I help?')
.getResponse();
}
};
const GetNextTalkIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'GetNextTalk';
},
handle(handlerInput) {
// put API call to the conference service
const speakOutput = 'The next talk is going to be by John Smith, covering Alexa';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('I did not get that, how can I help?')
.getResponse();
}
};
const RespondSurveyIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'RespondSurveyIntent';
},
handle(handlerInput) {
// put API call to the conference service
const speakOutput = 'Thanks for your feedback, your rating was an average of: ';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('I did not get that, how can I help?')
.getResponse();
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speakOutput = 'You can say hello to me! How can I help?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speakOutput = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
},
handle(handlerInput) {
// Any cleanup logic goes here.
return handlerInput.responseBuilder.getResponse();
}
};
// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
const IntentReflectorHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
},
handle(handlerInput) {
const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
const speakOutput = `You just triggered ${intentName}`;
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`~~~~ Error handled: ${error.stack}`);
const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
HelloWorldIntentHandler,
GetNextTalkIntentHandler,
RespondSurveyIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
)
.addErrorHandlers(
ErrorHandler,
)
.lambda();
{
"interactionModel": {
"languageModel": {
"invocationName": "conference buddy",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "HelloWorldIntent",
"slots": [],
"samples": [
"hello",
"how are you",
"say hi world",
"say hi",
"hi",
"say hello world",
"say hello"
]
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "RespondSurveyIntent",
"slots": [
{
"name": "answerA",
"type": "AMAZON.NUMBER",
"samples": [
"{answerA}"
]
},
{
"name": "answerB",
"type": "AMAZON.NUMBER",
"samples": [
"{answerB}"
]
},
{
"name": "answerC",
"type": "AMAZON.NUMBER",
"samples": [
"{answerC}"
]
}
],
"samples": [
"open the survey",
"i would like to take a part in this survey",
"i want to give feedback",
"start the survey"
]
},
{
"name": "GetNextTalk",
"slots": [],
"samples": [
"what's next",
"who is the next speaker",
"what is the next talk"
]
}
],
"types": []
},
"dialog": {
"intents": [
{
"name": "RespondSurveyIntent",
"confirmationRequired": false,
"prompts": {},
"slots": [
{
"name": "answerA",
"type": "AMAZON.NUMBER",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.817823842422.409695866204"
}
},
{
"name": "answerB",
"type": "AMAZON.NUMBER",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.817823842422.645072295927"
}
},
{
"name": "answerC",
"type": "AMAZON.NUMBER",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.817823842422.464203815335"
}
}
]
}
],
"delegationStrategy": "ALWAYS"
},
"prompts": [
{
"id": "Elicit.Slot.817823842422.409695866204",
"variations": [
{
"type": "PlainText",
"value": "From one to ten, how would you rate the quality of the talks?"
}
]
},
{
"id": "Elicit.Slot.817823842422.645072295927",
"variations": [
{
"type": "PlainText",
"value": "From one to ten, how would you rate the quality of the food?"
}
]
},
{
"id": "Elicit.Slot.817823842422.464203815335",
"variations": [
{
"type": "PlainText",
"value": "From one to ten, how would you rate the quality of the event overall?"
}
]
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment