仅在之前触发RandomLetterIntentHandler时,我才想触发我的WaitAnswerIntentHandler。目前,我的第二个意图可以由{animal} {country} {color} {food}言语触发(并非所有要求,并且可以按任何顺序),但是它需要第一个意图才能形成我想要的逻辑。
const RandomLetterIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'RandomLetterIntent';
},
handle(handlerInput) {
// **get a letter from the user**
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const randomLetter = randomLetterGenerator.getOneRandomLetter();
const speechText = requestAttributes.t('RANDOM_LETTER_ASK', randomLetter);
timerUtils.startTimer();
puntuacion = 0;
letter= randomLetter;
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
const WaitAnswerIntentHandler = {
canHandle(handlerInput){
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'WaitAnswerIntent'
&& gameState > 0;
},
handle(handlerInput){
### **// get {animal}{country}{color}{food} (not all required and can be in any order) but need the letter that is obtained in the previous intent**
const intent = handlerInput.requestEnvelope.request.intent;
const animal = intent.slots.ANIMAL.value;
const country = intent.slots.COUNTRY.value;
const color = intent.slots.COLOR.value;
const food = intent.slots.FOOD.value;
let cadenaFinal = '';
let tiempoFinal = 0;
if(animal && animal[0]===letter){
puntuacion = puntuacion + 10;
cadenaFinal = cadenaFinal + ' ' + animal;
}
if(country && country[0]===letter){
puntuacion = puntuacion + 10;
cadenaFinal = cadenaFinal + ' ' + country;
}
if(color && color[0]===letter){
puntuacion = puntuacion + 10;
cadenaFinal = cadenaFinal + ' ' + color;
}
if(food && food[0]===letter){
puntuacion = puntuacion + 10;
cadenaFinal = cadenaFinal + ' ' + food;
}
tiempoFinal = timerUtils.endTimer();
puntuacion = puntuacion - tiempoFinal;
if(!cadenaFinal){cadenaFinal='ninguna'}
const repromtText = 'pídeme otra letra para seguir jugando';
const speakOutput = `Tu respuesta válida fue ${cadenaFinal}, has tardado ${tiempoFinal.toString()} segundos y tu puntuación es ${puntuacion.toString()}`;
gameState = 0;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(repromtText)
.getResponse();
}
};
您可以添加一个名为state
的新会话属性,并在canHandle
函数中对其进行验证。
根据您的情况,在处理了RandomLetterIntentHandler
之后,您可以将state
设置为answer
或您认为最合适的名称,然后在WaitAnswerIntentHandler
canHandle
函数中检查state
是否为[C0 ]。如果是,请处理该请求并将answer
设置为默认值或将其删除。这样,state
将仅在WaitAnswerIntentHandler
之后被调用。
可以在alexa sdk repo'RandomLetterIntentHandler
'游戏中找到更具体的示例。