使用对话框模型对类型为AMAZON.NUMBER的Alexa输入验证 [英] Alexa input validation for type AMAZON.NUMBER using dialog model

查看:58
本文介绍了使用对话框模型对类型为AMAZON.NUMBER的Alexa输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASK SDK 2.0 forNode.js。
我有一种技巧,可以使用对话框模型提示用户一系列输入,所有这些都是AMAZON.NUMBER类型的必需插槽。当用户给出数值响应时,一切正常。但是,如果用户给出了非数字的响应,例如黄色,则插槽值将填充为:

I am using the ASK SDK 2.0 for Node.js. I have a skill that uses a dialog model to prompt the user for a series of inputs, all of which are required slots with the type AMAZON.NUMBER. When a user gives a numerical response, everything works fine. However, if the user gives a non-numeric response, such as "yellow", the slot value is filled in with:

value :?

并继续提示用户输入下一个广告位。如果他们提供了无效的响应,我如何才能再次提示用户该插槽?我已经翻阅了文档,找不到任何东西。理想情况下,我希望该技巧在提供有效输入之前提示用户(即,值不是

and moves on to propmpt the user for the input for the next slot. How can I get it to reprompt the user for that slot again if they provide an invalid response? I've poured over the documentation and can't find anything. Ideally I would want the skill to reprompt the user until a valid input is given (i.e., the value isn't "?")

(奇怪的是,如果我将类型设置为AMAZON.DATE,它将自动重新提示一次,然后,如果第二次提供了无效的类型,则该技能将退出。)

(Strangely, if i set the type to AMAZON.DATE, it will automatically reprompt once, and then if an invalid type is provided a second time the skill will just quit out.)

我的回答如下:

"response": {
    "directives": [{
        "type": "Dialog.Delegate",
        "updatedIntent": {
            "name": "MRRIntent",
            "confirmationStatus": "NONE",
            "slots": {
                "growthValue": {
                    "name": "growthValue",
                    "value": "?",
                    "confirmationStatus": "NONE"
                },
                "churnValue": {
                    "name": "churnValue",
                    "value": "?",
                    "confirmationStatus": "NONE"
                },
                "startingValue": {
                    "name": "startingValue",
                    "value": "10",
                    "confirmationStatus": "NONE"
                }
            }
        }
    }]
}

在此示例中,给 startingValue 给出了数值响应,但其他两个( growthValue churnValue )得到了非数字响应。

Where in this example startingValue was given a numerical response, but the other two (growthValue and churnValue) were given non-number responses.

在意图处理程序中的什么地方我可以检查该值并在出现故障的特定插槽?我正在使用Dialog.Directive,该文档说文档不与( https://developer.amazon.com/docs/custom-skills/dialog-interface-reference.html#details ),除非我误解了。

Where in the intent handler would I be able to check for this value and reprompt on the particular slots that are failing? I am using the Dialog.Directive which the docs say not to use reprompt with ( https://developer.amazon.com/docs/custom-skills/dialog-interface-reference.html#details ), unless I am misunderstanding it.

我的处理程序如下:

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

const CompletedPlanMyTripHandler = {
    canHandle(handlerInput) {
        const request = handlerInput.requestEnvelope.request;
        return request.type === 'IntentRequest' && request.intent.name === 'MRRIntent';
    },
    handle(handlerInput) {

        const responseBuilder = handlerInput.responseBuilder;
        const filledSlots = handlerInput.requestEnvelope.request.intent.slots;
        const slotValues = getSlotValues(filledSlots);

        let speechOutput = `Your values are: startingValue: ${slotValues.startingValue.synonym} growthValue: ${slotValues.growthValue.synonym}, and churnValue: ${slotValues.churnValue.synonym}`

        return responseBuilder
            .speak(speechOutput)
            .getResponse();
    },
};

我以计划我的旅行示例为起点,所以我的绝大多数代码都在使用与此相同: https://github.com/alexa/alexa-cookbook/tree/master/feature-demos/skill-demo-plan-my-trip

I used the Plan My Trip example as my starting point, so the vast majority of my code is going to be identical to it: https://github.com/alexa/alexa-cookbook/tree/master/feature-demos/skill-demo-plan-my-trip

我想念/不了解什么?谢谢

What am I missing / not understanding? Thanks

推荐答案

我相信我已经找到了解决方案-我不确定这是否是最好的解决方案,但是看来

I believe I've found a solution -- I'm not sure it's the best solution, but it appears to work.

CompletedPlanMyTripHandler 中,我将以下检查添加到了句柄方法:

In the CompletedPlanMyTripHandler, I added the following check to the handle method:

handle(handlerInput) {
    const currentIntent = handlerInput.requestEnvelope.request.intent;

    let slots = currentIntent.slots;
    let badInputSlot;
    for (let x in slots) {
        if (slots.hasOwnProperty(x) && slots[x].hasOwnProperty('value')) {
            if (isNaN(slots[x].value)) {
                badInputSlot = x;
                break;
            }
        }
    }
    if (badInputSlot) {
        return handlerInput.responseBuilder.speak('I do not understand. Please respond with a number.').reprompt('Please respond with a number').addElicitSlotDirective(badInputSlot, currentIntent).getResponse();
    } else {
        return handlerInput.responseBuilder
            .addDelegateDirective(currentIntent)
            .getResponse();
    }
},

它的作用是在for循环中进行检查意图上的插槽,查看每个插槽是否具有 value 属性(仅在提供响应后才添加),然后执行 isNaN 检查该值是否实际上有效。如果不是,那么我们需要再次询问用户该值,因此我们将插槽的名称存储在 badInputSlot 中,并退出循环。

What it's doing is in the for loop its checking the the slots on the intent, seeing if each one has a value property (which is only added once a response has been supplied), and then does an isNaN check on it to see if the value is in fact valid. If it is not, we need to ask the user again for that value, so we store the slot's name in badInputSlot and break out of the loop.

现在,我们继续执行if语句,如果为 badInputSlot 分配了一个值,我们将为特定的对象返回一个被引发的slot指令。

Now, we move on to the if statement, and if there is a value assigned to badInputSlot we return an elicit slot directive for the specific slot that had a bad value.

然后,在用户提供新值之后,我们重复该过程,直到 handlerInput.requestEnvelope中的每个插槽。 request.intent.slots 具有 value 属性,该属性通过了我们的 isNaN 验证检查。一旦发生这种情况, badInputSlot 将是未定义的,我们的if语句将进入 else 块并返回委托指令完成意图。

Then, after the user supplies a new value, we repeat the process until every slot in handlerInput.requestEnvelope.request.intent.slots has a value property that passes our isNaN validation check. Once that happens, badInputSlot will be undefined, and our if statement will go to the else block and return a delegate directive to finish off the intent.

这篇关于使用对话框模型对类型为AMAZON.NUMBER的Alexa输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆