Alexa Hello World 技能项目教程
1. 项目的目录结构及介绍
skill-sample-nodejs-hello-world/
├── ask/
│ ├── github/
│ └── hooks/
├── instructions/
├── lambda/
│ └── custom/
├── models/
├── .gitignore
├── LICENSE.txt
├── README.md
└── skill.json
- ask/: 包含与 Alexa Skills Kit (ASK) CLI 相关的文件和配置。
- instructions/: 包含项目的说明文档。
- lambda/custom/: 包含技能的后端逻辑代码。
- models/: 包含语音交互模型的定义。
- .gitignore: 指定 Git 版本控制系统忽略的文件和目录。
- LICENSE.txt: 项目的许可证文件。
- README.md: 项目的介绍和使用说明。
- skill.json: 技能的配置文件。
2. 项目的启动文件介绍
项目的启动文件位于 lambda/custom/
目录下,通常是 index.js
或 handler.js
。这个文件包含了技能的主要逻辑和处理函数。
// lambda/custom/index.js
const Alexa = require('ask-sdk-core');
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
handle(handlerInput) {
const speakOutput = '欢迎使用 Hello World 技能!';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speakOutput = '你可以问我你好吗?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
LaunchRequestHandler,
HelpIntentHandler
)
.lambda();
3. 项目的配置文件介绍
项目的配置文件是 skill.json
,它定义了技能的基本信息和配置。
{
"manifest": {
"publishingInformation": {
"locales": {
"en-US": {
"name": "Hello World Skill",
"summary": "Sample Short Description",
"description": "Sample Full Description",
"examplePhrases": [
"Alexa, open hello world",
"Alexa, ask hello world to say hello",
"Alexa, ask hello world to introduce itself"
],
"keywords": [
"sample",
"example"
]
}
},
"isAvailableWorldwide": true,
"testingInstructions": "Sample Testing Instructions.",
"category": "EDUCATION_AND_REFERENCE",
"distributionCountries": []
},
"apis": {
"custom": {}
},
"permissions": [],
"privacyAndCompliance": {
"allowsPurchases": false,
"isExportCompliant": true,
"containsAds": false,
"isChildDirected": false,
"usesPersonalInfo": false
}
}
}
- publishingInformation: 包含技能的发布信息,如名称、描述、示例短语等。
- apis: 定义技能使用的 API。
- permissions: 定义技能需要的权限。
- privacyAndCompliance: 包含技能的隐私和合规信息。