【问题标题】:alexa skill maintain the order of slots and add validationalexa技能维护插槽的顺序并添加验证
【发布时间】:2019-03-19 23:00:42
【问题描述】:

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

该技能首先提示用户输入银行名称。如果用户提供了无效的银行名称,例如“我不知道”。它识别出它不是有效的银行名称,并再次提示用户输入银行名称。但在这个过程中,它用“我不知道”填充 BRANCH 槽。我想保持插槽被填充的顺序。 BRANCH 槽不应该被填充,除非并且直到 BANK 槽被填充。我想确保 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 插槽的无效值不应用作分支。我想在这里添加验证。请帮助我理解这里的内容。

谢谢

【问题讨论】:

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


    【解决方案1】:

    使用Dialog.Delegate 指令,您可以将控制权交给 Alexa,而您无法控制 Alexa 请求哪个插槽。您也不能从您的代码中发送outputSpeech 或reprompt 作为响应。而是使用交互模型中定义的那些。

    因此,在您的 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();
        }
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多