Created
April 24, 2019 04:31
-
-
Save CILP/ddfbc45c416b27aa4b9d48ff14d82da5 to your computer and use it in GitHub Desktop.
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
const Alexa = require('ask-sdk-core'); | |
const colors = [ | |
'Azul', | |
'Verde', | |
'Cafe', | |
'Amarillo', | |
'Rojo', | |
'Blanco' | |
]; | |
const questions = { | |
'De que color es el cielo?': 'Azul', | |
'De que color es el sol?': 'Amarillo', | |
'De que color es la manzana?': 'Rojo' | |
}; | |
// debemos mostrar preguntar en diferente orden siempre | |
function createRandomTrivia(questions) { | |
const qs = Object.keys(questions); | |
const trivia = []; | |
let lastIndex = qs.length; | |
for (let i = 0; i !== qs.length; i++) { | |
const randomIndex = Math.floor(Math.random() * lastIndex); | |
lastIndex--; | |
const tmp = qs[lastIndex]; | |
qs[lastIndex] = qs[randomIndex]; | |
qs[randomIndex] = tmp; | |
trivia.push(qs[lastIndex]); | |
} | |
return trivia; | |
} | |
function isAnswerSlotValid(intent) { | |
const answerSlotFilled = intent && | |
intent.slots && | |
intent.slots.Answer && | |
intent.slots.Answer.value; | |
const answerSlotIsColor = answerSlotFilled && | |
colors.includes(intent.slots.Answer.value); | |
return answerSlotIsColor; | |
} | |
function handleUserGuess(userGaveUp, handlerInput) { | |
const { requestEnvelope, attributesManager, responseBuilder } = handlerInput; | |
const { intent } = requestEnvelope.request; | |
const isSlotValid = isAnswerSlotValid(intent); | |
let speechOutput = ''; | |
const sessionAttributes = attributesManager.getSessionAttributes(); | |
const triviaQuestions = sessionAttributes.trivia; | |
let currentQuestion = sessionAttributes.currentQuestion; | |
let currentScore = parseInt(sessionAttributes.score, 10); | |
let speechOutputAnalysis = ''; | |
let repromptText = ''; | |
if ( | |
isSlotValid && | |
intent.slots.Answer.value === questions[currentQuestion] | |
) { | |
speechOutputAnalysis = 'Respuesta correcta'; | |
currentScore += 10; | |
// currentScore = currentQuestion.toString(); | |
// pero queremos mostrar la siguiente pregunta | |
} else { | |
speechOutputAnalysis = 'Respuesta incorrecta'; | |
speechOutputAnalysis += ` La respuesta correcta era ${questions[currentQuestion]}` | |
} | |
const nextQuestion = triviaQuestions.pop(); | |
if (!nextQuestion && currentScore < 30) { | |
// return response ENDGAME | |
return responseBuilder | |
.speak(`${speechOutputAnalysis}. Termino el juego, perdiste!. Tu puntuacion fue, ${currentScore}`) | |
.getResponse(); | |
} else if(!nextQuestion && currentScore === 30) { | |
return responseBuilder | |
.speak(`${speechOutputAnalysis}. Termino el juego, ganaste!. Tu puntuacion fue, ${currentScore}`) | |
.getResponse(); | |
} | |
repromptText = `${speechOutputAnalysis}. Siguiente pregunta, `; | |
repromptText += nextQuestion; | |
Object.assign(sessionAttributes, { | |
speechOutput: repromptText, | |
repromptText, | |
currentQuestion: nextQuestion, | |
trivia: triviaQuestions, | |
score: currentScore | |
}); | |
return responseBuilder | |
.speak(repromptText) | |
.reprompt(repromptText) | |
.getResponse(); | |
} | |
function startGame(newGame, handlerInput) { | |
const requestAttributes = handlerInput.attributesManager.getRequestAttributes(); | |
const trivia = createRandomTrivia(questions); | |
const currentQuestion = trivia.pop(); | |
const sessionAttributes = {}; | |
Object.assign(sessionAttributes, { | |
speechOutput: currentQuestion, | |
repromptText: currentQuestion, | |
currentQuestion, | |
trivia, | |
score: 0 | |
}); | |
handlerInput.attributesManager.setSessionAttributes(sessionAttributes); | |
return handlerInput.responseBuilder | |
.speak(currentQuestion) | |
.reprompt(currentQuestion) | |
.getResponse(); | |
} | |
const LaunchRequest = { | |
canHandle(handlerInput) { | |
const { request } = handlerInput.requestEnvelope; | |
return request.type === 'LaunchRequest' | |
|| (request.type === 'IntentRequest' | |
&& request.intent.name === 'AMAZON.StartOverIntent'); | |
}, | |
handle(handlerInput) { | |
return startGame(true, handlerInput); | |
}, | |
}; | |
const HelpIntent = { | |
canHandle(handlerInput) { | |
const { request } = handlerInput.requestEnvelope; | |
return request.type === 'IntentRequest' && request.intent.name === 'AMAZON.HelpIntent'; | |
}, | |
handle(handlerInput) { | |
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes(); | |
// const newGame = !(sessionAttributes.trivia); | |
// return helpTheUser(newGame, handlerInput); | |
return handlerInput.responseBuilder.speak('Para jugar di, iniciar trivia').reprompt('Para jugar di, iniciar trivia').getResponse(); | |
}, | |
}; | |
const UnhandledIntent = { | |
canHandle() { | |
return true; | |
}, | |
handle(handlerInput) { | |
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes(); | |
const requestAttributes = handlerInput.attributesManager.getRequestAttributes(); | |
if (Object.keys(sessionAttributes).length === 0) { | |
// const speechOutput = requestAttributes.t('START_UNHANDLED'); | |
return handlerInput.attributesManager | |
.speak('Di iniciar trivia para comenzar a jugar') | |
.reprompt('Di iniciar trivia para comenzar a jugar') | |
.getResponse(); | |
} else if (sessionAttributes.questions) { | |
// const speechOutput = requestAttributes.t('TRIVIA_UNHANDLED', ANSWER_COUNT.toString()); | |
return handlerInput.attributesManager | |
.speak('Intenta diciendo un color') | |
.reprompt('Intenta diciendo un color') | |
.getResponse(); | |
} | |
// const speechOutput = requestAttributes.t('HELP_UNHANDLED'); | |
return handlerInput | |
.attributesManager | |
.speak('Di si para continuar, o di no para terminar el juego') | |
.reprompt('Di si para continuar, o di no para terminar el juego') | |
.getResponse(); | |
}, | |
}; | |
const SessionEndedRequest = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest'; | |
}, | |
handle(handlerInput) { | |
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`); | |
return handlerInput.responseBuilder.getResponse(); | |
}, | |
}; | |
const AnswerIntent = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& (handlerInput.requestEnvelope.request.intent.name === 'AnswerIntent' | |
|| handlerInput.requestEnvelope.request.intent.name === 'DontKnowIntent'); | |
}, | |
handle(handlerInput) { | |
if (handlerInput.requestEnvelope.request.intent.name === 'AnswerIntent') { | |
return handleUserGuess(false, handlerInput); | |
} | |
return handleUserGuess(true, handlerInput); | |
}, | |
}; | |
const RepeatIntent = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.RepeatIntent'; | |
}, | |
handle(handlerInput) { | |
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes(); | |
return handlerInput.responseBuilder.speak(sessionAttributes.speechOutput) | |
.reprompt(sessionAttributes.repromptText) | |
.getResponse(); | |
}, | |
}; | |
const YesIntent = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.YesIntent'; | |
}, | |
handle(handlerInput) { | |
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes(); | |
if (sessionAttributes.questions) { | |
return handlerInput.responseBuilder.speak(sessionAttributes.speechOutput) | |
.reprompt(sessionAttributes.repromptText) | |
.getResponse(); | |
} | |
return startGame(false, handlerInput); | |
}, | |
}; | |
const StopIntent = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent'; | |
}, | |
handle(handlerInput) { | |
// const requestAttributes = handlerInput.attributesManager.getRequestAttributes(); | |
// const speechOutput = requestAttributes.t('STOP_MESSAGE'); | |
return handlerInput.responseBuilder.speak('Quieres seguir jugando?') | |
.reprompt('Quieres seguir jugando?') | |
.getResponse(); | |
}, | |
}; | |
const CancelIntent = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'; | |
}, | |
handle(handlerInput) { | |
// const requestAttributes = handlerInput.attributesManager.getRequestAttributes(); | |
// const speechOutput = requestAttributes.t('CANCEL_MESSAGE'); | |
return handlerInput.responseBuilder.speak('Muy bien, juguemos despues!') | |
.getResponse(); | |
}, | |
}; | |
const NoIntent = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.NoIntent'; | |
}, | |
handle(handlerInput) { | |
// const requestAttributes = handlerInput.attributesManager.getRequestAttributes(); | |
// const speechOutput = requestAttributes.t('NO_MESSAGE'); | |
return handlerInput.responseBuilder.speak('Jugaremos en otra ocacion, hasta luego!').getResponse(); | |
}, | |
}; | |
const ErrorHandler = { | |
canHandle() { | |
return true; | |
}, | |
handle(handlerInput, error) { | |
console.log(`Error handled: ${error.message}`); | |
return handlerInput.responseBuilder | |
.speak('Perdon, no te entendi, puedes repetirlo?') | |
.reprompt('Perdon, no te entendi, puedes repetirlo?') | |
.getResponse(); | |
}, | |
}; | |
const skillBuilder = Alexa.SkillBuilders.custom(); | |
exports.handler = skillBuilder | |
.addRequestHandlers( | |
LaunchRequest, | |
HelpIntent, | |
AnswerIntent, | |
RepeatIntent, | |
YesIntent, | |
StopIntent, | |
CancelIntent, | |
NoIntent, | |
SessionEndedRequest, | |
UnhandledIntent | |
) | |
.addErrorHandlers(ErrorHandler) | |
.lambda(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment