简体   繁体   English

Alexa(Amazon Echo)对话技巧 - 使用会话属性(JavaScript - AWS Lambda)

[英]Alexa (Amazon Echo) conversation skill - Using Session Attributes (JavaScript - AWS Lambda)

this might be an easy one but I couldn't figure it out in days. 这可能是一个简单的,但我几天都想不出来。

I want to make Alexa have a conversation, like; 我想让Alexa有一个对话,比如;

>> Alexa, start testSkill. >> Alexa,开始测试技能。

A: Test Skill started. 答:测试技能开始了。 Tell me a number. 告诉我一个号码。

>> One. >>一个。

A: Okay, tell me a color now. A:好的,现在告诉我一种颜色。

>> Blue. >>蓝色。

A: And finally, tell me an animal name. A:最后,告诉我一个动物名字。

>> Chicken. >>鸡肉。

A: You told me one, blue and chicken. - 答:你告诉我一个,蓝色和鸡肉。

I found out that I have to handle the Session Attributes of the skill, which is a JSON holds and transfers the information between intents. 我发现我必须处理技能的会话属性,这是一个JSON,并在意图之间传递信息。

I use functions like this ; 我使用这样的函数;

    function testConversation(intent, session, callback) {

    var cardTitle = intent.name;
    var repromptText = "";
    var sessionAttributes = { // I don't know how to handle this
        nameOfPairOne: "",
        nameOfPairTwo: "",
    };
    var shouldEndSession = false;
    var speechOutput = "";

    var color= convertToASCII(intent.slots.color.value);
    sessionAttributes.nameOfPairOne = color;

    speechOutput = "You said "+sessionAttributes.nameOfPairOne+". Please say another thing. ";
    callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}

 function testConversation2(intent, session, callback) {

    var cardTitle = intent.name;
    var repromptText = "";
    var sessionAttributes = session.attributes;
    var shouldEndSession = false;
    var speechOutput = "";

    var number = convertToASCII(intent.slots.number.value);
    sessionAttributes.nameOfPairTwo = number;

    speechOutput = "You first said "+sessionAttributes.nameOfPairOne+", and now said "+sessionAttributes.nameOfPairTwo;
    callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}

//------Helpers that build all of the responses ---------//
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
    return {
        outputSpeech: {type: "PlainText", text: output},
        card: {type: "Simple", title: "SessionSpeechlet - " + title, content: "SessionSpeechlet - " + output},
        reprompt: {outputSpeech: {type: "PlainText", text: repromptText}},
        shouldEndSession: shouldEndSession
    };
}


function buildResponse(sessionAttributes, speechletResponse) {
    return {version: "1.0", sessionAttributes: sessionAttributes, response: speechletResponse};
} 

A piece of code from onIntent() function where I call the above functions. 来自onIntent()函数的一段代码,我调用上面的函数。 (I know it's wrong but couldn't figure out the right way) (我知道这是错的,但无法找到正确的方法)

 else if ("getColorNum" == intentName) {
    if (session.attributes.nameOfPairOne === "") {
        testConversation(intent, session, callback);
    } else {
        testConversation2(intent, session, callback);
    }
}

And the Intent Schema JSON is like that; 而Intent Schema JSON就是这样;

 "intents": [
{
  "intent": "getColorNum",
  "slots": [
    {
      "name": "Color",
      "type": "ColorSlot"
    },
    {
      "name": "Number",
      "type": "NumberSlot"
    }
  ]
}

] } ]}

So, am I doing all of the things wrong ? 所以,我做错了什么? Where is the mistake ? 哪里出错了? And, how can I build a conversation like I mentioned ? 而且,我怎样才能像我提到的那样建立对话? Thanks from now. 谢谢你们。

Since no one gave you any answer yet I thought I'd give you my two cents. 既然没有人给你任何答案,我想我会给你两分钱。 My language of choice is Python but I can give you some high-level ideas. 我选择的语言是Python,但我可以给你一些高级的想法。

  • Use a session object to keep track of everything related to the conversation. 使用会话对象来跟踪与对话相关的所有内容。 After you compute the "response", serialize your session object into the session json and deserialize it in the next intent request. 计算“响应”后,将会话对象序列化为会话json,并在下一个intent请求中反序列化它。
  • Maintain a conversation state and use a finite-state-machine approach to know where you are and what callback to execute given an intent and a specific conversation state. 保持对话状态并使用有限状态机方法知道您的位置以及在给定意图和特定对话状态的情况下执行的回调。
  • Decouple your conversation logic from the ASK interface as much as you can. 尽可能多地将对话逻辑与ASK界面分离。 Try to write tests you can run locally. 尝试编写可以在本地运行的测试。

If you want to see how I implemented these, check out: 如果您想了解我如何实现这些,请查看:

There are several ways to approach this, depending on the complexity of what you are trying to store. 有几种方法可以解决这个问题,具体取决于您要存储的内容的复杂程度。

First, using slots. 首先,使用插槽。 If One, blue, chicken and such are in classified categories, you can store them in slots and access them the JSON way, getting the value and store in a variable in your JavaScript code. 如果One,blue,chicken等属于分类类别,则可以将它们存储在插槽中并以JSON方式访问它们,获取值并将其存储在JavaScript代码中的变量中。 Example (sudo): 示例(sudo):

var something1 = event.request.intent.slots.assetName.value;
var something2 = event.request.intent.slots.assetName.value;
buildSpeechletResponse(`my ${something1} likes ${something2} and blah blah blah`)

Second, using attributes to store something and use it late on runtime, Alexa won't remember this after skill ended. 其次,使用属性来存储某些内容并在运行时后期使用它,Alexa在技能结束后将不记得这一点。 Example: 例:

'BreakfastIntent': function () {
    var restaurant = randomArrayElement(getRestaurantsByMeal('breakfast'));
    this.attributes['restaurant'] = restaurant.name;

    var say = 'For breakfast, try this, ' + restaurant.name + '. Would you like to hear more?';
    this.response.speak(say).listen(say);
    this.emit(':responseReady');
},

Last, storing them in a database, highly recommended one is DynamoDB and there are plenty of tutorial for storing and access Dynamo from Alexa Skill Kit. 最后,将它们存储在数据库中,强烈推荐的是DynamoDB,并且有大量的教程可以存储和访问Alexa Skill Kit中的Dynamo。 Checkout: https://github.com/alexa/alexa-cookbook/tree/master/aws/Amazon-DynamoDB/read 结帐: https//github.com/alexa/alexa-cookbook/tree/master/aws/Amazon-DynamoDB/read

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM