
API 設計原理:從理論到實踐
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