如何获取和使用 Alexa 技能意图响应的确认 'yes' 或 'no'

How to get and use confirmation 'yes' or 'no' for Alexa skill intent response

我正在开发一项 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,看来可以达到目的。但是我不确定如何实现它。

从技能中完成您想要的事情的最简单方法是处理技能中的 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
  }
};

请注意,在更复杂的技能中,您可能必须存储一些状态才能确定用户发送了 'Yes' 意图以响应您是否 "do something" 的问题。您可以使用 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,但如果您想要做的只是获得简单的 Yes/No 确认作为提示启动请求比我认为上面的示例更容易实现。

以下是我在技能的 Lambda 函数中使用 javascript 编码的方式:

    'myIntent': function() {
        
        // there is a required prompt setup in the language interaction model (in the Alexa Skill Kit platform) 
        // To use it we "deligate" it to Alexa via the delegate dialoge directive.
        
        if (this.event.request.dialogState === 'STARTED') {
            // Pre-fill slots: update the intent object with slot values for which
            // you have defaults, then emit :delegate with this updated intent.
            //var updatedIntent = this.event.request.intent;
            //updatedIntent.slots.SlotName.value = 'DefaultValue';
            //this.emit(':delegate', updatedIntent);
            this.emit(':delegate');
        } else if (this.event.request.dialogState !== 'COMPLETED'){
            this.emit(':delegate');
        } else {
            // completed
            var intentObj = this.event.request.intent;
            if (intentObj.confirmationStatus !== 'CONFIRMED') {
                // not confirmed
                if (intentObj.confirmationStatus !== 'DENIED') {
                    // Intent is completed, not confirmed but not denied
                    this.emit(':tell', "You have neither confirmed or denied. Please try again.");
                } else {
                    // Intent is completed, denied and not confirmed
                    this.emit(':ask', 'I am sorry but you cannot continue.');
                }
            } else {
                // intent is completed and confirmed. Success!
                var words = "You have confirmed, thank you!";
                this.response.speak(words);
                this.emit(':responseReady');
            }
        }
        
    },

并且您需要在 Alexa 交互模型中启用对意图的确认。