两个问题都是。您可以将响应与 6 个不同的自定义插槽串在一起。 “用户:我的号码是 {num1}、{num2}、{num3}、{num4}、{num5}、{num6}”,并使用技能 beta 开发人员将它们全部设为必需。但是,如果用户没有恰当地表达他们的答案并且 Alexa 必须提出后续问题以获取每个数字,这将是一个相当糟糕的用户体验。您将遇到的最后一个问题是,虽然自定义插槽可以定义为包含数字 1-50,但 alexa 通常会识别与自定义插槽中提供的值相似的值,例如 50-99 的数字。然后由您来检查您收到的值是否在 1 到 50 之间。如果不是,您可能想要求用户在适当的范围内提供不同的数字。
结论:您需要进行单独的交互,用户一次只提供一个数字。
Alexa:"you will be prompted for 6 numbers between 1 and 50 please state them one at a time. Choose your first number."
User:"50"
Alexa:"Your First number is 50, Next number."...
您可以使用单一意图来实现这一点。让我们将这个意图命名为GetNumberIntent。 GetNumberIntent 将有沿
行的示例话语
{number}
pick {number}
choose {number}
其中 {number} 是自定义槽类型或简称为 AMAZON.NUMBER。然后由您来检查该数字是否在 1 到 50 之间。
我使用 SDK 在 Node.js 中编程。您的实施可能会因您选择的语言而异。
我要做的是定义 6 个不同的状态处理程序。每个处理程序都应该有 GetNumberIntent。如果槽值适当,则返回 GetNumberIntent 时,将该值存储到会话数据和/或 dynamodb 并前进到下一个状态。如果槽值无效,则保持例如状态“NumberInputFiveStateHandlers”,直到收到一个好的值,然后将状态更改为下一个“NumberInputSixStateHandlers”
var NumberInputFiveStateHandlers = Alexa.CreateStateHandler(states.NUMFIVEMODE, {
'NewSession': function () {
this.emit('NewSession'); // Uses the handler in newSessionHandlers
},
//Primary Intents
'GetNumberIntent': function () {
let message = ` `;
let reprompt = ` `;
let slotValue = this.event.request.intent.slots.number.value;
if(parseInt(slotValue) >= 1 && parseInt(slotValue) <= 50){
this.handler.state = states.NUMSIXMODE;
this.attributes['NUMBERFIVE'] = this.event.request.intent.slots.number.value;
message = ` Your fifth number is `+slotValue+`. please select your sixth value. `;
reprompt = ` please select your sixth value. `;
}else{
message = ` The number `+slotValue)+` is not in the desired range between 1 and 50. please select a valid fifth number. `;
reprompt = ` please select your fifth value. `;
}
this.emit(':ask',message,reprompt);
},
//Help Intents
"InformationIntent": function() {
console.log("INFORMATION");
var message = ` You've been asked to choose a lottery number between 1 and 50. Please say your selection.`;
this.emit(':ask', message, message);
},
"AMAZON.StopIntent": function() {
console.log("STOPINTENT");
this.emit(':tell', "Goodbye!");
},
"AMAZON.CancelIntent": function() {
console.log("CANCELINTENT");
this.emit(':tell', "Goodbye!");
},
'AMAZON.HelpIntent': function() {
var message = `You're playing lottery. you'll be picking six numbers to play the game. For help with your current situation say Information. otherwise you may exit the game by saying quit.`;
this.emit(':ask', message, message);
},
//Unhandled
'Unhandled': function() {
console.log("UNHANDLED");
var reprompt = ' That was not an appropriate response. Please say a number between 1 and 50.';
this.emit(':ask', reprompt, reprompt);
}
});
这是第五个请求的示例。您将拥有 6 个与此类似的相同状态,它们背靠背排列。最终你会得到 6 个会话值。
this.attributes['NUMBERONE']
this.attributes['NUMBERTWO']
this.attributes['NUMBERTHREE']
this.attributes['NUMBERFOUR']
this.attributes['NUMBERFIVE']
this.attributes['NUMBERSIX']
然后您可以将这些值用于您的游戏。
如果您之前没有使用过 alexa-sdk,您必须记住注册您的状态处理程序并将您的模式添加到 states 变量。
alexa.registerHandlers(newSessionHandlers, NumberInputOneStateHandlers, ... NumberInputSixStateHandlers);
var states = {
NUMONEMODE: '_NUMONEMODE',
...
...
NUMSIXMODE: '_NUMSIXMODE',
}
此答案并非旨在涵盖使用 Alexas-SDK 进行编码的基础知识。对于该主题的更具体问题,还有其他资源。
另外,由于您的意图是相同的 [GetNumberIntent],您可以使用单个 StateHandler 将新的有效数字推送到数组中,直到数组达到所需的长度。这将只需要 Intent Handler 内部的更多逻辑以及一旦数组长度为 6 时打破状态的条件。
首先尝试上面的代码,因为它更容易看到不同的状态。