Skip to content

Instantly share code, notes, and snippets.

@softwarechido
Last active April 17, 2019 19:24
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 softwarechido/285dff572b655b766819e992271c095c to your computer and use it in GitHub Desktop.
Save softwarechido/285dff572b655b766819e992271c095c to your computer and use it in GitHub Desktop.
Agregamos APL welcome.json - Hola mundo APL
// lambda
// Este ejemplo es una demostración de como manejar intents en una skill de Alexa utilziando el Alexa Skills Kit SDK (v2)
const Alexa = require('ask-sdk-core');
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const speechText = 'Hola! bienvenido a Nutrición Inteligente. ¿Cómo te puedo ayudar?';
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Nutrición Inteligente')
.reprompt('¿Cómo te puedo ayudar?')
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
version: '1.0',
document: require('./welcome.json'),
datasources: {}
})
.getResponse();
}
};
const HolaMundoIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'HolaMundoIntent';
},
handle(handlerInput) {
const speechText = 'Hola Mundo!';
return handlerInput.responseBuilder
.speak(speechText)
//.reprompt('agrega un texto de reprompt si deseas dejar la sesión abierta para que el usuario responda. No olvide cerrar con una pregunta. ¿Cómo te puedo ayudar?')
.getResponse();
}
};
const ConsejoIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'ConsejoIntent';
},
handle(handlerInput) {
const listaConsejos = [
'Alimentate de forma balanceada entre carbohidratos, grasas y proteínas',
'Divide tus calorías en tres comidas, por ejemplo si tu plan es comer 1800 calorías, distribuye 600 en cada comida',
'Procura alimentos de bajo índice glucémico, es decir, alimentos que no suban tus niveles de azucar en la sangre'
];
const indiceAleatorio = Math.floor(Math.random() * listaConsejos.length);
const consejoAleatorio = listaConsejos[indiceAleatorio];
const speechOutput = consejoAleatorio;
return handlerInput.responseBuilder
.speak(`${speechOutput} ¿qué mas necesitas?`)
.reprompt('¿qué mas necesitas?')
.getResponse();
}
};
const PesoIdealIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'PesoIdealIntent';
},
handle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
var estatura = request.intent.slots.estatura.value;
var pesoidealHombre = (estatura-100)-((estatura-150)/4);
var pesoidealMujer = (estatura-100)-((estatura-150)/2);
const speechOutput = ` Tu peso ideal si eres hombre es ${pesoidealHombre} y es ${pesoidealMujer} si eres mujer`;
return handlerInput.responseBuilder
.speak(`${speechOutput} ¿qué mas necesitas?`)
.reprompt('¿qué mas necesitas?')
.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();
}
};
// Este handler para que hagas tus pruebas y debugging.
// Simplemente repite el intent que dijo el usuario.
const IntentReflectorHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest';
},
handle(handlerInput) {
const intentName = handlerInput.requestEnvelope.request.intent.name;
const speechText = `Lanzaste el intent que se llama ${intentName}`;
return handlerInput.responseBuilder
.speak(speechText)
//.reprompt('agrega un texto de reprompt si deseas dejar la sesión abierta para que el usuario responda. No olvide cerrar con una pregunta. ¿Cómo te puedo ayudar?')
.getResponse();
}
};
// Erro genério para capturar erroes de sintaxis o de enrutamiento.
// Si recibes un error, eso significa que no hay un handler que regrese "true" para el método canHandle()
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`~~~~ Error handled: ${error.message}`);
const speechText = `Lo siento, No puedo entender lo que has dicho. Por favor inténtalo de nuevo`;
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
// Este handler funciona como el punto de entrada de tu skill, se encarga de enrutar
// todas las peticiones.
// Asegúrate de tener todos los handlers e interceptores dado de alta aquí.
// El órden importa, son procesador de arriba hacia abajo.
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
HolaMundoIntentHandler,
ConsejoIntentHandler,
PesoIdealIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler) // Asegúrate de que el IntentReflector sea el último para evitar que maneje peticiones incorrectas
.addErrorHandlers(
ErrorHandler)
.lambda();
{
"interactionModel": {
"languageModel": {
"invocationName": "nutrición inteligente",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "HolaMundoIntent",
"slots": [],
"samples": [
"hola",
"cómo estás",
"di hola mundo",
"di hola",
"comenta hola mundo",
"comenta hola"
]
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "ConsejoIntent",
"slots": [],
"samples": [
"que me de un consejo",
"consejo",
"quiero un consejo",
"dame un consejo",
"un consejo"
]
},
{
"name": "PesoIdealIntent",
"slots": [
{
"name": "estatura",
"type": "AMAZON.NUMBER",
"samples": [
"mido {estatura}",
"{estatura}"
]
}
],
"samples": [
"dame mi peso ideal",
"calcúla mi peso ideal",
"mi peso ideal",
"peso ideal",
"quiero saber mi peso ideal",
"cuanto debo de pesar",
"dime mi peso ideal",
"cual es mi peso ideal"
]
}
],
"types": []
},
"dialog": {
"intents": [
{
"name": "PesoIdealIntent",
"confirmationRequired": false,
"prompts": {},
"slots": [
{
"name": "estatura",
"type": "AMAZON.NUMBER",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.666209592402.794048786262"
}
}
]
}
],
"delegationStrategy": "ALWAYS"
},
"prompts": [
{
"id": "Elicit.Slot.666209592402.794048786262",
"variations": [
{
"type": "PlainText",
"value": "¿cuánto mides en centimetros?"
}
]
}
]
}
}
{
"type": "APL",
"version": "1.0",
"import": [
{
"name": "alexa-layouts",
"version": "1.0.0"
}
],
"mainTemplate": {
"items": [
{
"type": "Text",
"text": "Nutrición Inteligente"
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment