首页 文章

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

提问于
浏览
0

My skill: 制作果汁建议,当用户说出某个州时,Alexa会根据该州给出果汁建议 . 状态和果汁对存储在一个数组中 .

Challenge: 目前,我的技能只执行一次 . 有没有办法我可以有一个循环,在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];
    }
  }
}

2 回答

  • 0

    MakeSuggestion 函数中,使用 ask 而不是 tell ,然后再次附加问题:

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

    是的,您可以通过使用状态而不是使用“:tell”来执行此操作,您可以通过“:ask”进行响应,以便alexa mic保持打开以进行用户交互 .

相关问题