亚马逊Alexa技能

时间:2018-08-29 12:13:34

标签: alexa alexa-skills-kit alexa-skill

我想问alexa不同类型的问题,然后最后我要问“您还有其他想知道的吗?”当我说“是”(在这里是可行的建议)时,应该根据自己的意图向我建议。就像我在其中一样

IncityIntent:

    'InCityIntent': function () {
        speechOutput = '';


speechOutput = "The atmosphere in the city is beautiful. Is there anything else you would like to know";
        this.emit(":ask", speechOutput, speechOutput);


'YesIntent': function () {
        speechOutput = '';
/*when the user say yes, he should get this output*/  
            speechOutput = You can learn more about city by trying, alexa what are the best places in the city";
            this.emit(":tell",speechOutput, speechOutput);

FoodIntent:

    'FoodIntent': function () {
        speechOutput = '';


speechOutput = "Food in the city is delicious. Is there anything else you would like to know";
        this.emit(":ask", speechOutput, speechOutput);

'YesIntent': function () {
        speechOutput = '';
/*change in response here*/
            speechOutput = You can learn more about food by trying, alexa what are the best restaurants in the city";
            this.emit(":tell",speechOutput, speechOutput);

1 个答案:

答案 0 :(得分:0)

第一件事,不要创建自定义YesIntentNoIntent,而要使用AMAZON.YesIntentAMAZON.NoIntent。如果需要,您始终可以将话语添加到这些预定义的意图中。

您的问题可以通过两种方式解决。

使用sessionAttributes
添加previousIntent属性或其他内容,以便在收到初始请求(例如sessionAttributes)时跟踪InCityIntent中的转换。然后在您的AMAZON.YesIntentAMAZON.NoIntent处理程序中检查先前的意图并做出相应的答复。

 'InCityIntent': function () {
       const speechOutput = "The atmosphere in the city is beautiful. Is there anything else you would like to know";
       const reprompt = "Is there anything else you would like to know";
       this.attributes['previousIntent'] = "InCityIntent";
       this.emit(":ask", speechOutput, reprompt);
  }

 'Amazon.YesIntent': function () {
     var speechOutput =  "";
     var reprompt = "";
     if (this.attributes 
        && this.attributes.previousIntent
        && this.attributes.previousIntent === 'InCityIntent' ) {
        speechOutput = "You can learn more about city by trying, Alexa what are the best places in the city";
        reprompt = "your reprompt";
     } else if ( //check for FoodIntent ) {

       // do accordingly
     }
     this.attributes['previousIntent'] = "Amazon.YesIntent";
     this.emit(":ask", speechOutput, reprompt);
  }

使用STATE处理程序
ask-nodejs-sdk v1具有状态处理程序,可以根据状态生成响应。这个想法很相似,sdk将为您添加sessionAttribute参数,并且when会自动针对该状态映射处理程序。

 'InCityIntent': function () {
       const speechOutput = "The atmosphere in the city is beautiful. Is there anything else you would like to know";
       const reprompt = "Is there anything else you would like to know";
       this.handler.state = "ANYTHING_ELSE";
       this.emit(":ask", speechOutput, reprompt);
  }

const stateHandlers = Alexa.CreateStateHandler("ANYTHING_ELSE", {
    "AMAZON.YesIntent": function () {
       var speechOutput =  "You can learn more about city by trying, Alexa what are the best places in the city";
       var reprompt = "your reprompt";
       this.emit(":ask", speechOutput, reprompt);
    },

一旦设置了state,那么下次将触发在该特定状态处理程序中定义的意图处理程序。相应地更改您的state,然后将其删除。