Alexa Skill Loop:基于用户输入,Alexa会说些什么

时间:2018-05-03 15:52:44

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

我的技能:制作果汁建议当用户说某个州时,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];
    }
  }
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

MakeSuggestion函数中,使用ask代替tell,然后重新附加问题:

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

答案 1 :(得分:0)

是的,你可以通过使用州而不是使用&#34;:告诉&#34;来做到这一点,你可以通过&#34;来回答:问&#34;所以alexa mic仍然可以进行用户交互。