
构建基于Claude MCP的天气查询智能体 | 实战落地示例
随着智能音箱和语音助手在全球范围内的普及,Alexa 已成为家庭与企业实现语音交互的主流平台。本文将从环境配置入手,手把手指导你用 Alexa Skills Kit (ASK) 构建第一个语音技能,涵盖Interaction Model设计、ASK CLI 部署、Node.js Lambda 编写、语音交互测试与多轮对话管理。无论你是否具备编程经验,都能在短时间内掌握Alexa API 开发的核心流程。
Amazon Developer Console
AWS 账户与 IAM 权限
AWSLambdaFullAccess
、CloudWatchLogsFullAccess
权限。安装 ASK CLI
npm install -g @ask-cli/ask-cli
ask configure
ask configure
会引导你完成与 Amazon Developer 和 AWS IAM 的授权关联。本地开发依赖
ask init
或 ask new
创建项目结构。ask new --skill-name hello-alexa --template hello-world
cd hello-alexa
这将生成包含 models/en-US.json
、lambda/index.js
及部署脚本的项目模版。
在 models/en-US.json
中,你可以看到:
{
"interactionModel": {
"languageModel": {
"invocationName": "hello alexa",
"intents": [
{
"name": "HelloWorldIntent",
"samples": ["hello", "say hello", "greet me"]
},
{ "name": "AMAZON.HelpIntent" },
{ "name": "AMAZON.CancelIntent" },
{ "name": "AMAZON.StopIntent" }
]
}
}
}
HelloWorldIntent
与内置意图。在 lambda/index.js
中使用 ask-sdk-core
:
const Alexa = require('ask-sdk-core');
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
},
handle(handlerInput) {
const speechText = 'Hello from your first Alexa skill!';
return handlerInput.responseBuilder
.speak(speechText)
.getResponse();
}
};
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(HelloWorldIntentHandler)
.lambda();
responseBuilder.speak()
用于设置语音回复。ask deploy
创建一个查询城市天气的技能,定义 GetWeatherIntent
:
{
"name": "GetWeatherIntent",
"slots": [
{
"name": "City",
"type": "AMAZON.US_CITY"
}
],
"samples": ["what's the weather in {City}", "weather in {City}"]
}
在 Lambda 中读取槽位值:
const GetWeatherIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'GetWeatherIntent';
},
async handle(handlerInput) {
const city = handlerInput.requestEnvelope.request.intent.slots.City.value;
const weather = await fetchWeather(city); // 调用外部 API
return handlerInput.responseBuilder
.speak(`The weather in ${city} is ${weather}.`)
.getResponse();
}
};
handlerInput.requestEnvelope.request.intent.slots.City.value
获取用户提供的城市名称。为应对缺少槽位的情况,可以启用 Dialog Management:
models/en-US.json
中为 GetWeatherIntent
添加 dialog
配置。Alexa.SkillBuilders.custom().addRequestHandlers()
中的 Delegate
指令进行对话委派。if (handlerInput.requestEnvelope.request.dialogState !== 'COMPLETED') {
return handlerInput.responseBuilder
.addDelegateDirective()
.getResponse();
}
handlerInput
内容,验证意图命中与参数传递。ASK CLI Profiles
ask deploy --profile prod
dev
、prod
环境。技能发布
在支持屏幕的 Echo 设备上,使用 APL 展示图文、按钮等:
handlerInput.responseBuilder.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
document: require('./documents/template.json'),
datasources: { ... }
});
entity resolution
精准匹配槽位值。视频教程:Zero to Hero 系列,涵盖从基础到高级的 Alexa Skills 开发。
官方文档:
开源示例:GitHub 上搜索 “alexa-skills-kit” 获取社区实践案例。
从注册账号、环境配置,到 Interaction Model 设计、Node.js Lambda 编写,再到测试调优与技能上线,本文覆盖了“零基础入门 Alexa API 开发”的完整路径。依托 ASK CLI、Alexa Simulator、CloudWatch 日志等工具,你可以快速迭代语音技能,逐步扩展至智能家居、多模态交互与第三方 API 集成。希望你通过本文深度掌握 Alexa 技能开发,并在语音交互的浪潮中抢占先机!