🤖 OpenClaw + Coze 集成指南
Coze是字节跳动的AI Bot平台,支持抖音、飞书、微信等渠道。把它和OpenClaw连接,让你的Agent触达更多用户。
🎯 为什么集成 Coze?
- 多渠道触达:抖音、飞书、微信、网站、API
- 可视化构建:拖拽式Bot设计
- 插件生态:丰富的官方和社区插件
- 免费额度:个人开发者友好
🏗️ 集成架构
┌─────────────────────────────────────────┐
│ Coze Platform │
│ ┌─────────────────────────────────┐ │
│ │ Bot Builder (可视化) │ │
│ │ 抖音 │ 飞书 │ 微信 │ 网站 │ │
│ └────────────┬────────────────────┘ │
│ │ Webhook / API │
└───────────────┼─────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ OpenClaw Gateway │
│ ┌─────────────────────────────────┐ │
│ │ Webhook接收器 │ │
│ │ 处理Coze发来的消息 │ │
│ └────────────┬────────────────────┘ │
│ │ │
│ ┌────────────┴────────────────────┐ │
│ │ Agent处理逻辑 │ │
│ │ web-search │ file │ browser │ │
│ └────────────┬────────────────────┘ │
│ │ │
│ ┌────────────┴────────────────────┐ │
│ │ 回复到Coze │ │
│ └─────────────────────────────────┘ │
└─────────────────────────────────────────┘
🚀 快速开始
步骤1:创建Coze Bot
# 1. 访问 coze.cn 注册账号
# 2. 创建新Bot
# 3. 配置"插件" → 添加"自定义API"插件
# 4. 设置Webhook URL指向你的OpenClaw Gateway
步骤2:配置OpenClaw Webhook
# webhook_server.py
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
OPENCLAW_GATEWAY = "http://localhost:3000"
@app.route('/coze/webhook', methods=['POST'])
def coze_webhook():
data = request.json
# 解析Coze消息
user_message = data.get('message', '')
user_id = data.get('user_id', '')
conversation_id = data.get('conversation_id', '')
# 调用OpenClaw处理
response = requests.post(
f"{OPENCLAW_GATEWAY}/api/agent/run",
json={
"task": user_message,
"context": {
"source": "coze",
"user_id": user_id,
"conversation_id": conversation_id
}
}
)
result = response.json()
# 返回给Coze
return jsonify({
"reply": result.get("output", "处理失败"),
"conversation_id": conversation_id
})
if __name__ == "__main__":
app.run(port=5000)
步骤3:配置Coze插件
# 在Coze Bot配置中:
# 1. 添加"自定义API"插件
# 2. 配置:
# - API名称: OpenClaw Agent
# - 请求方式: POST
# - URL: https://your-server.com/coze/webhook
# - 请求体: {"message": "{{user_input}}"}
# - 响应解析: $.reply
# 3. 在Bot的"人设与回复逻辑"中添加:
# "当用户提问时,调用OpenClaw Agent插件获取答案"
💡 高级用法
1. 多Bot协作
# 不同的Coze Bot调用不同的OpenClaw Agent
# Bot1: 研究助手
# → OpenClaw Agent: web-search + data-analysis
# Bot2: 内容创作
# → OpenClaw Agent: writing + image-gen
# Bot3: 客服助手
# → OpenClaw Agent: knowledge-base + crm
2. 记忆保持
# 在OpenClaw中保持Coze用户的对话记忆
@app.route('/coze/webhook', methods=['POST'])
def coze_webhook():
data = request.json
user_id = data.get('user_id', '')
# 使用OpenClaw的Session管理
session_key = f"coze-user-{user_id}"
response = requests.post(
f"{OPENCLAW_GATEWAY}/api/agent/run",
json={
"task": data['message'],
"sessionKey": session_key # 保持对话连续性
}
)
return jsonify({"reply": response.json()["output"]})
3. 工具扩展
# OpenClaw可以调用Coze的工具
# 在OpenClaw中添加Coze插件作为MCP Server
{
"mcpServers": {
"coze-plugins": {
"command": "node",
"args": ["coze-mcp-bridge.js"],
"env": {
"COZE_API_KEY": "your-api-key"
}
}
}
}
⚠️ 注意事项
- 网络要求:OpenClaw Gateway需要公网可访问
- 延迟:Coze → OpenClaw → 模型 → 返回,总延迟可能较高
- 费用:注意Coze和OpenClaw的API调用费用
- 安全:Webhook需要验证请求来源
🔗 相关资源