使用对话模型对 AMAZON.NUMBER 类型进行 Alexa 输入验证 [英] Alexa input validation for type AMAZON.NUMBER using dialog model
问题描述
我将 ASK SDK 2.0 用于 Node.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 文档说不要将 reprompt 与 ( 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.
我的处理程序如下所示:
My handlers look like this:
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();
},
};
我使用 Plan My Trip 示例作为我的起点,因此我的绝大多数代码将与它相同:https://github.com/alexa/alexa-cookbook/tree/master/feature-demos/skill-demo-计划我的旅行
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
中,我在handle
方法中添加了以下检查:
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
,我们将为具有错误值的特定插槽返回一个引出插槽指令.
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屋!