这可能是一个简单的,但我无法在几天内弄清楚。
我想让 Alexa 进行对话,比如;
>> Alexa,开始 testSkill。
A:测试技能开始。告诉我一个数字。
>> 一。
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};
}
我调用上述函数的 onIntent() 函数中的一段代码。(我知道这是错误的,但无法找出正确的方法)
else if ("getColorNum" == intentName) {
if (session.attributes.nameOfPairOne === "") {
testConversation(intent, session, callback);
} else {
testConversation2(intent, session, callback);
}
}
Intent Schema JSON 就是这样;
"intents": [
{
"intent": "getColorNum",
"slots": [
{
"name": "Color",
"type": "ColorSlot"
},
{
"name": "Number",
"type": "NumberSlot"
}
]
}
] }
那么,我做错了所有事情吗?错误在哪里?而且,我怎样才能像我提到的那样建立对话?从现在开始感谢。