共计 3298 个字符,预计需要花费 9 分钟才能阅读完成。
背景与痛点
Google ADK Skill(Action Development Kit)是用于开发 Google 智能助理语音交互应用的工具包。它让开发者能够创建自定义的语音指令和对话流程,广泛应用于智能家居控制、信息查询、娱乐互动等场景。

对于新手开发者来说,主要面临以下挑战:
- 概念理解困难:意图、场景、实体等核心概念容易混淆
- 环境配置复杂:需要安装多个工具和依赖项
- 调试不便:语音交互的调试比传统应用更困难
- 文档分散:相关文档分布在多个平台
开发环境搭建
-
安装必要工具
-
JDK 8 或更高版本
- Android Studio 4.1+
- Google Cloud SDK
-
Node.js 12+
-
配置 Google Cloud 项目
-
登录 Google Cloud Console
- 创建新项目并启用 Actions API
-
设置 OAuth 2.0 凭证
-
安装 ADK 工具包
npm install -g @assistant/gactions
- 验证安装
gactions --version
核心实现细节
- 创建项目结构
my-adk-skill/
├── actions.json
├── webhook/
│ ├── package.json
│ ├── index.js
│ └── ...
└── ...
- 定义意图
在 actions.json 中定义基本意图:
{
"actions": [{
"name": "MAIN",
"intent": {"name": "actions.intent.MAIN"}
}],
"conversations": {
"MyFirstSkill": {
"name": "My First Skill",
"url": "https://your-webhook-url.com"
}
}
}
- 实现 Webhook
创建一个简单的 Node.js webhook 服务:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.post('/', (req, res) => {
const intent = req.body.intent;
let response = {
prompt: {
override: false,
firstSimple: {
speech: 'Hello! Welcome to my first ADK Skill.',
text: 'Hello! Welcome to my first ADK Skill.'
}
}
};
res.json(response);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {console.log(`Server running on port ${PORT}`);
});
- 部署和测试
gactions update --action_package actions.json --project your-project-id
gactions test --action_package actions.json --project your-project-id
代码示例详解
下面是一个完整的 ADK Skill 示例,实现了一个简单的天气查询功能:
// 天气查询 webhook 实现
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// 模拟天气数据
const weatherData = {'new york': { temp: '72°F', condition: 'sunny'},
'san francisco': {temp: '65°F', condition: 'cloudy'},
'seattle': {temp: '58°F', condition: 'rainy'}
};
app.use(bodyParser.json());
app.post('/', (req, res) => {
const intent = req.body.intent.name;
let response;
// 处理主意图
if (intent === 'actions.intent.MAIN') {
response = {
prompt: {
override: false,
firstSimple: {
speech: 'Welcome to Weather Assistant. Ask me about the weather in any city.',
text: 'Welcome to Weather Assistant. Ask me about the weather in any city.'
}
}
};
}
// 处理天气查询意图
else if (intent === 'actions.intent.GET_WEATHER') {
const city = req.body.scene.slots.city.value;
const weather = weatherData[city.toLowerCase()] ||
{temp: 'unknown', condition: 'unknown'};
response = {
prompt: {
override: false,
firstSimple: {speech: `The weather in ${city} is ${weather.temp} and ${weather.condition}.`,
text: `${city}: ${weather.temp}, ${weather.condition}`
}
}
};
}
// 默认回复
else {
response = {
prompt: {
override: false,
firstSimple: {
speech: 'Sorry, I didn\'t understand that.',
text: 'Sorry, I didn\'t understand that.'
}
}
};
}
res.json(response);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {console.log(`Weather Assistant running on port ${PORT}`);
});
测试与调试
-
使用模拟器测试
-
打开 Google Actions Console
- 选择你的项目
- 点击 ”Test” 标签页
-
输入语音或文本指令进行测试
-
调试技巧
-
使用
console.log输出请求和响应数据 - 检查 Google Cloud Logging 中的日志
- 使用 Postman 测试 webhook 端点
-
关注错误代码和状态信息
-
常见测试场景
-
测试不同语音输入方式
- 测试异常情况处理
- 测试多轮对话流程
- 测试设备兼容性
避坑指南
-
常见错误与解决方案
-
错误:”Sorry, something went wrong”
原因:Webhook 未响应或返回无效格式
解决:检查 webhook URL 和响应格式 -
错误:”I can’t help with that yet”
原因:意图未正确定义
解决 :检查actions.json中的意图配置 -
错误:”There was an error talking to your Action”
原因:权限或认证问题
解决:检查 Google Cloud 项目设置和 OAuth 配置 -
性能优化建议
-
减少 webhook 响应时间
- 使用缓存提高性能
- 合理设计对话流程
- 优化错误处理机制
进阶建议
-
学习资源
-
Google Actions 官方文档
- ADK GitHub 仓库示例代码
- Google Developers 社区
-
Coursera 上的 Google Assistant 开发课程
-
进阶方向
-
集成第三方 API 服务
- 实现复杂的多轮对话
- 添加个性化用户体验
- 支持多语言和多地区
-
使用机器学习优化语音识别
-
最佳实践
-
设计简洁自然的对话流程
- 提供清晰明确的错误提示
- 考虑不同设备的使用场景
- 定期更新和维护你的 Skill
总结
通过本文,你已经掌握了 Google ADK Skill 开发的基本流程。从环境搭建到基础实现,再到测试调试和常见问题解决,这些知识将帮助你快速入门语音交互应用开发。记住,实践是最好的学习方式,建议从简单的 Skill 开始,逐步增加复杂度。随着经验的积累,你将能够创建出更加智能和丰富的语音交互体验。
