如果未先调用另一个意图,如何防止意图触发

问题描述 投票:0回答:1

仅在之前触发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();
    }
};
alexa alexa-skills-kit alexa-skill alexa-voice-service alexa-slot
1个回答
0
投票

您可以添加一个名为state的新会话属性,并在canHandle函数中对其进行验证。

根据您的情况,在处理了RandomLetterIntentHandler之后,您可以将state设置为answer或您认为最合适的名称,然后在WaitAnswerIntentHandler canHandle函数中检查state是否为[C0 ]。如果是,请处理该请求并将answer设置为默认值或将其删除。这样,state将仅在WaitAnswerIntentHandler之后被调用。

可以在alexa sdk repo'RandomLetterIntentHandler'游戏中找到更具体的示例。

© www.soinside.com 2019 - 2024. All rights reserved.