Skip to content

Instantly share code, notes, and snippets.

@muttoni
Created September 12, 2019 12:22
Show Gist options
  • Save muttoni/ba6c84313f08f65529aafd6f1ab0be46 to your computer and use it in GitHub Desktop.
Save muttoni/ba6c84313f08f65529aafd6f1ab0be46 to your computer and use it in GitHub Desktop.
ciao-mondo
// 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 in Ciao Mondo. Come posso aiutarti?';
console.log('ciao dai log!');
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 = 'Hello World!';
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
const AboutMeIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AboutMeIntent';
},
handle(handlerInput) {
const nome = handlerInput.requestEnvelope.request.intent.slots.nome.value;
const date = handlerInput.requestEnvelope.request.intent.slots.date.value;
const fruit = handlerInput.requestEnvelope.request.intent.slots.fruit.value;
const speakOutput = `Ok la tua password personalizzata è ${nome}, ${date}, ${fruit}.`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.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,
AboutMeIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
)
.addErrorHandlers(
ErrorHandler,
)
.lambda();
{
"interactionModel": {
"languageModel": {
"invocationName": "hello world",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "HelloWorldIntent",
"slots": [],
"samples": [
"ciao",
"ciao mondo",
"dimmi ciao",
"come va",
"buongiorno"
]
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "AboutMeIntent",
"slots": [
{
"name": "nome",
"type": "AMAZON.FirstName",
"samples": [
"mi chiamo {nome}",
"{nome}"
]
},
{
"name": "date",
"type": "AMAZON.DATE",
"samples": [
"{date}"
]
},
{
"name": "fruit",
"type": "FRUTTA",
"samples": [
"mi piace la {fruit}",
"{fruit}"
]
}
],
"samples": [
"il mio frutto preferito è {fruit}",
"sono nato {date}",
"io mio chiamo {nome}"
]
}
],
"types": [
{
"name": "FRUTTA",
"values": [
{
"name": {
"value": "pera"
}
},
{
"name": {
"value": "ciliegia"
}
},
{
"name": {
"value": "fragola"
}
},
{
"name": {
"value": "mela"
}
},
{
"name": {
"value": "banana"
}
}
]
}
]
},
"dialog": {
"intents": [
{
"name": "AboutMeIntent",
"confirmationRequired": false,
"prompts": {},
"slots": [
{
"name": "nome",
"type": "AMAZON.FirstName",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.326358645356.1281889193325"
}
},
{
"name": "date",
"type": "AMAZON.DATE",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.326358645356.147681397816"
}
},
{
"name": "fruit",
"type": "FRUTTA",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.326358645356.1060161956006"
}
}
]
}
],
"delegationStrategy": "ALWAYS"
},
"prompts": [
{
"id": "Elicit.Slot.326358645356.1281889193325",
"variations": [
{
"type": "PlainText",
"value": "Qual'è il tuo nome?"
},
{
"type": "PlainText",
"value": "Come ti chiami?"
}
]
},
{
"id": "Elicit.Slot.326358645356.147681397816",
"variations": [
{
"type": "PlainText",
"value": "Qual'è la tua data di nascita?"
}
]
},
{
"id": "Elicit.Slot.326358645356.1060161956006",
"variations": [
{
"type": "PlainText",
"value": "Qual'è il tuo frutto preferito?"
},
{
"type": "PlainText",
"value": "Che frutto preferisci?"
}
]
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment