Skip to content

Instantly share code, notes, and snippets.

@muttoni
Created November 1, 2019 12:05
Show Gist options
  • Save muttoni/e3ce6c81c18867babccc4d5c13bc1a5f to your computer and use it in GitHub Desktop.
Save muttoni/e3ce6c81c18867babccc4d5c13bc1a5f to your computer and use it in GitHub Desktop.
Lucca Trivia
// 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 = 'Benvenuto a Lucca Trivia. Se vuoi giocare chiedimi pure di iniziare una nuova partita oppure chiedimi aiuto. Cosa vuoi fare?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const PlayIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'PlayIntent';
},
handle(handlerInput) {
const speakOutput = `Ecco una domanda: qual'è il workshop migliore di Lucca: Alexa o Tiro con la balestra?`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
const AnswerIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AnswerIntent';
},
handle(handlerInput) {
//const answer = getSlotValue(handlerInput.requestEnvelope, 'answer');
const answer = handlerInput.requestEnvelope.request.intent.slots.answer.value;
const speakOutput = `La tua risposta è ${answer.toUpperCase()}`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.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,
AnswerIntentHandler,
PlayIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
)
.addErrorHandlers(
ErrorHandler,
)
.lambda();
{
"interactionModel": {
"languageModel": {
"invocationName": "lucca trivia",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": [
"annulla"
]
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "PlayIntent",
"slots": [],
"samples": [
"giochiamo insieme",
"facciamo un gioco",
"facciamo una partita",
"nuova partita",
"inizia una nuova partita"
]
},
{
"name": "AnswerIntent",
"slots": [
{
"name": "answer",
"type": "QUIZ_ANSWERS",
"samples": [
"{answer}"
]
}
],
"samples": [
"dico {answer}",
"sarà mica {answer}",
"quella corretta penso che sia {answer}",
"quella giusta è {answer}",
"{answer}",
"la mia risposta è {answer}"
]
}
],
"types": [
{
"name": "QUIZ_ANSWERS",
"values": [
{
"name": {
"value": "B",
"synonyms": [
"l'ultima",
"due",
"la seconda"
]
}
},
{
"name": {
"value": "A",
"synonyms": [
"uno",
"la prima"
]
}
}
]
}
]
},
"dialog": {
"intents": [
{
"name": "AnswerIntent",
"confirmationRequired": false,
"prompts": {},
"slots": [
{
"name": "answer",
"type": "QUIZ_ANSWERS",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.61596825832.684122851834"
},
"validations": [
{
"type": "hasEntityResolutionMatch",
"prompt": "Slot.Validation.61596825832.684122851834.1156675162182"
}
]
}
]
}
],
"delegationStrategy": "ALWAYS"
},
"prompts": [
{
"id": "Slot.Validation.61596825832.684122851834.1156675162182",
"variations": [
{
"type": "PlainText",
"value": "Non ho capito bene, per favore rispondi A o B"
}
]
},
{
"id": "Elicit.Slot.61596825832.684122851834",
"variations": [
{
"type": "PlainText",
"value": "Non ho sentito una risposta, dimmi A o B"
}
]
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment