Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Last active October 10, 2018 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save germanviscuso/8792b98e78610426683ab4ab1b8983ab to your computer and use it in GitHub Desktop.
Save germanviscuso/8792b98e78610426683ab4ab1b8983ab to your computer and use it in GitHub Desktop.
Alexa Skills Basics: Slot Confirmation in Dialogue Management
{
"interactionModel": {
"languageModel": {
"invocationName": "pintor mágico",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "MyCustomIntent",
"slots": [
{
"name": "habitacion",
"type": "AMAZON.Room",
"samples": [
"{habitacion}",
"para {habitacion}",
"para la {habitacion}",
"para el {habitacion}",
"quiero pintar {habitacion}",
"quiero pintar un {habitacion}",
"quiero pintar una {habitacion}",
"una {habitacion}",
"un {habitacion}",
"habitación {habitacion}"
]
},
{
"name": "color",
"type": "AMAZON.Color",
"samples": [
"{color}",
"de {color}",
"color {color}",
"pintar {color}",
"pintar de {color}",
"pintar color {color}",
"píntalo {color}",
"píntalo de {color}",
"píntalo color {color}"
]
}
],
"samples": [
"quiero pintar",
"quiero pintar una {habitacion} de color {color}",
"quiero pintar un {habitacion} de color {color}",
"quiero una {habitacion} de color {color}",
"quiero un {habitacion} de color {color}",
"quiero pintar {habitacion} de color {color}",
"quiero pintar una {habitacion} de {color}",
"quiero pintar un {habitacion} de {color}",
"quiero una {habitacion} de {color}",
"quiero un {habitacion} de {color}",
"quiero pintar {habitacion} de {color}",
"quiero pintar de color {color} una {habitacion}",
"quiero pintar de color {color} un {habitacion}",
"quiero de color {color} una {habitacion}",
"quiero de color {color} un {habitacion}",
"quiero de color {color} pintar {habitacion}",
"quiero de {color} pintar una {habitacion}",
"quiero de {color} pintar un {habitacion} de {color}",
"quiero de {color} una {habitacion}",
"quiero de {color} un {habitacion}",
"quiero de {color} pintar {habitacion}",
"pintar una {habitacion} de color {color}",
"pintar un {habitacion} de color {color}",
"una {habitacion} de color {color}",
"un {habitacion} de color {color}",
"pintar {habitacion} de color {color}",
"pintar una {habitacion} de {color}",
"pintar un {habitacion} de {color}",
"una {habitacion} de {color}",
"un {habitacion} de {color}",
"pintar {habitacion} de {color}",
"pintar de color {color} una {habitacion}",
"pintar de color {color} un {habitacion}",
"de color {color} quiero una {habitacion}",
"de color {color} quiero un {habitacion}",
"de color {color} quiero pintar {habitacion}",
"de {color} quiero pintar una {habitacion}",
"de {color} quiero pintar un {habitacion} de {color}",
"de {color} quiero una {habitacion}",
"de {color} quiero un {habitacion}",
"de {color} quiero pintar {habitacion}",
"píntame la {habitacion} de color {color}",
"píntame el {habitacion} de color {color}",
"píntame la {habitacion} de {color}",
"píntame el {habitacion} de {color}",
"píntame de color {color} la {habitacion}",
"píntame de color {color} el {habitacion}",
"píntame de {color} la {habitacion}",
"píntame de {color} el {habitacion}",
"pinta",
"pinta la {habitacion} de color {color}",
"pinta el {habitacion} de color {color}",
"pinta la {habitacion} de {color}",
"pinta el {habitacion} de {color}",
"pinta de color {color} la {habitacion}",
"pinta de color {color} el {habitacion}",
"pinta de {color} la {habitacion}",
"pinta de {color} el {habitacion}"
]
}
],
"types": []
},
"dialog": {
"intents": [
{
"name": "MyCustomIntent",
"confirmationRequired": false,
"prompts": {},
"slots": [
{
"name": "habitacion",
"type": "AMAZON.Room",
"confirmationRequired": true,
"elicitationRequired": true,
"prompts": {
"confirmation": "Confirm.Slot.147269857970.1311113080241",
"elicitation": "Elicit.Slot.147269857970.1311113080241"
}
},
{
"name": "color",
"type": "AMAZON.Color",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.542396406661.65620125874"
}
}
]
}
]
},
"prompts": [
{
"id": "Elicit.Slot.147269857970.1311113080241",
"variations": [
{
"type": "PlainText",
"value": "¿Vale, de color {color} , pero cual habitación?"
},
{
"type": "PlainText",
"value": "¿Vale, pero cual habitación?"
}
]
},
{
"id": "Confirm.Slot.147269857970.1311113080241",
"variations": [
{
"type": "PlainText",
"value": "¿Estás seguro de elegir {habitacion} ?"
}
]
},
{
"id": "Elicit.Slot.542396406661.65620125874",
"variations": [
{
"type": "PlainText",
"value": "¿Ya has dicho {habitacion} , pero de que color?"
},
{
"type": "PlainText",
"value": "¿Vale, pero de que color?"
}
]
}
]
}
}
const Alexa = require('ask-sdk');
const HELP_MESSAGE = 'Por favor dime: quiero pintar';
const HELP_REPROMPT = 'Puedes decir: quiero pintar y especificar que habitacón y de que color';
const STOP_MESSAGE = 'Adios!';
const InProgressMyCustomIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'MyCustomIntent'
&& request.dialogState !== 'COMPLETED';
},
handle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
const responseBuilder = handlerInput.responseBuilder;
const intent = request.intent;
return responseBuilder
.addDelegateDirective(intent)
.getResponse();
},
};
const CompletedMyCustomIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'MyCustomIntent';
},
handle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
const intent = request.intent;
const slotValues = getSlotValues(intent.slots);
const habitacion = slotValues["habitacion"].value;
const color = slotValues["color"].value;
let speechOutput = 'Vale! Ya te he pintado ' + habitacion + ' de color ' + color + '. Que lo disfrutes! Adiós!';
return handlerInput.responseBuilder
.speak(speechOutput)
.getResponse();
},
};
const HelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest' ||
(request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.HelpIntent');
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(HELP_MESSAGE)
.reprompt(HELP_REPROMPT)
.getResponse();
},
};
const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& (request.intent.name === 'AMAZON.CancelIntent'
|| request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(STOP_MESSAGE)
.getResponse();
},
};
function getSlotValues(filledSlots) {
const slotValues = {};
console.log(`The filled slots: ${JSON.stringify(filledSlots)}`);
Object.keys(filledSlots).forEach((item) => {
const name = filledSlots[item].name;
if (filledSlots[item] &&
filledSlots[item].resolutions &&
filledSlots[item].resolutions.resolutionsPerAuthority[0] &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
switch (filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
case 'ER_SUCCESS_MATCH':
slotValues[name] = {
synonym: filledSlots[item].value,
value: filledSlots[item].resolutions.resolutionsPerAuthority[0].values[0].value.name,
id: filledSlots[item].resolutions.resolutionsPerAuthority[0].values[0].value.id,
isValidated: true,
canUnderstand: true,
canFulfill: true,
};
break;
case 'ER_SUCCESS_NO_MATCH':
slotValues[name] = {
synonym: filledSlots[item].value,
value: filledSlots[item].value,
id: null,
isValidated: false,
canUnderstand: false,
canFulfill: null,
};
break;
default:
break;
}
} else {
slotValues[name] = {
synonym: filledSlots[item].value,
value: filledSlots[item].value,
id: filledSlots[item].id,
isValidated: false,
canUnderstand: false,
canFulfill: false,
};
}
}, this);
return slotValues;
}
const skillBuilder = Alexa.SkillBuilders.standard();
exports.handler = skillBuilder
.addRequestHandlers(
InProgressMyCustomIntentHandler,
CompletedMyCustomIntentHandler,
HelpHandler,
ExitHandler
)
.lambda();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment