微信截圖_17363901826052.png)
API 設(shè)計原理:從理論到實踐
Intent & Slot 設(shè)計:
GetWeatherIntent
、ControlLightIntent
;City
、DeviceName
,支持自定義類型與系統(tǒng)類型(AMAZON.DATE
、AMAZON.NUMBER
)。下面以“智能餐廳推薦”(“l(fā)unch wizard”)為示例,展示 Alexa 語音技能開發(fā) 全流程。
本地開發(fā)環(huán)境:安裝 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 設(shè)備說:
> “Alexa, open lunch wizard”
> “Alexa, suggest a Chinese restaurant”
在 AWS CloudWatch 查看日志并調(diào)優(yōu)。
針對需要用戶數(shù)據(jù)的技能,如日歷查詢、用戶檔案,需要賬號綁定(Account Linking):
handlerInput.requestEnvelope.context.System.user.accessToken
獲取令牌。profiles
,區(qū)分 dev
、prod
環(huán)境,避免測試數(shù)據(jù)污染生產(chǎn)環(huán)境。通過 Smart Home API 控制燈光、插座、恒溫器:
在支持屏幕的設(shè)備(Echo Show)展示圖文:
const aplDocument = require('./documents/lunchApl.json');
...
return handlerInput.responseBuilder
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
document: aplDocument,
datasources: { /* 推薦數(shù)據(jù) */ }
})
.speak(speechText)
.getResponse();
免去手動對話流程設(shè)計,由 Conversations 模塊 生成狀態(tài)機,支持自然對話與上下文管理。
將未命中 Intent 的請求轉(zhuǎn)發(fā)給 GPT 模型:
if (req.intent.name === 'AMAZON.FallbackIntent') {
const userUtterance = handlerInput.requestEnvelope.request.inputTranscript;
const aiResponse = await callChatGPT(userUtterance);
return handlerInput.responseBuilder.speak(aiResponse).getResponse();
}
結(jié)合 DynamoDB 存儲個性化數(shù)據(jù),如用戶偏好、歷史記錄,結(jié)合 AI 生成個性化推薦或?qū)υ挘嵘脩麴ば浴?/p>
通過本篇 2025 最新指南,你已掌握從環(huán)境搭建、交互模型設(shè)計、Lambda 后端實現(xiàn)、Smart Home Skills、多模態(tài) APL、生成式 AI 集成,到測試部署與上線運營的全流程。無論是打造智能家居控制、信息查詢機器人,還是導(dǎo)入 ChatGPT 動態(tài)對話,你都能快速上手并構(gòu)建高質(zhì)量、可擴展的 Alexa 語音技能。
祝你在 Alexa 生態(tài)中大展拳腳,開啟語音交互與智能家居新時代!
原文引自YouTube視頻:https://www.youtube.com/watch?v=lc9A_6Uz_t4