进阶 09
⏱️ 40分钟
自定义技能开发
开发自己的技能,扩展 OpenClaw 功能。
学习目标
- 了解技能结构
- 创建简单技能
- 使用 API 和工具
技能目录结构
~/.openclaw/skills/
└── my-skill/
├── SKILL.md # 技能描述
├── main.js # 执行脚本
└── package.json # 依赖(可选)
创建第一个技能
步骤 1:创建目录
mkdir -p ~/.openclaw/skills/hello-skill
cd ~/.openclaw/skills/hello-skill
步骤 2:编写 SKILL.md
---
name: hello-skill
description: 打招呼技能
version: 1.0.0
---
# 使用示例
openclaw skill hello-skill --name "World"
步骤 3:编写执行脚本
// main.js
module.exports = {
async execute(args, context) {
const name = args.name || "OpenClaw";
return {
message: `Hello, ${name}!`
};
}
};
步骤 4:使用技能
openclaw skill hello-skill --name "Alice"
# 输出: Hello, Alice!
进阶:使用工具 API
// file-processor.js
module.exports = {
async execute(args, context) {
const { file } = args;
const { read } = context.tools.file;
// 读取文件
const content = await read(file);
// 处理内容
const lines = content.split('\n').length;
return {
lines: lines,
chars: content.length
};
}
};