OpenClaw 的渠道和工具都是插件系统的一部分。 如果内置插件满足不了你的需求,你可以自己开发。
插件类型
渠道插件(Channel Plugin):
→ 让 OpenClaw 接入新的消息平台
→ 例:为公司内部 IM 系统开发接入插件
→ 实现:onMessage / sendMessage / start / stop
工具插件(Tool Plugin):
→ 给 AI 添加新的工具能力
→ 例:调用公司 ERP 系统的工具
→ 实现:一组 Tool 函数(带参数 Schema)
Provider 插件(Model Provider):
→ 接入新的 AI 模型服务
→ 例:私有部署的模型 API
→ 实现:OpenAI 兼容接口适配
快速开始:安装与管理
bash
# 从 npm 安装官方插件
openclaw plugins install @openclaw/mattermost
openclaw plugins install @openclaw/matrix
# 从本地目录安装(开发中的插件)
openclaw plugins install ./my-custom-plugin
# 列出已安装插件
openclaw plugins list
# 更新所有插件
openclaw plugins update
# 卸载插件
openclaw plugins uninstall @openclaw/mattermost项目结构(渠道插件)
my-channel-plugin/
package.json ← 插件元数据
index.js ← 入口文件
src/
channel.js ← 渠道实现
config-schema.js ← 配置项 Schema
README.md
package.json 规范
json
{
"name": "@yourscope/openclaw-my-channel",
"version": "1.0.0",
"description": "OpenClaw plugin for MyChat",
"main": "index.js",
"openclaw": {
"type": "plugin",
"pluginType": "channel",
"channelId": "mychat",
"capabilities": ["messages", "groups", "reactions"],
"configSchema": "./src/config-schema.js"
},
"peerDependencies": {
"openclaw": ">=1.0.0"
}
}关键字段:
openclaw.type:固定为"plugin"openclaw.pluginType:"channel"/"tool"/"provider"openclaw.channelId:在配置文件中用的渠道名(channels.mychat)openclaw.capabilities:声明支持的能力
渠道插件实现
javascript
// src/channel.js
class MyChatChannel {
constructor(config, gateway) {
this.config = config;
this.gateway = gateway;
this.client = null;
}
// 启动渠道(连接到外部平台)
async start() {
this.client = new MyChatClient({
token: this.config.botToken,
baseUrl: this.config.baseUrl,
});
// 监听消息
this.client.on('message', (msg) => {
this.gateway.ingest({
channel: 'mychat',
chatId: msg.channelId,
userId: msg.userId,
text: msg.text,
timestamp: msg.ts,
});
});
await this.client.connect();
}
// 停止渠道
async stop() {
await this.client?.disconnect();
}
// 发送消息(Gateway 调用此方法回复用户)
async sendMessage(target, content) {
await this.client.send({
channelId: target.chatId,
text: content.text,
});
}
}
module.exports = { MyChatChannel };工具插件实现
javascript
// src/tools.js
// 工具定义(AI 会根据 description 和 parameters 决定何时调用)
const myErpTool = {
name: "query_erp",
description: "查询公司 ERP 系统中的订单、库存或客户信息",
parameters: {
type: "object",
properties: {
queryType: {
type: "string",
enum: ["order", "inventory", "customer"],
description: "查询类型"
},
id: {
type: "string",
description: "订单号/产品ID/客户ID"
}
},
required: ["queryType", "id"]
},
// 实际执行逻辑
async execute({ queryType, id }, context) {
const response = await fetch(
`https://erp.company.com/api/${queryType}/${id}`,
{ headers: { Authorization: `Bearer ${context.config.erpToken}` } }
);
const data = await response.json();
return JSON.stringify(data, null, 2);
}
};
module.exports = { tools: [myErpTool] };配置 Schema
javascript
// src/config-schema.js
module.exports = {
type: "object",
properties: {
botToken: {
type: "string",
description: "Bot Token from MyChat developer portal",
secret: true // 标记为密钥,不会出现在日志中
},
baseUrl: {
type: "string",
description: "MyChat server URL",
default: "https://chat.example.com"
},
dmPolicy: {
type: "string",
enum: ["open", "pairing", "allowlist"],
default: "pairing"
}
},
required: ["botToken", "baseUrl"]
};本地开发与调试
bash
# 安装本地插件(软链接,修改立即生效)
openclaw plugins install ./my-channel-plugin --link
# 带详细日志启动
OPENCLAW_LOG_LEVEL=debug openclaw gateway start
# 查看插件加载状态
openclaw plugins list --verbose发布到 npm
bash
# 登录 npm
npm login
# 测试打包
npm pack --dry-run
# 发布(推荐遵循 @openclaw/ 命名规范)
npm publish --access public发布后其他用户可以直接安装:
bash
openclaw plugins install @yourscope/openclaw-my-channelPlugin Bundles(插件包)
多个插件打包分发:
bash
# 安装一个 Bundle(包含多个相关插件)
openclaw plugins install @openclaw/bundle-enterprise社区插件列表:
docs.openclaw.ai/plugins/community
来源:OpenClaw 官方文档 - docs.openclaw.ai/tools/plugin