Alexa技能可以通过使用Alexa Skills Kit (ASK)和AWS Lambda来实现。以下是通过ASK和Lambda创建一个根据用户指定的时间间隔计数的Alexa技能的示例代码:
首先,创建一个新的Alexa Skill项目并打开AWS Lambda控制台。
创建一个新的Lambda函数,并选择Alexa Skills Kit作为触发器。
将以下示例代码粘贴到Lambda函数中:
exports.handler = function(event, context) {
try {
var request = event.request;
if (request.type === "IntentRequest") {
var intent = request.intent;
if (intent.name === "CountIntent") {
var interval = intent.slots.Interval.value;
var count = 0;
var message = "";
setInterval(function() {
count++;
message = "Count is now " + count;
var response = {
version: "1.0",
response: {
outputSpeech: {
type: "PlainText",
text: message
},
shouldEndSession: true
}
};
context.succeed(response);
}, interval * 1000);
}
}
} catch (e) {
context.fail("Exception: " + e);
}
};
保存并部署Lambda函数。
回到Alexa技能控制台,并添加以下用于CountIntent的示例语句和插槽:
Intent-Schema:
{
"intents": [
{
"intent": "CountIntent",
"slots": [
{
"name": "Interval",
"type": "AMAZON.NUMBER"
}
]
}
]
}
Sample Utterances:
CountIntent count every {Interval} seconds
CountIntent start counting every {Interval} seconds
CountIntent every {Interval} seconds count