【问题标题】:Provide a list of numbers to Alexa向 Alexa 提供号码列表
【发布时间】:2017-08-04 19:16:43
【问题描述】:

我想创建一个从用户那里获取 6 个号码的彩票技能。 我目前正在通过阅读示例和开发人员指南进行学习,我可以阅读指南并获得一项工作技能,该技能将接受一个输入然后结束会话。但我相信我需要以某种方式创建一个对话框,这就是我卡住的地方。

在设计方面,我希望对话框如下所示: Alexa:请提供第一个号码 用户:1 Alexa:现在是第二个…… 用户:2 等等等等

但我认为如果是这样就可以了: Alexa:请拨出 6 个号码 用户:1、2、3、4、5、6。

这甚至可能吗?我是否必须创建一个名为“数字”的自定义插槽类型,然后输入数字,例如 1-50 或任何限制?

充其量,我目前可以让它要求一个数字,所以它确实是我坚持的对话交互。有没有人做过这样的事情?

谢谢。

【问题讨论】:

    标签: alexa-skills-kit alexa-skill alexa-voice-service


    【解决方案1】:

    两个问题都是。您可以将响应与 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 时打破状态的条件。 首先尝试上面的代码,因为它更容易看到不同的状态。

    【讨论】:

    • 你不能只做 {number} {number} {number} {number} {number} {number} 吗?我已经将类似的用于随机单词列表。这样,一旦用户说了 6 个单词,它只会发送一个意图。然后,您只需确保它们都在您要查找的范围内。
    猜你喜欢
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 2013-07-07
    • 2019-01-15
    相关资源
    最近更新 更多