亚马逊 Alexa 技能

Posted

技术标签:

【中文标题】亚马逊 Alexa 技能【英文标题】:Amazon Alexa Skill 【发布时间】:2019-02-04 05:28:53 【问题描述】:

我想问 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】:

首先,不要创建自定义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);
  

使用状态处理程序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,完成后将其删除。

【讨论】:

感谢您的回复,但以上方法均无效。它给出了没有响应的错误。 请勿复制粘贴代码,仅供参考。 我没有复制粘贴。当我尝试运行此代码时,如果我用我的技能名称说是,比如告诉 citytravel 是,那么它会从我第一次声明是的意图重复对话,如果我只是说是,那么它会说存在问题请求技能的响应。 进入技能会话后,您不必每次都使用技能调用名称。

以上是关于亚马逊 Alexa 技能的主要内容,如果未能解决你的问题,请参考以下文章

亚马逊推Alexa技能开发工具:使用者无需编程能力

如何使用亚马逊的 Alexa API 为技能设置帐户链接?

亚马逊 Alexa 意图技能控制台

如何在 Alexa Skill 中使用 Java 获取亚马逊用户电子邮件

Alexa 技能包与 Alexa 语音服务

Alexa - 允许用户将商品添加到亚马逊购物篮/购物车。需要帮助