OpenClaw Skills 开发实战:5 个实用案例

10 阅读1分钟

从入门到精通,手把手教你写 Skill

Skill 基础回顾

skill-name/
├── SKILL.md          # 必需:描述文件
├── script.js         # 可选:执行脚本
└── references/       # 可选:参考文档

案例 1:天气查询 Skill

SKILL.md

# 天气查询

## 触发
用户问天气时激活

## 功能
1. 解析城市名
2. 调用天气 API
3. 返回天气信息

## 脚本
weather.js

weather.js

#!/usr/bin/env node
const city = process.argv[2] || "北京";
const url = `https://wttr.in/${city}?format=j1`;

fetch(url)
  .then(r => r.json())
  .then(data => {
    const temp = data.current_condition[0].temp_C;
    const desc = data.current_condition[0].weatherDesc[0].value;
    console.log(`${city}${temp}°C,${desc}`);
  });

案例 2:汇率查询 Skill

SKILL.md

# 汇率查询

## 触发
用户问汇率时激活

## 功能
查询实时汇率

exchange.js

#!/usr/bin/env node
const [from, to] = process.argv.slice(2);
const url = `https://api.exchangerate.host/latest?base=${from}`;

fetch(url)
  .then(r => r.json())
  .then(data => {
    const rate = data.rates[to];
    console.log(`1 ${from} = ${rate} ${to}`);
  });

案例 3:GitHub 仓库分析 Skill

SKILL.md

# GitHub 分析

## 触发
用户给出 GitHub 仓库链接时激活

## 功能
分析仓库信息:star、fork、语言等

github.js

#!/usr/bin/env node
const repo = process.argv[2]; // owner/repo
const url = `https://api.github.com/repos/${repo}`;

fetch(url, {
  headers: { 'User-Agent': 'OpenClaw' }
})
  .then(r => r.json())
  .then(data => {
    console.log(`
📦 ${data.full_name}${data.stargazers_count}
🍴 ${data.forks_count}
🔤 ${data.language}
📝 ${data.description}
    `);
  });

案例 4:待办事项 Skill

SKILL.md

# 待办事项

## 触发
用户说"添加待办"或"查看待办"时激活

## 功能
管理个人待办事项

todo.js

#!/usr/bin/env node
const fs = require('fs');
const file = '/tmp/todos.json';

const [action, ...args] = process.argv.slice(2);

let todos = [];
if (fs.existsSync(file)) {
  todos = JSON.parse(fs.readFileSync(file));
}

switch (action) {
  case 'add':
    todos.push({ text: args.join(' '), done: false });
    fs.writeFileSync(file, JSON.stringify(todos));
    console.log('✅ 已添加');
    break;
  case 'list':
    console.log('📋 待办事项:');
    todos.forEach((t, i) => {
      console.log(`${i + 1}. ${t.done ? '✅' : '⏳'} ${t.text}`);
    });
    break;
  case 'done':
    todos[parseInt(args[0]) - 1].done = true;
    fs.writeFileSync(file, JSON.stringify(todos));
    console.log('✅ 已完成');
    break;
}

案例 5:PDF 总结 Skill

SKILL.md

# PDF 总结

## 触发
用户上传 PDF 文件时激活

## 功能
提取 PDF 文本并生成摘要

pdf.js

#!/usr/bin/env node
const fs = require('fs');
const { PDFLoader } = require('langchain/document_loaders/fs/pdf');

async function summarize(pdfPath) {
  const loader = new PDFLoader(pdfPath);
  const docs = await loader.load();
  const text = docs.map(d => d.pageContent).join('\n');

  // 调用 AI 生成摘要
  const summary = await ai.summarize(text);
  console.log(summary);
}

summarize(process.argv[2]);

高级技巧

1. 环境变量

// 从环境变量获取配置
const apiKey = process.env.WEATHER_API_KEY;

2. 错误处理

try {
  // 执行逻辑
} catch (error) {
  console.error('❌ 错误:', error.message);
  process.exit(1);
}

3. 参数解析

const args = process.argv.slice(2);
const params = {};
args.forEach(arg => {
  const [key, value] = arg.split('=');
  params[key.replace('--', '')] = value;
});

4. 异步处理

async function main() {
  // 异步逻辑
}

main().catch(console.error);

测试 Skill

# 直接测试脚本
node ~/.openclaw/skills/weather/script.js 北京

# 在 OpenClaw 中测试
openclaw skill test weather

发布到 ClawHub

# 1. Fork ClawHub 仓库
git clone https://github.com/openclaw/clawhub

# 2. 复制你的 Skill
cp -r my-skill clawhub/skills/

# 3. 提交 PR
git add .
git commit -m "Add weather skill"
git push

💬 你有什么 Skill 创意?评论区分享!

🎯 需要 Skill 开发服务?微信:yang1002378395