alexa技能可维护广告位的顺序并添加验证

时间:2018-10-15 13:37:13

标签: node.js aws-lambda alexa alexa-skills-kit

我正在研究一种简单的技能,可从银行名称和支行检索银行的ifsc代码。我实现了多匝对话,这意味着在填充插槽之前需要进行对话。交互模型中有两个插槽。他们是银行和分支。两个插槽都需要。我已经为BANK插槽提供了200种语音提示。

该技能首先提示用户输入银行名称。如果用户提供了无效的银行名称,请说“我不知道”。它识别出无效的银行名称,并再次提示用户输入银行名称。但是在此过程中,它用“我不知道”填充了BRANCH插槽。我想保持插槽填充的顺序。除非并且直到BANK插槽已被填充,否则BRANCH插槽不应被填充。我想确保BRANCH是有效的街道地址(这是BRANCH的slotType)。如何在填充广告位值时强制执行顺序?请在下面找到lambda代码:

const InProgressIfscCodeIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
            handlerInput.requestEnvelope.request.intent.name === 'ifscCode' &&
            handlerInput.requestEnvelope.request.dialogState !== 'COMPLETED'
    },
    handle(handlerInput) {
        const currentIntent = handlerInput.requestEnvelope.request.intent;
        return handlerInput.responseBuilder
            .addDelegateDirective(currentIntent)
            .getResponse();
    }
};

const CompletedIfscCodeIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
            handlerInput.requestEnvelope.request.intent.name === 'ifscCode'
    },
    async handle(handlerInput) {

        if (!handlerInput.requestEnvelope.request.intent.slots.BANK.value) {
            return handlerInput.responseBuilder
                .speak("Let me know which IFSC code for which bank are you looking for ?")
                .reprompt("Let me know which IFSC code for which bank are you looking for ?")
                .addElicitSlotDirective('BANK')
                .getResponse();
        } else {

            const bankName = handlerInput.requestEnvelope.request.intent.slots.BANK.value;
            const branch = handlerInput.requestEnvelope.request.intent.slots.BRANCH.value;

            // retrieve the bank details by calling the API
            const bankDetails = await getBankDetails(bankName, branch);

            console.log('--- length of the bank ---', bankDetails.banks.length);
            if (bankDetails.banks.length > 0) {
                console.log('-- bank details --', bankDetails);
                const speechText = `IFSC code of the ${bankName}, ${branch} is
                <break time="2s"/>
                <emphasis level="strong"><say-as interpret-as='spell-out'>${bankDetails.banks[0].IFSC}</say-as></emphasis>.
                <break time="2s"/>Try again for different bank by invoking open ifsc code finder.`;

                return handlerInput.responseBuilder
                    .speak(speechText)
                    .reprompt(speechText)
                    .withShouldEndSession(true)
                    .getResponse();
            } else {
                const speechText = "We were not able to get the bank details. Please try again by invoking open ifsc code finder.";
                return handlerInput.responseBuilder
                    .speak(speechText)
                    .reprompt(speechText)
                    .withShouldEndSession(true)
                    .getResponse();
            }

        }
    }
};

此外,字符串“我不知道”也不是有效的街道地址。到目前为止,为100万个地址训练模型是不可行的选择。我的问题是,BANK插槽的无效值不应用作分支。我想在这里添加验证。请在这里帮助我理解事情。

谢谢

1 个答案:

答案 0 :(得分:0)

使用Dialog.Delegate指令,您可以将控制权交给Alexa,而对Alexa要求的插槽没有任何控制。同样,您无法从代码中发送outputSpeechreprompt作为响应。而是将使用交互模型中定义的那些。

因此,在您的InProgressIfscCodeIntentHandler中,您实际上可以引出所需的插槽,而不是将控件委派给Alexa。如果要先填充BANK槽,请先引出BANK槽并进行验证。如果是正确的银行名称,请询问BRANCH,然后进行验证。

如果任何验证失败,例如用户说“我不知道” 。然后使用正确的错误消息再次引发该特定插槽。

const InProgressIfscCodeIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
            handlerInput.requestEnvelope.request.intent.name === 'ifscCode' &&
            handlerInput.requestEnvelope.request.dialogState !== 'COMPLETED'
    },
    handle(handlerInput) {
        const currentIntent = handlerInput.requestEnvelope.request.intent;

        // write a function to validate BANK name
        if (isBankNameValid(handlerInput)) {
            return handlerInput.responseBuilder
                .speak("Let me know which IFSC code for which bank are you looking for ?")
                .reprompt("Let me know which IFSC code for which bank are you looking for ?")
                .addElicitSlotDirective('BANK')
                .getResponse();
        }
        // write a function to validate BRANCH name
        if (isBranchNameValid(handlerInput)) {
            return handlerInput.responseBuilder
                .speak("Let me know the branch ?")
                .reprompt("Let me know the branch ?")
                .addElicitSlotDirective('BRANCH')
                .getResponse();

        }

        //Do your stuff

        return handlerInput.responseBuilder
            .speak("Your speech here")
            .reprompt("Your reprompt here")
            .getResponse();
    }

};