🔗 OpenClaw Webhook配置完全指南
📖 功能介绍
Webhook让OpenClaw能够"响应外部事件"。当GitHub有新的issue、Slack有人@你、或者你的网站有用户注册时,OpenClaw都能自动触发相应的任务。
这就是AI的"神经系统"!让AI不仅仅是被动响应,还能主动感知外部世界的变化。
🚀 使用方法
配置Webhook回调
// 创建带Webhook回调的任务
cron({
action: "add",
job: {
name: "数据同步任务",
schedule: { "kind": "cron", "expr": "0 */2 * * *" },
payload: {
kind: "agentTurn",
message: "执行数据同步"
},
delivery: {
mode: "webhook",
to: "https://your-server.com/callback"
}
}
})
接收Webhook事件
// 在Agent中处理Webhook事件
// 外部服务发送POST请求到OpenClaw Gateway
// Gateway会将事件转换为agentTurn消息
// 示例:GitHub Webhook
// 当有新的PR时,自动执行代码审查
{
"payload": {
"kind": "agentTurn",
"message": "请审查这个PR: https://github.com/user/repo/pull/123"
}
}
✨ 最佳实践
- 安全验证: Webhook URL要加签名验证,防止伪造
- 超时处理: 长时间任务用异步,避免HTTP超时
- 重试机制: 失败时外部服务会重试,设计幂等接口
- 日志追踪: 记录每次Webhook触发,便于排查
💻 代码示例
示例1:GitHub Issue自动处理
// GitHub Webhook -> OpenClaw -> 自动回复
// 1. 配置GitHub Webhook指向OpenClaw
// 2. Agent收到事件后自动处理
// 处理函数
async function handleGitHubIssue(event) {
const issue = event.issue
// AI分析issue内容
const response = await analyzeIssue(issue.body)
// 自动回复
await githubClient.issues.createComment({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
body: `🤖 AI助手分析:\n\n${response.suggestion}`
})
}
示例2:网站监控告警
// 监控系统 -> Webhook -> OpenClaw -> 通知
{
"payload": {
"kind": "agentTurn",
"message": "网站alert! 错误率超过5%,请检查并处理"
}
}
// Agent自动执行检查
exec({ command: "curl -s https://miaoquai.com/health" })
// 发送告警到飞书
message({
action: "send",
channel: "feishu",
target: "ops-channel",
message: "⚠️ 网站异常!错误率5%,已自动检查..."
})
🎯 妙趣实测技巧
Webhook加
bestEffort: true真的很有用!有些回调不重要但会拖慢速度,开这个选项能让核心任务先跑完,回调失败也不影响主流程。