Hi im making a simple Alexa skill that asks for a monster name and tells you the speed
I ask the user for a monster name, the user's monster name then is stored in: handlerInput.requestEnvelope.request.intent.slots.name.value
and I save it to variable monster (code below) so it saves the name given by the user.
I also have a file called typecharts, there i got numbers relative to monsters speed.
typecharts file:
module.exports = {
Werewolf: '60',
Alien: "98",
Herb: "10",
};
so variable monster (code below) prints the name given by the user correctly, the problem is this:
I know how to call the number values in that file (typecharts) like Werewolf or Alien for alexa to speak by doing this: .speak(typecharts.Alien) and it says 98,
but i need to place the variable that stores the name of the monters the user given so it looks for the monster we want, something like this .speak(typecharts.variable monster here) so she looks for typecharts.whathevertheusersaid, alien merewolf or herb.
I tried by creating this variable const spe = (typecharts.monster) but it fails because it doesn't look for the name the user said (saved at "monster" variable), it looks for "monster" instead, not the name inside variable, ¿how can i make it look for the text stored at monster?.
CODE TO FIX:
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'CompareIntent';
},
handle(handlerInput) {
const monster = handlerInput.requestEnvelope.request.intent.slots.name.value
const spe = (typecharts.monster) // <- i need to fix this to do what i need.
return handlerInput.responseBuilder
.speak("you said "+ monster +" with speed " + spe ) // <- alexa speech output
.reprompt("tell me more")
.getResponse();
}
Use bracket syntax to access object keys with a variable (otherwise, it will try to find a key named "monster", which you don't have).
handle(handlerInput) {
const monster = handlerInput.requestEnvelope.request.intent.slots.name.value
const spe = typecharts[monster]
return handlerInput.responseBuilder
.speak("you said "+ monster +" with speed " + spe ) // <- alexa speech output
.reprompt("tell me more")
.getResponse();
}
Keep in mind to handle the case where the monster is not found, i.e.
if (!spe) { ... } else { ... }