第8讲:OpenClaw 平台深度解析

第8讲:OpenClaw 平台深度解析

掌握开源 Skill 平台 OpenClaw,实现更灵活的 Skill 开发。

一、OpenClaw 简介

1.1 什么是 OpenClaw?

OpenClaw 是一个开源的 AI Skill 开发平台

  • ✅ 完全开源,代码可控
  • ✅ Python 原生支持
  • ✅ 本地运行,数据安全
  • ✅ 丰富的 ClawHub 生态

1.2 与 Coze 的区别

特性CozeOpenClaw
部署方式云端本地/云端
代码控制受限完全控制
数据安全云端存储本地存储
开发方式可视化+代码纯代码
生态规模中等

二、环境搭建

2.1 安装 OpenClaw

1
2
3
4
5
6
7
# 使用 pip 安装
pip install openclaw

# 或者从源码安装
git clone https://github.com/openclaw/openclaw.git
cd openclaw
pip install -e .

2.2 初始化项目

1
2
3
4
5
6
7
8
9
10
11
12
# 创建新项目
openclaw init my-skill

# 进入项目
cd my-skill

# 项目结构
my-skill/
├── skill.yaml # Skill 配置
├── main.py # 主入口
├── requirements.txt # 依赖
└── tests/ # 测试

2.3 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# skill.yaml
name: excel-merge-skill
description: 合并 Excel 文件的 Skill
version: 1.0.0
author: your-name

entry: main.py

intents:
- name: merge_excel
description: 合并多个 Excel 文件
parameters:
- name: files
type: array
required: true
- name: mode
type: string
enum: [vertical, horizontal]
default: vertical

三、开发第一个 OpenClaw Skill

3.1 主程序结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# main.py
from openclaw import Skill, Intent
import pandas as pd

class ExcelMergeSkill(Skill):
"""Excel 合并 Skill"""

def __init__(self):
super().__init__()
self.register_intent("merge_excel", self.merge_excel)

def merge_excel(self, files, mode="vertical"):
"""合并 Excel 文件"""
# 读取所有文件
dfs = [pd.read_excel(f) for f in files]

# 合并
if mode == "vertical":
result = pd.concat(dfs, ignore_index=True)
else:
result = pd.concat(dfs, axis=1)

# 保存
output_path = "merged.xlsx"
result.to_excel(output_path, index=False)

return {
"success": True,
"file": output_path,
"rows": len(result)
}

# 启动
if __name__ == "__main__":
skill = ExcelMergeSkill()
skill.run()

3.2 运行 Skill

1
2
3
4
5
6
7
8
# 本地运行
openclaw run

# 调试模式
openclaw run --debug

# 指定端口
openclaw run --port 8080

四、ClawHub 生态

4.1 什么是 ClawHub?

ClawHub 是 OpenClaw 的 Skill 市场:

  • 1700+ 开源 Skills
  • 覆盖各种办公场景
  • 可直接安装使用

4.2 使用 ClawHub

1
2
3
4
5
6
7
8
9
10
11
# 搜索 Skill
openclaw search excel

# 安装 Skill
openclaw install excel-processor

# 查看已安装
openclaw list

# 更新 Skill
openclaw update excel-processor

4.3 发布到 ClawHub

1
2
3
4
5
# 打包 Skill
openclaw package

# 发布(需注册开发者账号)
openclaw publish

五、高级功能

5.1 自定义工具

1
2
3
4
5
6
7
8
9
10
11
12
13
from openclaw import Tool

class ExcelTool(Tool):
"""自定义 Excel 工具"""

def read(self, file_path):
return pd.read_excel(file_path)

def merge(self, files, mode):
dfs = [self.read(f) for f in files]
if mode == "vertical":
return pd.concat(dfs, ignore_index=True)
return pd.concat(dfs, axis=1)

5.2 多轮对话

1
2
3
4
5
6
7
8
9
10
11
12
def handle_conversation(self, user_input, context):
"""处理多轮对话"""
if context.get("step") == "waiting_files":
# 处理文件上传
context["files"] = user_input
context["step"] = "waiting_mode"
return "请选择合并方式:1.按行 2.按列"

elif context.get("step") == "waiting_mode":
# 处理模式选择
mode = "vertical" if user_input == "1" else "horizontal"
return self.merge_excel(context["files"], mode)

六、下节预告

第9讲:OpenClaw 实战:开发数据处理 Skill

我们将:

  • 开发一个完整的数据处理 Skill
  • 集成 Python 数据分析库
  • 发布到 ClawHub

加入学习群

OpenClaw 使用有疑问?欢迎加入交流群:

👉 加入AI编程学习交流群

点击加入


本讲是《Skills 从入门到实践》系列课程的第8讲。

🎓 AI 编程实战课程

想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!