OpenClaw Webhook:与外部系统无缝集成

4 阅读1分钟

轻松连接任何 API

什么是 Webhook?

Webhook 是一种让外部系统主动通知 OpenClaw 的方式。

外部系统 → HTTP POST → OpenClaw → 处理

快速开始

配置 Webhook 服务器

# ~/.openclaw/config.yaml
webhooks:
  enabled: true
  port: 8080
  path: /webhook

创建处理脚本

# ~/.openclaw/webhooks/handle.js
const body = JSON.parse(process.env.WEBHOOK_BODY);
console.log(`收到 Webhook: ${body.event}`);

启动

openclaw webhooks start

实战案例

案例 1:GitHub PR 监控

// ~/.openclaw/webhooks/github.js
const body = JSON.parse(process.env.WEBHOOK_BODY);

if (body.action === 'opened') {
  console.log(`
📦 新 PR:${body.pull_request.title}
👤 作者:${body.user.login}
🔗 链接:${body.pull_request.html_url}
  `);

  // 自动调用 AI 审查
  const review = await ai.codeReview(body.pull_request.diff_url);
  console.log(`Review: ${review}`);
}

配置 GitHub

  1. 仓库 Settings → Webhooks
  2. Payload URL: https://your-domain.com/webhook/github
  3. 选择事件:Pull requests

案例 2:飞书机器人

channels:
  - name: feishu-bot
    type: webhook
    url: ${FEISHU_WEBHOOK_URL}
// 发送消息到飞书
fetch(process.env.FEISHU_WEBHOOK_URL, {
  method: 'POST',
  body: JSON.stringify({
    msg_type: "text",
    content: {
      text: "来自 OpenClaw 的消息"
    }
  })
});

案例 3:Stripe 支付通知

// ~/.openclaw/webhooks/stripe.js
const body = JSON.parse(process.env.WEBHOOK_BODY);

if (body.type === 'payment_intent.succeeded') {
  console.log(`💰 收到付款: $${body.amount}`);
  
  // 更新数据库
  db.updatePayment(body.id, 'paid');
  
  // 发送通知
  notify.admin('新付款通知');
}

Webhook 安全

1. 签名验证

const crypto = require('crypto');

const signature = req.headers['x-signature'];
const body = req.body;

const expected = crypto
  .createHmac('sha256', WEBHOOK_SECRET)
  .update(JSON.stringify(body))
  .digest('hex');

if (signature !== expected) {
  throw new Error('签名验证失败');
}

2. IP 白名单

webhooks:
  whitelist:
    - 1.2.3.4
    - 5.6.7.8

3. 时间戳验证

const timestamp = req.headers['x-timestamp'];
if (Math.abs(Date.now() - timestamp) > 5000) {
  throw new Error('请求过期');
}

高级功能

1. 路由分发

webhooks:
  routes:
    - path: /github
      handler: github.js
    - path: /stripe
      handler: stripe.js
    - path: /feishu
      handler: feishu.js

2. 过滤条件

// 只处理特定事件
if (body.event !== 'payment') {
  return;  // 忽略
}

3. 重试机制

webhooks:
  retry:
    max: 3
    delay: 1m

测试 Webhook

# 测试脚本
curl -X POST http://localhost:8080/webhook \
  -H "Content-Type: application/json" \
  -d '{"event": "test", "data": "hello"}'

常见平台 Webhook 配置

GitHub

Settings → Webhooks → Add webhook
Payload URL: https://your-domain.com/webhook/github

GitLab

Settings → Webhooks
URL: https://your-domain.com/webhook/gitlab

Stripe

Developers → Webhooks → Add endpoint
Endpoint URL: https://your-domain.com/webhook/stripe

Slack

Incoming Webhooks → Create Incoming Webhook
Webhook URL: https://hooks.slack.com/...

错误处理

try {
  // 处理 webhook
} catch (error) {
  console.error('Webhook 处理失败:', error);
  
  // 返回错误(触发重试)
  throw error;
}

日志记录

logging:
  webhooks:
    enabled: true
    file: ~/.openclaw/logs/webhooks.log

💬 你用 Webhook 做什么?评论区分享!

🎯 需要 Webhook 集成服务?微信:yang1002378395