如何获取并使用Alexa技能意图响应的确认'是'或'否'

时间:2018-03-31 13:21:45

标签: amazon-web-services aws-lambda alexa alexa-skills-kit alexa-skill

我正在开发一种Alexa技能,在启动时它会询问Do you want to perform something ? 根据用户的回复'yes''no',我想启动另一个意图。

var handlers = {
  'LaunchRequest': function () {
    let prompt = this.t("ASK_FOR_SOMETHING");
    let reprompt = this.t("LAUNCH_REPROMPT");
    this.response.speak(this.t("WELCOME_MSG") + ' ' + prompt).listen(reprompt);
    this.emit(':responseReady');
  },
  "SomethingIntent": function () {
    //Launch this intent if the user's response is 'yes'
  }
};

我确实看过dialog model,似乎它将达到目的。但我不确定如何实施它。

1 个答案:

答案 0 :(得分:5)

从技能中寻找所需内容的最简单方法是从您的技能中处理AMAZON.YesIntentAMAZON.NoIntent(确保将它们添加到交互模型中):

var handlers = {
  'LaunchRequest': function () {
    let prompt = this.t("ASK_FOR_SOMETHING");
    let reprompt = this.t("LAUNCH_REPROMPT");
    this.response.speak(this.t("WELCOME_MSG") + ' ' + prompt).listen(reprompt);
    this.emit(':responseReady');
  },
  "AMAZON.YesIntent": function () { 
    // raise the `SomethingIntent` event, to pass control to the "SomethingIntent" handler below 
    this.emit('SomethingIntent');
  },
  "AMAZON.NoIntent": function () {
    // handle the case when user says No
    this.emit(':responseReady');
  }
  "SomethingIntent": function () {
    // handle the "Something" intent here
  }
};

请注意,在更复杂的技能中,您可能需要存储一些状态,以确定用户发送了“是”意图以回答您是否“做某事”的问题。您可以使用session object中的技能会话属性保存此状态。例如:

var handlers = {
  'LaunchRequest': function () {
    let prompt = this.t("ASK_FOR_SOMETHING");
    let reprompt = this.t("LAUNCH_REPROMPT");
    this.response.speak(this.t("WELCOME_MSG") + ' ' + prompt).listen(reprompt);
    this.attributes.PromptForSomething = true;
    this.emit(':responseReady');
  },
  "AMAZON.YesIntent": function () { 
    if (this.attributes.PromptForSomething === true) {
      // raise the `SomethingIntent` event, to pass control to the "SomethingIntent" handler below 
      this.emit('SomethingIntent');
    } else {
      // user replied Yes in another context.. handle it some other way
      //  .. TODO ..
      this.emit(':responseReady');
    }
  },
  "AMAZON.NoIntent": function () {
    // handle the case when user says No
    this.emit(':responseReady');
  }
  "SomethingIntent": function () {
    // handle the "Something" intent here
    //  .. TODO ..
  }
};

最后,您还可以在问题中提到使用Dialog Interface,但是如果你想要做的就是从发布请求中得到一个简单的是/否确认作为提示而不是我我认为上面的例子很容易实现。