Skip to content

Hermes Webhook实战:搭建事件驱动的AI工作流

2026年4月26日

Hermes Webhook实战:搭建事件驱动的AI工作流

AI Agent 吹了这么久,到底能干啥?直到我开始玩 Webhook,才真正理解什么叫「Agent 在你睡觉的时候替你干活」。

Webhook 是什么?

打个比方:

  • 轮询(Polling):每隔5分钟开门看一眼有没有快递
  • Webhook:装了个门铃——快递到了,一按,你立刻就知道了

技术上说,就是一个外部服务在某件事发生时,往你指定的 URL 发一个 HTTP POST 请求,请求里带着事件详情的 JSON 数据。谁下了订单、哪笔支付到账、哪个 PR 被提交——发生即通知,实时的。

Agent 改变了这件事:不用自己去处理通知,Agent 自动审查代码、推送通知、分析数据、回写结果。全程不需要打开聊天窗口。

Hermes Webhook 的核心能力

入站:外部 -> Agent

GitHub 提了个 PR、有赞来了新订单、微信支付收到付款——事件进来,Agent 自动处理。

出站:Agent -> 外部

Agent 完成分析后,把结果 POST 到飞书、企业微信,或直接写回 GitHub 做评论。

这两个方向组合起来,Agent 就变成了事件驱动、自动响应、跨平台协调的后端服务。

配置实操:从零开始

第一步:启用 Webhook 适配器

方式一:向导配置

bash
hermes gateway setup

按提示启用 Webhook、设端口、设全局 HMAC 密钥。

方式二:手动配置

编辑 ~/.hermes/.env

bash
WEBHOOK_ENABLED=true
WEBHOOK_PORT=8644
WEBHOOK_SECRET=your-global-secret

验证服务是否启动:

bash
curl http://localhost:8644/health
# 返回 {"status": "ok", "platform": "webhook"}

第二步:定义路由

路由决定:什么事件进来、用什么提示词交给 Agent、结果送去哪。

编辑 ~/.hermes/config.yaml

yaml
platforms:
  webhook:
    enabled: true
    extra:
      port: 8644
      secret: "global-fallback-secret"
      routes:
        github-pr:
          events: ["pull_request"]
          secret: "github-webhook-secret"
          prompt: |
            Review this pull request:
            Repository: {repository.full_name}
            PR #{number}: {pull_request.title}
            Author: {pull_request.user.login}
            URL: {pull_request.html_url}
            Diff URL: {pull_request.diff_url}
            Action: {action}
          skills: ["github-code-review"]
          deliver: "github_comment"
          deliver_extra:
            repo: "{repository.full_name}"
            pr_number: "{number}"

关键字段

字段说明
events监听哪些事件类型,如 pull_requestpush
prompt给 Agent 的指令模板,{field} 自动从 JSON payload 取值
skills注入技能,如 github-code-review
deliver结果投递目标:github_commentfeishuwecom
deliver_extra投递附加参数

第三步:配置外部服务

以 GitHub 为例:

  1. 仓库 -> Settings -> Webhooks -> Add webhook
  2. Payload URL:http://your-server:8644/webhooks/github-pr
  3. Content type:application/json
  4. Secret:与路由配置一致
  5. 触发事件:Pull requests
  6. 保存

第四步:确保投递目标已连接

bash
# GitHub CLI 认证
gh auth login

# 飞书机器人:创建自定义机器人,获取 Webhook URL

四个真实场景

场景一:电商订单实时通知 + 定时报告

实时通知

yaml
routes:
  new-order:
    events: ["trade.TradePaid"]
    secret: "youzan-webhook-secret"
    prompt: |
      有赞新订单到账!
      订单号:{order_no}
      买家:{buyer_nick}
      商品:{orders[0].title}
      实付金额:{payment} 元
    deliver: "feishu"
    deliver_extra:
      webhook_url: "https://open.feishu.cn/open-apis/bot/v2/hook/your-token"

定时报告

bash
hermes cron create \
  --schedule "every 1h" \
  --prompt "根据脚本输出,总结最近一小时有赞店铺的销售情况。" \
  --script youzan_stats.py

场景二:GitHub PR 自动代码审查

有人提 PR -> GitHub 触发 Webhook -> Agent 拉取 diff、分析代码 -> 自动在 PR 下留审查评论。

踩坑点

  • GitHub CLI 必须在 Gateway 运行环境里安装和认证
  • Gateway 必须在运行状态,否则 Webhook 打过来是 502
  • ngrok 免费版 URL 每次重启会变,需更新 GitHub 配置

场景三:Agent 分析数据后回写应用

cron job 让 Agent 定时跑 Python 脚本拉 RSS 新闻源,分析热点主题,POST 结果到应用 API。

bash
hermes cron create \
  --schedule "0 9 * * *" \
  --script poll_news_rss.py \
  --prompt "分析新闻热点趋势,提取前10个关键词,POST到 https://your-app.com/api/agent-updates"

出站模式不需要 ngrok,Agent 主动发 HTTP 请求。

场景四:微信公众号新关注者自动运营

配置公众号服务器

  1. 设置与开发 -> 基本配置 -> 服务器配置
  2. URL:http://your-server:8644/webhooks/wechat-mp
  3. Token:验证签名用

路由配置

yaml
routes:
  wechat-mp-subscribe:
    events: ["subscribe"]
    secret: "your-mp-token"
    prompt: |
      微信公众号新增关注者!
      用户 OpenID:{FromUserName}
      请生成热情的欢迎语。
    skills: ["mp-welcome"]
    deliver: "wechat_mp_reply"
    deliver_extra:
      to_user: "{FromUserName}"
      from_user: "{ToUserName}"

踩坑点

  • 微信要求 5 秒内响应,超时用 deliver_only 先回「正在处理」
  • 未认证订阅号每天只能发 1 条模板消息,客服消息无此限制

直投模式:不需要 Agent 思考

有些场景只是想把消息推到飞书/企业微信,不需要 LLM:

yaml
routes:
  deploy-notify:
    events: ["push"]
    secret: "deploy-secret"
    prompt: "New push to {repository.full_name}: {head_commit.message}"
    deliver: "wecom"
    deliver_only: true

deliver_only: true 模式下,prompt 模板渲染完直接投递,零 token 消耗。

CLI 动态创建:

bash
hermes webhook subscribe order-matches \
  --deliver wecom \
  --deliver-webhook-key "your-wecom-bot-key" \
  --deliver-only \
  --prompt "新订单:{order.buyer_nick} 购买了 {order.title}!"

安全配置要点

HMAC 签名验证

平台Header
GitHubX-Hub-Signature-256
有赞Authorization
通用X-Webhook-Signature

签名不对直接 401 拒掉。

限流

默认每个路由 30 请求/分钟,超了返回 429:

yaml
platforms:
  webhook:
    extra:
      rate_limit: 60

其他安全措施

  • 每个路由必须有 secret
  • 幂等性:重复投递自动去重,缓存 1 小时
  • 请求体大小限制:默认 1MB

特别提醒:Webhook payload 里包含攻击者可控的数据,强烈建议在 Docker 或虚拟机里跑 Gateway。

总结

Webhook 把 Agent 从「你问它答」的被动模式,变成了「事件驱动、主动响应」的服务节点。

模式特点
入站外部事件触发 Agent 处理
出站Agent 处理结果推送外部
直投不调用 LLM,零 token 消耗

自动化的门槛从来不是「有没有工具」,而是「愿不愿意花一个小时把管道接好」。

官方文档https://hermes-agent.nousresearch.com/docs/user-guide/messaging/webhooks


关键词:Hermes Webhook, AI工作流自动化, 事件驱动Agent, GitHub PR自动审查, 微信公众号自动化, 有赞订单通知, Webhook配置

不要孤军奋战啦!

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

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

微信公众号

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

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