
使用NestJS和Prisma构建REST API:身份验证
随着物联网(IoT)和智能家居的快速普及,语音控制已成为人机交互的必然趋势。2025 年,Alexa API 与 Alexa Skills Kit (ASK) 持续进化,不但简化了语音技能(Skill)的创建流程,也大幅提升了技能的智能化、跨设备同步与多模态交互能力。本篇指南将结合权威 2025 年 YouTube 教程《Create Your Own Amazon Alexa Skill from Scratch》(youtube.com),从基础概念、环境搭建、交互模型设计、后端实现、最佳实践、安全策略,到多媒体交互与 AI 集成,全面覆盖 Alexa 语音控制开发 的全链路。
Intent & Slot 设计:
GetWeatherIntent
、ControlLightIntent
;City
、DeviceName
,支持自定义类型与系统类型(AMAZON.DATE
、AMAZON.NUMBER
)。下面以“智能餐厅推荐”(“lunch wizard”)为示例,展示 Alexa 语音技能开发 全流程。
本地开发环境:安装 Node.js (≥14.x)、npm;全局安装 ASK CLI:
npm install -g @ask-cli/ask-cli
ask configure
AWSLambdaFullAccess
、AmazonDynamoDBFullAccess
、AlexaDeveloperConsoleFullAccess
。ask new --skill-name lunch-wizard --template hello-world
cd lunch-wizard
在 models/en-US.json
中修改 Intent 与 Slot:
{
"interactionModel": {
"languageModel": {
"invocationName": "lunch wizard",
"intents": [
{
"name": "GetLunchIntent",
"slots": [
{
"name": "Cuisine",
"type": "CuisineType"
}
],
"samples": [
"what should I eat",
"suggest a {Cuisine} restaurant",
"I want some {Cuisine} food"
]
},
{ "name": "AMAZON.HelpIntent" },
{ "name": "AMAZON.StopIntent" },
{ "name": "AMAZON.CancelIntent" }
],
"types": [
{
"name": "CuisineType",
"values": [
{ "name": { "value": "Italian" } },
{ "name": { "value": "Chinese" } },
{ "name": { "value": "Mexican" } }
]
}
]
}
}
}
在 lambda/index.js
中使用 Alexa SDK for Node.js:
const Alexa = require('ask-sdk-core');
const GetLunchIntentHandler = {
canHandle(handlerInput) {
const req = handlerInput.requestEnvelope.request;
return req.type === 'IntentRequest' && req.intent.name === 'GetLunchIntent';
},
handle(handlerInput) {
const cuisine = handlerInput.requestEnvelope.request.intent.slots.Cuisine.value || 'food';
const recommendations = {
Italian: 'Olive Garden',
Chinese: 'Panda Express',
Mexican: 'Chipotle'
};
const choice = recommendations[cuisine] || 'your favorite spot';
const speechText = 我为你推荐 ${choice},享受美味!
;
return handlerInput.responseBuilder
.speak(speechText)
.reprompt('还想听点别的吗?')
.withSimpleCard('Lunch Wizard', speechText)
.getResponse();
}
};
const HelpHandler = { /* ... */ };
const CancelHandler = { /* ... */ };
const ErrorHandler = { /* ... */ };
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(GetLunchIntentHandler, HelpHandler, CancelHandler)
.addErrorHandlers(ErrorHandler)
.lambda();
ask deploy
在 Developer Console → Test 栏使用 Alexa Simulator,或用真实 Echo 设备说:
> “Alexa, open lunch wizard”
> “Alexa, suggest a Chinese restaurant”
在 AWS CloudWatch 查看日志并调优。
针对需要用户数据的技能,如日历查询、用户档案,需要账号绑定(Account Linking):
handlerInput.requestEnvelope.context.System.user.accessToken
获取令牌。profiles
,区分 dev
、prod
环境,避免测试数据污染生产环境。通过 Smart Home API 控制灯光、插座、恒温器:
在支持屏幕的设备(Echo Show)展示图文:
const aplDocument = require('./documents/lunchApl.json');
...
return handlerInput.responseBuilder
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
document: aplDocument,
datasources: { /* 推荐数据 */ }
})
.speak(speechText)
.getResponse();
免去手动对话流程设计,由 Conversations 模块 生成状态机,支持自然对话与上下文管理。
将未命中 Intent 的请求转发给 GPT 模型:
if (req.intent.name === 'AMAZON.FallbackIntent') {
const userUtterance = handlerInput.requestEnvelope.request.inputTranscript;
const aiResponse = await callChatGPT(userUtterance);
return handlerInput.responseBuilder.speak(aiResponse).getResponse();
}
结合 DynamoDB 存储个性化数据,如用户偏好、历史记录,结合 AI 生成个性化推荐或对话,提升用户黏性。
通过本篇 2025 最新指南,你已掌握从环境搭建、交互模型设计、Lambda 后端实现、Smart Home Skills、多模态 APL、生成式 AI 集成,到测试部署与上线运营的全流程。无论是打造智能家居控制、信息查询机器人,还是导入 ChatGPT 动态对话,你都能快速上手并构建高质量、可扩展的 Alexa 语音技能。
祝你在 Alexa 生态中大展拳脚,开启语音交互与智能家居新时代!
原文引自YouTube视频:https://www.youtube.com/watch?v=lc9A_6Uz_t4