【发布时间】:2021-09-08 20:26:33
【问题描述】:
做一些智能家居技能开发,我的意图是“我的电池要多久才能{状态}”状态是空的或充满的。我知道我可以用不同的意图来做到这一点,但我不想有两个很多的意图,因为我已经有很多了。我基本上希望用户说满或空来代替 {state}。然后从那里,根据他们的答案,给出完整或空的不同答案。网上查的不多,希望大家帮忙。我也是代码新手。
【问题讨论】:
标签: alexa alexa-skills-kit alexa-slot
做一些智能家居技能开发,我的意图是“我的电池要多久才能{状态}”状态是空的或充满的。我知道我可以用不同的意图来做到这一点,但我不想有两个很多的意图,因为我已经有很多了。我基本上希望用户说满或空来代替 {state}。然后从那里,根据他们的答案,给出完整或空的不同答案。网上查的不多,希望大家帮忙。我也是代码新手。
【问题讨论】:
标签: alexa alexa-skills-kit alexa-slot
您应该为您的电池状态创建一个插槽:
{
"name": "BatteryState",
"values": [
{
"id": "empty",
"name": {
"value": "empty"
}
},
{
"id": "full",
"name": {
"value": "full"
}
}
]
}
您可以在 UI 中自行创建,也可以在 JSON 编辑器中粘贴到 types 集合中。
然后在您的意图中使用创建的插槽
{
"name": "BatteryStateIntent",
"slots": [
{
"name": "batteryState",
"type": "BatteryState"
}
],
"samples": [
"how long until my battery is {batteryState}"
]
}
您可以在 UI 中自行创建,也可以在 JSON 编辑器中粘贴到 intents 集合中。
然后在代码中:
const BatteryStateIntentHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
Alexa.getIntentName(handlerInput.requestEnvelope) === "BatteryStateIntent"
);
},
handle(handlerInput) {
const batteryStatus =
handlerInput.requestEnvelope.request.intent.slots.batteryState.value;
let speakOutput = "";
if (batteryStatus === "full") {
const timeToFull = 10; // or some sophisticated calculations ;)
speakOutput += `Your battery will be full in ${timeToFull} minutes!`;
} else if (batteryStatus === "empty") {
const timeToEmpty = 5; // or some sophisticated calculations ;)
speakOutput += `Your battery will be full in ${timeToEmpty} minutes!`;
} else {
speakOutput += "I don't know this status";
}
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
},
};
确保您使用了与构建器中相同的意图名称。最后,将创建的意图处理程序添加到请求处理程序:
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
BatteryStateIntentHandler,
// ...
)
.lambda();
【讨论】: