Skip to content

OpenClaw Webhook 外部触发完整指南:让外部服务驱动 AI 执行

2026年4月8日

OpenClaw Webhook 外部触发完整指南:让外部服务驱动 AI 执行

定时任务让 OpenClaw 按时间行动,Webhook 则让它被外部事件驱动。GitHub 有新 Issue、监控告警触发、表单提交——这些事件都可以通过 Webhook 触发 OpenClaw 执行任务。

Webhook 是什么?

简单理解: Webhook 是一个 URL,外部服务向这个 URL 发送请求,OpenClaw 就会执行对应的任务。

外部事件 → 发送 HTTP 请求 → OpenClaw Webhook → Agent 执行任务

典型应用场景:

场景外部服务触发动作
GitHub 新 IssueGitHub自动分析、回复
监控告警Prometheus发送通知、尝试修复
表单提交Typeform数据处理、入库
支付成功Stripe发送确认邮件
部署完成CI/CD通知团队

Webhook 配置方法

方法一:命令行配置

bash
# 创建 Webhook
openclaw webhook create my-webhook \
  --path /webhooks/github-issue \
  --agent github-assistant \
  --secret my-secret-key

方法二:配置文件

~/.openclaw/openclaw.json 中添加:

json
{
  "webhooks": [
    {
      "name": "github-issue",
      "path": "/webhooks/github-issue",
      "agent": "github-assistant",
      "secret": "your-webhook-secret",
      "enabled": true
    }
  ]
}

配置说明:

字段说明
nameWebhook 名称,用于识别
pathURL 路径,如 /webhooks/github-issue
agent触发的 Agent 名称
secret密钥,用于验证请求来源
enabled是否启用

获取 Webhook URL

配置后,Webhook URL 格式为:

http://你的服务器IP:18789/webhooks/github-issue

或本地:

http://localhost:18789/webhooks/github-issue

实战案例一:GitHub Issue 自动回复

场景

GitHub 有新 Issue 时,自动分析问题并回复。

Step 1:创建 Webhook

json
{
  "webhooks": [
    {
      "name": "github-issue",
      "path": "/webhooks/github-issue",
      "agent": "github-assistant",
      "secret": "gh-webhook-secret-2026",
      "enabled": true
    }
  ]
}

Step 2:配置 GitHub Webhook

  1. 进入 GitHub 仓库 → Settings → Webhooks
  2. 点击 "Add webhook"
  3. 填写配置:
字段
Payload URLhttp://你的服务器:18789/webhooks/github-issue
Content typeapplication/json
Secretgh-webhook-secret-2026
Events选择 "Issues"
  1. 点击 "Add webhook"

Step 3:配置 Agent

创建 Agent 处理 Issue:

json
{
  "agents": {
    "github-assistant": {
      "model": "deepseek/deepseek-chat",
      "systemPrompt": "你是 GitHub Issue 助手。收到新 Issue 时,分析问题并给出建议回复。回复要简洁、专业、有帮助。"
    }
  }
}

Step 4:测试

创建一个测试 Issue,检查 OpenClaw 是否收到请求:

bash
# 查看 Webhook 日志
openclaw logs -f | grep webhook

实战案例二:监控告警触发

场景

Prometheus 监控到异常时,发送告警到 OpenClaw,AI 自动分析并通知。

Step 1:创建 Webhook

json
{
  "webhooks": [
    {
      "name": "prometheus-alert",
      "path": "/webhooks/prometheus",
      "agent": "monitor-assistant",
      "secret": "prometheus-secret",
      "enabled": true
    }
  ]
}

Step 2:配置 Prometheus Alertmanager

编辑 alertmanager.yml

yaml
route:
  receiver: 'openclaw-webhook'

receivers:
  - name: 'openclaw-webhook'
    webhook_configs:
      - url: 'http://openclaw-server:18789/webhooks/prometheus'
        send_resolved: true

Step 3:配置 Agent

json
{
  "agents": {
    "monitor-assistant": {
      "model": "deepseek/deepseek-chat",
      "systemPrompt": "你是运维监控助手。收到告警时,分析问题原因,给出排查建议,并通过微信通知相关负责人。"
    }
  }
}

实战案例三:第三方服务集成

场景

使用 Zapier、Make 或 n8n 等 iPaaS 平台触发 OpenClaw。

配置方法

以 Zapier 为例:

  1. 创建 Zap,选择触发器(如 Google Forms 提交)
  2. 添加 Action,选择 "Webhooks by Zapier"
  3. 配置 POST 请求:
字段
URLhttp://your-server:18789/webhooks/form-submit
MethodPOST
Data表单数据的 JSON
  1. 测试并发布

请求格式

标准请求

bash
curl -X POST http://localhost:18789/webhooks/github-issue \
  -H "Content-Type: application/json" \
  -H "X-Webhook-Secret: your-secret" \
  -d '{
    "action": "opened",
    "issue": {
      "number": 42,
      "title": "Bug: 安装失败",
      "body": "在 Windows 上安装时报错..."
    },
    "repository": {
      "name": "my-project"
    }
  }'

请求头

Header说明
Content-Typeapplication/json
X-Webhook-Secret密钥验证(可选但推荐)

安全认证

密钥验证

配置 secret 后,OpenClaw 会验证请求头中的 X-Webhook-Secret

bash
# 正确的请求
curl -X POST http://localhost:18789/webhooks/test \
  -H "X-Webhook-Secret: your-secret" \
  -H "Content-Type: application/json" \
  -d '{"test": true}'

# 错误的请求(密钥不匹配)
# 返回 401 Unauthorized

IP 白名单

在配置中添加 IP 白名单:

json
{
  "webhooks": [
    {
      "name": "github-issue",
      "path": "/webhooks/github-issue",
      "agent": "github-assistant",
      "secret": "your-secret",
      "allowedIPs": [
        "192.30.252.0/22",
        "185.199.108.0/22"
      ]
    }
  ]
}

高级配置

条件触发

只在特定条件下执行:

json
{
  "webhooks": [
    {
      "name": "github-issue",
      "path": "/webhooks/github-issue",
      "agent": "github-assistant",
      "conditions": [
        {
          "field": "action",
          "value": "opened"
        },
        {
          "field": "issue.labels",
          "contains": "bug"
        }
      ]
    }
  ]
}

重试机制

请求失败时自动重试:

json
{
  "webhooks": [
    {
      "name": "github-issue",
      "retry": {
        "enabled": true,
        "maxAttempts": 3,
        "delay": 5000
      }
    }
  ]
}

超时设置

json
{
  "webhooks": [
    {
      "name": "github-issue",
      "timeout": 30000
    }
  ]
}

调试技巧

查看 Webhook 日志

bash
# 实时查看日志
openclaw logs -f --filter webhook

# 查看特定 Webhook
openclaw logs --filter "github-issue"

测试 Webhook

bash
# 使用 curl 测试
curl -X POST http://localhost:18789/webhooks/test \
  -H "Content-Type: application/json" \
  -H "X-Webhook-Secret: test-secret" \
  -d '{"test": true}'

# 使用在线工具
# webhook.site 或 requestbin.com

查看 Webhook 列表

bash
# 列出所有 Webhook
openclaw webhook list

# 查看 Webhook 详情
openclaw webhook show github-issue

常见问题

问题一:Webhook 不触发

排查步骤:

bash
# 1. 检查 Webhook 是否启用
openclaw webhook show github-issue

# 2. 检查端口是否开放
curl http://localhost:18789/webhooks/github-issue

# 3. 查看日志
openclaw logs -f | grep webhook

问题二:返回 401 Unauthorized

原因: 密钥不匹配

解决:

bash
# 确认密钥配置
openclaw config get webhooks.github-issue.secret

# 测试时使用正确的密钥
curl -H "X-Webhook-Secret: 正确的密钥" ...

问题三:Agent 不执行

原因: Agent 配置错误或不存在

解决:

bash
# 检查 Agent 是否存在
openclaw agent list

# 检查 Webhook 关联的 Agent
openclaw webhook show github-issue

问题四:外网无法访问

原因: 防火墙或 NAT 问题

解决:

  1. 开放端口:
bash
# Linux
sudo ufw allow 18789

# 云服务器安全组放行 18789
  1. 使用内网穿透:
bash
# 使用 ngrok
ngrok http 18789

最佳实践

实践一:使用密钥验证

永远配置 secret,防止未授权请求:

json
{
  "webhooks": [{
    "secret": "强密钥-至少32字符"
  }]
}

实践二:记录请求日志

配置日志记录:

json
{
  "webhooks": [{
    "logging": {
      "enabled": true,
      "level": "debug"
    }
  }]
}

实践三:条件触发

避免不必要的执行:

json
{
  "conditions": [
    {"field": "action", "value": "opened"}
  ]
}

实践四:错误通知

配置失败通知:

json
{
  "webhooks": [{
    "onError": {
      "notify": ["wechat", "email"]
    }
  }]
}

总结

步骤操作
1创建 Webhook 配置
2获取 Webhook URL
3在外部服务中配置 URL
4测试验证
5监控日志

Webhook vs 定时任务:

场景推荐
事件驱动Webhook
定期执行定时任务
外部触发Webhook
自主执行定时任务

Webhook 让 OpenClaw 从"被动响应"变成"事件驱动"。GitHub 有新 Issue、监控告警、表单提交——这些外部事件都能自动触发 AI 执行任务,真正实现自动化闭环。

不要孤军奋战啦!

加入微信群一起学习交流 AI

与大神一起使用 OpenClaw、Hermes、Claude Code、Seedance 2.0、GPT-Image-2 等

微信公众号

扫码关注微信公众号
私信 "加群",将自动获取微信群二维码

探索 AI 世界,掌握智能未来