问题描述
我正在开发我的第一个Alexa技能,并且想尝试改善其对话框管理。
我的技能有以下几种意图:一种是获取室内温度,另一种是获取湿度,依此类推。
每个意图都有一个代表我房子地板/房间的插槽,所以Alexa的典型问题是“什么是一楼的温度?”
每次执行意图时,它都会将插槽存储在会话属性中,这样我就可以处理如下对话:
我:“ Alexa,一楼的温度是多少?”
Alexa:“一楼的温度是24度”
我:“湿度?”
Alexa:“一楼的湿度是50%”
我要实现的下一步是这种类型的对话框:
我:“ Alexa,一楼的温度是多少?”
Alexa:“一楼的温度是24度”
我:“在二楼吗?”
Alexa:“二楼的温度是26度”
在实践中,我需要启动最后执行的意图,而不必说出话语。
我正在考虑创建一个仅接收插槽的新通用意图,然后将请求分派到最后执行的意图。
我可以跟踪将其ID保存在会话属性中的最后一个意图。
有更好的方法吗?
每个建议都值得欢迎,因为自上周一以来我一直在开发Alexa技能! :-)
非常感谢。
解决方法
您在正确的轨道上。要记住的是,您可以对多个插槽有一个意图,而不需要全部。
在这里,您可以为所有这些创建单个意图。
How are things on the {floor}
And on the {floor}
What is the {condition}
What is the {condition} on the {floor}
然后,创建“条件”和“地板”插槽类型,并用适当的样本值(例如,条件的“温度”和地板的“第一层”)填充它们。然后确保将这些插槽类型分配给您的意图中的插槽。
然后您的处理程序代码如下所示……
const conditionIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'conditionIntent';
},handle(handlerInput) {
var speakOutput = "";
var condition = "";
var floor = "";
const attributesManager = handlerInput.attributesManager;
const attributes = attributesManager.getSessionAttributes();
if (handlerInput.requestEnvelope.request.intent.slots.condition.hasOwnProperty('value')) {
condition = handlerInput.requestEnvelope.request.intent.slots.condition.value;
} else if (attributes.hasOwnProperty('condition')) {
if(attributes.condition !== "") condition = attributes.condition;
}
if (handlerInput.requestEnvelope.request.intent.slots.floor.hasOwnProperty('value')) {
floor = handlerInput.requestEnvelope.request.intent.slots.floor.value;
} else if (attributes.hasOwnProperty('floor')) {
if(attributes.floor !== "") floor = attributes.floor;
}
if (floor !== "" && condition === ""){
speakOutput = "Here's the conditions for the " + floor;
} else if (floor === "" && condition !== ""){
speakOutput = "Here's the " + condition + " throughout the house";
} else if (floor !== "" && condition !== ""){
speakOutput = "Here's the " + condition + " on the " + floor;
} else {
speakOutput = "I have no idea what you're saying. Are you okay?"
}
attributes.floor = floor;
attributes.condition = condition;
attributesManager.setSessionAttributes(attributes);
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('What else can I tell you?')
.getResponse();
}
};
我没有编写实际的代码来表示值,只是占位符响应,但是您应该明白这一点。添加更多包含一个或两种插槽类型的运营商词组,以使此操作以更多方式处理人们可能会要求提供此信息的方式。