创建您的第一个技能
为您的代理赋予新能力。我们将构建一个“随机名言生成器”来演示技能结构。
我们将构建什么
我们将创建一个 随机名言技能,它可以:
- 在被要求时提供励志名言
- 演示基础技能结构
- 展示如何使用内置工具
- 在以后可以轻松扩展
这是一个简单的例子,但它涵盖了构建更复杂技能所需的所有基础知识。
前提条件
在开始之前,请确保您拥有:
- 已安装并运行 OpenClaw - 请先完成入门指南教程
- 已连接渠道 - 遵循渠道设置教程
- 基础文本编辑器 - 任何编辑器都可以 (nano, VS Code 等)
- 访问您的工作区 - 默认位置是
~/.openclaw
第 1 步:规划您的技能
在编写代码之前,让我们规划一下我们的技能将做什么:
技能目的
随机名言技能将:
- 当用户寻求名言、灵感或动力时做出回应
- 从预定义列表中提供随机名言
- 足够简单易懂,但具有可扩展性
我们需要什么工具?
对于这个技能,我们将使用 OpenClaw 的内置能力。我们不需要外部 API - 我们将名言存储在技能文件本身中。这有助于简化学习过程。
Tutorial.alert.tip
第 2 步:创建技能目录
技能存储在您的工作区中。让我们创建目录结构:
mkdir -p ~/.openclaw/skills/random-quote
Directory structure: Skills are organized as ~/.openclaw/skills/<skill-name>/. The skill name should be lowercase with hyphens (kebab-case).
Tutorial.alert.success
~/.openclaw,但您可以在配置中检查或更改它。有关详细信息,请参阅配置参考。第 3 步:创建技能文件
每个技能都需要一个 SKILL.md 文件。这是一个定义您技能的 Markdown 文件。
第 3.1 步:创建 SKILL.md
在您的技能目录中创建技能文件:
touch ~/.openclaw/skills/random-quote/SKILL.md # or open in your editor: code ~/.openclaw/skills/random-quote/SKILL.md
第 3.2 步:编写技能内容
打开 SKILL.md 并添加以下内容:
# Random Quote Skill ## Description Provides inspirational and motivational quotes when users ask for quotes, inspiration, or motivation. This skill demonstrates basic skill structure and can be extended with more quotes or API integration. ## Tools This skill uses OpenClaw's built-in capabilities to select and return quotes. No external tools required. ## Instructions When the user asks for: - A quote - Inspiration - Motivation - A random quote - Something uplifting Respond with a random quote from the following list, formatted nicely: 1. "The only way to do great work is to love what you do." - Steve Jobs 2. "Innovation distinguishes between a leader and a follower." - Steve Jobs 3. "Life is what happens to you while you're busy making other plans." - John Lennon 4. "The future belongs to those who believe in the beauty of their dreams." - Eleanor Roosevelt 5. "It is during our darkest moments that we must focus to see the light." - Aristotle 6. "The only impossible journey is the one you never begin." - Tony Robbins 7. "In the middle of difficulty lies opportunity." - Albert Einstein 8. "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill Vary your responses and make them feel natural. You can add context or commentary to the quote if appropriate. ## Examples - User: "Give me a quote" Response: "Here's an inspiring quote for you: 'The only way to do great work is to love what you do.' - Steve Jobs" - User: "I need some motivation" Response: "Here's something to inspire you: 'The future belongs to those who believe in the beauty of their dreams.' - Eleanor Roosevelt" - User: "Random quote please" Response: "Here's a quote: 'In the middle of difficulty lies opportunity.' - Albert Einstein"
添加此内容后保存文件。
了解技能结构
让我们分解一下每个部分的作用:
- Description (描述) - 您的技能是做什么的 (OpenClaw 使用它来了解何时使用该技能)
- Tools (工具) - 您的技能使用哪些能力 (可以是内置的或外部 API)
- Instructions (指令) - OpenClaw 应该如何使用此技能 (触发器、行为、示例)
- Examples (示例) - 技能应该如何工作的具体示例
第 4 步:启用您的技能
现在我们需要告诉 OpenClaw 您的新技能。有两种方法可以做到:
方法 A:询问 OpenClaw (最简单)
只需通过您连接的渠道要求 OpenClaw 启用您的技能:
- “启用 random-quote 技能”
- “使用 random-quote 技能”
- “激活 random-quote”
OpenClaw 将自动检测并启用该技能!
方法 B:手动配置
或者,将其添加到您的配置文件中:
{
"agent": {
"model": "anthropic/claude-opus-4-5"
},
"skills": {
"enabled": ["random-quote"]
}
}添加到配置后,重启您的网关以使更改生效。
第 5 步:测试您的技能
现在让我们测试您的技能是否有效!
第 5.1 步:发送测试消息
通过您连接的渠道 (Telegram, WhatsApp 等),尝试询问:
- “给我一句名言”
- “我需要灵感”
- “请随机来句名言”
- “激励我一下”
第 5.2 步:验证响应
OpenClaw 应该用您技能中的名言做出响应。如果它做到了,恭喜——您的技能生效了! 🎉
Tutorial.alert.success
- OpenClaw 用您列表中的名言做出响应
- 名言在不同请求之间会有变化
- 响应自然且符合上下文
第 6 步:改进您的技能 (可选)
现在您的基础技能已经可以工作了,这里有一些增强它的方法:
- 1添加更多名言在指令 (Instructions) 部分扩展您的名言列表。添加您最喜欢的作家、演讲者或来源的名言。
- 2添加类别您可以扩展技能以支持类别 (例如,“给我一句励志名言”,“关于成功的名言”)。更新指令部分以处理这些变化。
- 3整合外部 API对于更高级的版本,您可以使用名言 API,添加一个从网络获取名言的工具,或者将名言存储在数据库中。
故障排除
技能无效
如果 OpenClaw 没有使用您的技能:
- 检查文件位置:确保
SKILL.md位于~/.openclaw/skills/random-quote/SKILL.md - 验证技能是否已启用:检查您的配置或要求 OpenClaw 列出已启用的技能
- 检查文件格式:确保它是有效的 Markdown 且结构正确
- 重启网关:有时需要重启才能加载新技能
技能未触发
如果在您询问名言时技能没有激活:
- 查看您的指令部分 - 触发器是否清晰?
- 尝试更明确的请求,如“使用 random-quote 技能”
- 检查描述 (Description) 是否提到了名言/灵感
响应错误
如果 OpenClaw 做出响应但不是名言:
- 让您的指令更加具体
- 添加更多示例以澄清预期行为
- 确保技能描述清晰地阐明其目的
后续步骤
恭喜您创建了第一个技能!接下来可以探索以下内容:
高级技能模式
一旦您熟悉了基础技能,请探索以下高级模式:
- API 整合 - 连接到外部服务和 API
- 文件操作 - 在系统上读取和写入文件
- 数据库访问 - 存储和检索数据
- 复杂工具 - 创建具有多个参数的自定义工具
- 条件逻辑 - 根据上下文进行调整的技能
浏览 OpenClaw Hub 技能注册表,查看社区创建的高级技能示例。