Skip to content

Instantly share code, notes, and snippets.

@muttoni
Created November 14, 2019 12:56
Show Gist options
  • Save muttoni/2aa0900f6053aabe81d40055931a98cc to your computer and use it in GitHub Desktop.
Save muttoni/2aa0900f6053aabe81d40055931a98cc to your computer and use it in GitHub Desktop.
Euroconsumer
// 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 Euro Consumers, you can ask for a product recommendation or a product evaluation. Which would you like to try?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const EvaluateProductHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'EvaluateProduct';
},
handle(handlerInput) {
const device = handlerInput.requestEnvelope.request.intent.slots.device.value;
const speakOutput = 'You want an evaluation of the ' + device;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('I did not hear that, can you please repeat?')
.getResponse();
}
};
const GetRecommendedProductHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'GetRecommendedProduct';
},
handle(handlerInput) {
const deviceb = handlerInput.requestEnvelope.request.intent.slots.deviceb.value;
const speakOutput = 'You want a ' + deviceb + ' recommendation.';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('I did not hear that, can you please repeat?')
.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,
EvaluateProductHandler,
GetRecommendedProductHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
)
.addErrorHandlers(
ErrorHandler,
)
.lambda();
{
"interactionModel": {
"languageModel": {
"invocationName": "euro consumers",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "EvaluateProduct",
"slots": [
{
"name": "device",
"type": "DEVICES"
}
],
"samples": [
"should I buy {device}",
"should I buy the {device}"
]
},
{
"name": "GetRecommendedProduct",
"slots": [
{
"name": "deviceb",
"type": "DEVICE_TYPES"
}
],
"samples": [
"what {deviceb} should I buy"
]
}
],
"types": [
{
"name": "DEVICES",
"values": [
{
"name": {
"value": "Echo Spot"
}
},
{
"name": {
"value": "Echo Show"
}
},
{
"name": {
"value": "Echo"
}
}
]
},
{
"name": "DEVICE_TYPES",
"values": [
{
"name": {
"value": "microwave oven"
}
},
{
"name": {
"value": "heater"
}
},
{
"name": {
"value": "refrigerator"
}
},
{
"name": {
"value": "washing machine"
}
},
{
"name": {
"value": "dishwasher"
}
}
]
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment