【问题标题】:Alexa Skill Loop: Based off of User Input, Alexa will Say SomethingAlexa 技能循环:根据用户输入,Alexa 会说些什么
【发布时间】:2018-05-03 15:52:44
【问题描述】:

我的技能:提出果汁建议,当用户说出一个状态时,Alexa 会根据该状态给出果汁建议。状态和果汁对存储在一个数组中。

挑战:目前,我的技能只会执行一次。有没有办法让我有一个循环,在 Alexa 给出建议后,Alexa 会再次提问并等待用户响应?附件是我的技能代码。谢谢。

var Alexa = require('alexa-sdk');

const APP_ID = undefined;

const skillData = [
    {
        state: "FLORIDA",
        suggestion: "My suggestion for Florida is organic orange juice by Naked"
    },
    {
        state: "CALIFORNIA",
        suggestion: "My suggestion for California is pomegrante by POM!!"
    },
    {
        state: "NEW JERSEY",
        suggestion: "My suggestion for Jersey is blueberry by Jersey Fresh"
    }
];

var number = 0;
while(number<3){
var handlers = {
  'LaunchRequest': function () {

    this.emit(':ask', 'I can suggest a juice from any state in the United States. What state would you like a juice suggestion for?', 'Tell me a state name and I will suggest a local juice from there.');
  
      
    },
  'MakeSuggestion': function() {
      var stateSlot = this.event.request.intent.slots.state.value;

      this.emit(':tell', getSuggestion(skillData, 'state', stateSlot.toUpperCase()).suggestion);

  },
  'Unhandled': function () {
    this.emit(':tell', 'Sorry, I don\'t know what to do');
  },
  'AMAZON.HelpIntent': function () {
      this.emit(':ask', "What can I help you with?", "How can I help?");
  },
  'AMAZON.CancelIntent': function () {
      this.emit(':tell', "Okay!");
  },
  'AMAZON.StopIntent': function () {
      this.emit(':tell', "Goodbye!");
  },
}
number = number+1;
};

exports.handler = function(event, context){
  var alexa = Alexa.handler(event, context);
  alexa.registerHandlers(handlers);
  alexa.execute();
};

function getSuggestion(arr, propName, stateName) {
  for (var i=0; i < arr.length; i++) {
    if (arr[i][propName] == stateName) {
      return arr[i];
    }
  }
}

【问题讨论】:

    标签: javascript amazon-web-services alexa alexa-skills-kit alexa-skill


    【解决方案1】:

    在您的MakeSuggestion 函数中,使用ask 而不是tell,并再次附加问题:

    this.emit(':ask', getSuggestion(skillData, 'state', stateSlot.toUpperCase()).suggestion + '. Tell me another state and I give you another suggestion!');
    

    【讨论】:

      【解决方案2】:

      是的,你可以通过使用 state 来做到这一点,而不是使用 ":tell",你可以通过 ":ask" 来响应,这样 alexa mic 保持打开状态以供用户交互。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-06
        • 1970-01-01
        • 2021-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多