OpenClaw 技能开发指南
开发自定义技能,扩展 AI 能力。
🎯 什么是技能?
技能是 OpenClaw 中 AI 可以调用的工具和能力。
技能类型
| 类型 | 说明 | 例子 |
|---|---|---|
| 查询类 | 获取信息 | 天气查询、新闻获取 |
| 操作类 | 执行动作 | 发送邮件、创建文档 |
| 计算类 | 数据处理 | 数据分析、图表生成 |
| 集成类 | 第三方服务 | GitHub、Notion 集成 |
📋 技能结构
基本结构
skills/
└── my-skill/
├── SKILL.md # 技能说明
├── index.js # 技能实现
└── config.json # 配置文件SKILL.md 模板
markdown
# Skill Name
技能描述。
## Usagecommand [arguments]
## Examplesexample 1 example 2
## Configuration
需要的配置项。实战:开发天气查询技能
步骤 1:创建技能目录
bash
mkdir -p ~/.openclaw/skills/weather
cd ~/.openclaw/skills/weather步骤 2:编写技能说明
创建 SKILL.md:
markdown
# Weather Skill
查询天气预报。
## Usage天气 [城市名] 天气 [城市名] [天数]
## Examples天气 北京 天气 上海 3
## Configuration
需要配置天气 API Key。步骤 3:实现技能逻辑
创建 index.js:
javascript
const axios = require('axios');
module.exports = {
name: 'weather',
description: '查询天气预报',
async execute(args, context) {
const city = args[0] || '北京';
const days = args[1] || 1;
// 调用天气 API
const response = await axios.get(
`https://api.weather.com/v1/forecast?city=${city}&days=${days}`
);
// 格式化结果
const forecast = response.data;
return formatWeather(forecast);
}
};
function formatWeather(data) {
return `
🌤️ ${data.city} 天气预报
${data.forecasts.map(f =>
`${f.date}: ${f.condition}, ${f.temp}`
).join('\n')}
`;
}步骤 4:配置 API Key
创建 config.json:
json
{
"apiKey": "your-api-key",
"baseUrl": "https://api.weather.com",
"timeout": 5000
}步骤 5:启用技能
bash
openclaw skills enable weather技能开发最佳实践
1. 错误处理
javascript
async execute(args, context) {
try {
return await doSomething(args);
} catch (error) {
console.error('技能执行失败:', error);
return '抱歉,技能执行失败,请稍后重试。';
}
}2. 参数验证
javascript
async execute(args, context) {
// 验证参数
if (!args[0]) {
return '请提供城市名,例如:天气 北京';
}
if (args[1] && isNaN(args[1])) {
return '天数必须是数字';
}
// 继续执行...
}3. 缓存结果
javascript
const cache = new Map();
async execute(args, context) {
const key = args.join('-');
// 检查缓存
if (cache.has(key)) {
return cache.get(key);
}
// 执行查询
const result = await query(args);
// 保存缓存(5 分钟)
cache.set(key, result);
setTimeout(() => cache.delete(key), 5 * 60 * 1000);
return result;
}4. 日志记录
javascript
async execute(args, context) {
console.log(`[Weather] 查询城市:${args[0]}`);
const result = await query(args);
console.log(`[Weather] 查询完成`);
return result;
}技能调试
本地测试
javascript
// test.js
const skill = require('./index');
async function test() {
const result = await skill.execute(['北京'], {});
console.log(result);
}
test();日志查看
bash
# 查看技能日志
openclaw logs --grep weather
# 实时日志
openclaw logs --follow技能发布
1. 打包技能
bash
cd ~/.openclaw/skills/weather
zip -r weather.zip .2. 分享到社区
bash
# 提交到 ClawHub
openclaw skills publish weather.zip3. 文档说明
markdown
# 安装
```bash
openclaw skills install weather使用
天气 北京
---
## 常用技能示例
### 1. 新闻查询技能
```javascript
module.exports = {
name: 'news',
async execute(args) {
const category = args[0] || 'top';
const news = await fetchNews(category);
return formatNews(news);
}
};2. 翻译技能
javascript
module.exports = {
name: 'translate',
async execute(args) {
const text = args.slice(1).join(' ');
const targetLang = args[0] || 'en';
return await translate(text, targetLang);
}
};3. 待办事项技能
javascript
const todos = [];
module.exports = {
name: 'todo',
async execute(args, context) {
const action = args[0];
const task = args.slice(1).join(' ');
if (action === 'add') {
todos.push({ task, done: false });
return '已添加待办事项';
}
if (action === 'list') {
return formatTodos(todos);
}
}
};📚 相关资源
提示
从简单技能开始,逐步扩展功能!
🟢🐉 泡泡龙