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

Bat*_*GDU 6 javascript amazon-web-services aws-lambda amazon-echo

这可能是一个简单的,但我几天都想不出来.

我想让Alexa有一个对话,比如;

>> Alexa,启动testSkill.

答:测试技能开始了.告诉我一个号码.

>>一个.

A:好的,现在告诉我一种颜色.

>>蓝色.

A:最后,告诉我一个动物名字.

>>鸡肉.

- 答:你告诉我一个,蓝色和鸡肉.

我发现我必须处理技能的会话属性,这是一个JSON,并在意图之间传递信息.

我使用这样的函数;

    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};
} 
Run Code Online (Sandbox Code Playgroud)

来自onIntent()函数的一段代码,我调用上面的函数.(我知道这是错的,但无法找到正确的方法)

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

而Intent Schema JSON就是这样;

 "intents": [
{
  "intent": "getColorNum",
  "slots": [
    {
      "name": "Color",
      "type": "ColorSlot"
    },
    {
      "name": "Number",
      "type": "NumberSlot"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

]}

所以,我做错了什么?哪里出错了?而且,我怎样才能像我提到的那样建立对话?谢谢你们.

Jos*_*lls 3

既然还没人给你任何答案,我想我应该给你我的两分钱。我选择的语言是 Python,但我可以给你一些高级的想法。

  • 使用会话对象来跟踪与对话相关的所有内容。计算“响应”后,将会话对象序列化为会话 json 并在下一个意图请求中反序列化。
  • 维护对话状态并使用有限状态机方法来了解您所在的位置以及在给定意图和特定对话状态的情况下要执行的回调。
  • 尽可能将对话逻辑与 ASK 界面分离。尝试编写可以在本地运行的测试。

如果您想了解我是如何实现这些的,请查看: