Last active
April 22, 2019 22:35
-
-
Save softwarechido/a1d84b9ccd1d903510211c83ee599df0 to your computer and use it in GitHub Desktop.
Ejemplo de Dynamic Entities - Juego de Mesa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//lambda | |
// Este ejemplo muestra como utilizar Dynamis Slot para cambia en tiempo de ejecución (de forma dinámica) los slot validos para el tipo de Slot: JuegoMesa | |
const Alexa = require('ask-sdk-core'); | |
const LaunchRequestHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'LaunchRequest'; | |
}, | |
handle(handlerInput) { | |
const juegos = [ | |
{ | |
"id": "1", | |
"name": { | |
"value": "turista", | |
"synonyms": [ | |
"turista mundial" | |
] | |
} | |
}, | |
{ | |
"id": "2", | |
"name": { | |
"value": "lotería", | |
"synonyms": [ | |
"bingo mexicano", | |
"lotería mexicana" | |
] | |
} | |
} | |
]; | |
console.log(JSON.stringify(juegos)); | |
juegos.forEach(game => { | |
console.log('game: ', JSON.stringify(game)); | |
console.log(game.name.value); | |
}); | |
const boardGameEntities = { | |
type: "Dialog.UpdateDynamicEntities", | |
updateBehavior: "REPLACE", | |
types: [ | |
{ | |
name: "TiposDeJuego", | |
values: juegos | |
} | |
] | |
}; | |
const speechText = 'Bienvenido a Juego de Mesa, puedes decir quiero jugar ajedrez y te diré las reglas. ¿qué deseas jugar?'; | |
return handlerInput.responseBuilder | |
.addDirective(boardGameEntities) | |
.speak(speechText) | |
.reprompt(speechText) | |
.getResponse(); | |
} | |
}; | |
const JuegoMesaIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'JuegoMesaIntent'; | |
}, | |
handle(handlerInput) { | |
const slotTiposDeJuegoValue = getStaticAndDynamicSlotValuesFromSlot(handlerInput.requestEnvelope.request.intent.slots.juegoMesa) | |
console.log('slotTiposDeJuegoValue:'+JSON.stringify(slotTiposDeJuegoValue)); | |
let prompt = "Que triste, pero no conozco ese juego"; | |
if (slotTiposDeJuegoValue.dynamic.statusCode === 'ER_SUCCESS_MATCH' || slotTiposDeJuegoValue.static.statusCode === 'ER_SUCCESS_MATCH') { | |
prompt = "Las reglas son... Oh! no me se las reglas"; | |
} | |
const speechText = `Entonces jugaremos ${slotTiposDeJuegoValue.value}. ${prompt}`; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
//.reprompt('add a reprompt if you want to keep the session open for the user to respond') | |
.getResponse(); | |
} | |
}; | |
const HelpIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent'; | |
}, | |
handle(handlerInput) { | |
const speechText = 'Me puedes decir hola, ¿Cómo te puedo ayudar?'; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.reprompt(speechText) | |
.getResponse(); | |
} | |
}; | |
const CancelAndStopIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent' | |
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent'); | |
}, | |
handle(handlerInput) { | |
const speechText = 'Adios!'; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.getResponse(); | |
} | |
}; | |
const SessionEndedRequestHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === '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 handlerInput.requestEnvelope.request.type === 'IntentRequest'; | |
}, | |
handle(handlerInput) { | |
const intentName = handlerInput.requestEnvelope.request.intent.name; | |
const speechText = `Has lanzado ${intentName}`; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
//.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.message}`); | |
const speechText = `Lo siento ha ocurrido un error. Por favor, intenta de nuevo.`; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.reprompt(speechText) | |
.getResponse(); | |
} | |
}; | |
const getStaticAndDynamicSlotValues = function(slots) { | |
const slotValues = {} | |
for (let slot in slots) { | |
slotValues[slot] = getStaticAndDynamicSlotValuesFromSlot(slots[slot]); | |
} | |
return slotValues; | |
} | |
const getStaticAndDynamicSlotValuesFromSlot = function(slot) { | |
const result = { | |
name: slot.name, | |
value: slot.value | |
}; | |
if (((slot.resolutions || {}).resolutionsPerAuthority || [])[0] || {}) { | |
slot.resolutions.resolutionsPerAuthority.forEach((authority) => { | |
const slotValue = { | |
authority: authority.authority, | |
statusCode: authority.status.code, | |
synonym: slot.value || undefined, | |
resolvedValues: slot.value | |
}; | |
if (authority.values && authority.values.length > 0) { | |
slotValue.resolvedValues = []; | |
authority.values.forEach((value) => { | |
slotValue.resolvedValues.push(value); | |
}); | |
} | |
if (authority.authority.includes('amzn1.er-authority.echo-sdk.dynamic')) { | |
result.dynamic = slotValue; | |
} else { | |
result.static = slotValue; | |
} | |
}); | |
} | |
return result; | |
}; | |
// This handler 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, | |
JuegoMesaIntentHandler, | |
HelpIntentHandler, | |
CancelAndStopIntentHandler, | |
SessionEndedRequestHandler, | |
IntentReflectorHandler) // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers | |
.addErrorHandlers( | |
ErrorHandler) | |
.lambda(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"interactionModel": { | |
"languageModel": { | |
"invocationName": "juego de mesa", | |
"intents": [ | |
{ | |
"name": "AMAZON.CancelIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "AMAZON.HelpIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "AMAZON.StopIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "JuegoMesaIntent", | |
"slots": [ | |
{ | |
"name": "juegoMesa", | |
"type": "TiposDeJuego" | |
} | |
], | |
"samples": [ | |
"{juegoMesa}", | |
"juguemos {juegoMesa}", | |
"jugar {juegoMesa}", | |
"deseo jugar {juegoMesa}", | |
"quiero jugar {juegoMesa}" | |
] | |
}, | |
{ | |
"name": "AMAZON.NavigateHomeIntent", | |
"samples": [] | |
} | |
], | |
"types": [ | |
{ | |
"name": "TiposDeJuego", | |
"values": [ | |
{ | |
"name": { | |
"value": "cartas", | |
"synonyms": [ | |
"naipes" | |
] | |
} | |
}, | |
{ | |
"name": { | |
"value": "dominó", | |
"synonyms": [ | |
"dominio" | |
] | |
} | |
}, | |
{ | |
"name": { | |
"value": "ajedrez", | |
"synonyms": [ | |
"el tradicional del rey" | |
] | |
} | |
} | |
] | |
} | |
] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
En los Samples, la función de "quiero jugar {juegoMesa}" y demás se le puede agregar "vamos a jugar, empecemos, a darle, comencemos", puede ser un acertamiento para más posibles respuestas.