OpenClaw 的强大之处在于其高度可扩展性。通过编写自定义 Skills,你可以将任何业务逻辑接入 OpenClaw,打造完全符合你需求的 AI 助手。本文将详细介绍如何开发自定义 Skills。
OpenClaw 的 Skills 本质上是一个个独立的模块,每个 Skill 包含:
建议使用 4核8G 配置的开发服务器来开发和测试 Skills:
# 安装 Node.js(用于编写 JavaScript Skills)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# 安装 Python(用于编写 Python Skills)
sudo apt-get install -y python3 python3-pip
my-custom-skill/
├── manifest.json
├── index.js
└── README.md
{
"name": "天气查询",
"version": "1.0.0",
"description": "查询指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "要查询的城市名称"
}
},
"required": ["city"]
},
"examples": [
"北京天气怎么样?",
"查询上海的天气"
]
}
const axios = require('axios');
module.exports = async function (params, context) {
const { city } = params;
try {
// 调用天气 API
const response = await axios.get(
`https://api.weatherapi.com/v1/current.json`,
{
params: {
key: process.env.WEATHER_API_KEY,
q: city
}
}
);
const data = response.data;
return {
success: true,
result: `${data.location.name}当前天气:${data.current.condition.text},温度${data.current.temp_c}°C,湿度${data.current.humidity}%`
};
} catch (error) {
return {
success: false,
error: error.message
};
}
};
将开发好的 Skill 放到 OpenClaw 的 skills 目录下:
cp -r my-custom-skill /path/to/openclaw/skills/
重启 OpenClaw 服务:
docker-compose restart openclaw
通过 OpenClaw 的对话界面测试:
用户:查询北京天气
AI:[调用天气查询 Skill] 北京当前天气:晴,温度22°C,湿度45%
对于需要语义理解的 Skill,可以接入向量数据库实现相似匹配:
const { Chroma } = require('chromadb');
module.exports = async function (params, context) {
const chroma = new Chroma({ path: 'http://localhost:8000' });
// 查询相似文档
const results = await chroma.query({
collectionName: 'knowledge_base',
queryEmbeddings: [context.embedding],
nResults: 3
});
return { context: results.documents };
};
Skill 可以调用任意外部 API,实现丰富的集成能力:
module.exports = async function (params, context) {
// 调用企业微信机器人发送消息
await axios.post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send', {
msgtype: 'text',
text: {
content: `收到新任务:${params.task}`
}
}, {
params: { key: process.env.WEBHOOK_KEY }
});
return { success: true };
};
对于耗时较长的任务,支持异步执行:
module.exports = async function (params, context) {
// 立即返回任务 ID
return {
async: true,
taskId: generateTaskId(),
message: '任务已提交,请稍候查询结果'
};
};
// 任务完成后通过 webhook 回调
module.exports.onComplete = async function (taskId, result) {
// 发送结果通知
await sendNotification(result);
};
# 直接运行 Skill
node -e "
const skill = require('./index.js');
skill({ city: '北京' }, {}).then(console.log);
"
console.log('Debug info:', params);
console.error('Error:', error);
查看日志:
docker logs openclaw | grep "Debug info"
始终做好异常捕获:
try {
// 业务逻辑
} catch (error) {
return {
success: false,
error: '服务暂时不可用,请稍后重试',
details: process.env.NODE_ENV === 'development' ? error.message : undefined
};
}
使用 schema 库验证输入参数:
const Ajv = require('ajv');
const ajv = new Ajv();
const validate = ajv.compile({
type: 'object',
properties: {
city: { type: 'string', minLength: 1 }
},
required: ['city']
});
if (!validate(params)) {
return { success: false, error: validate.errors };
}
使用环境变量存储敏感配置,不要硬编码:
// 错误 ❌
const apiKey = 'sk-xxx';
// 正确 ✓
const apiKey = process.env.API_KEY;
编写自定义 Skills 是 OpenClaw 扩展能力的核心。通过本文的学习,你应该掌握了 Skill 开发的基本流程和高级技巧。现在就可以开始开发属于你自己的 Skills,让 OpenClaw 真正成为你的专属 AI 助手。
建议先从简单的功能开始,逐步迭代完善。如果遇到问题,OpenClaw 社区是获取帮助的好地方。