目标:Gmail watch → Pub/Sub 推送 → gog gmail watch serve → OpenClaw webhook。
前置条件
- 已安装并登录
gcloud(见 官方安装指南)。 - 已安装并授权
gog(gogcli)用于该 Gmail 账号。 - OpenClaw hooks 已启用(见 Webhooks)。
- 已登录
tailscale。官方支持的方案使用 Tailscale Funnel 提供公网 HTTPS 端点;其它隧道可用但需手工配置且不在支持范围内。
Hook 配置(Gmail 预设)
启用 Gmail 预设映射:
{
hooks: {
enabled: true,
token: "OPENCLAW_HOOK_TOKEN",
path: "/hooks",
presets: ["gmail"],
},
}
如果希望把 Gmail 摘要直接投递到聊天界面,覆盖预设并设置 deliver + 可选 channel/to:
{
hooks: {
enabled: true,
token: "OPENCLAW_HOOK_TOKEN",
presets: ["gmail"],
mappings: [
{
match: { path: "gmail" },
action: "agent",
wakeMode: "now",
name: "Gmail",
sessionKey: "hook:gmail:{{messages[0].id}}",
messageTemplate: "New email from {{messages[0].from}}\n Subject: {{messages[0].subject}}\n{{messages[0].snippet}}\n{{messages[0].body}}",
model: "openai/gpt-5.2-mini",
deliver: true,
channel: "last",
// to: "+15551234567"
},
],
},
}
固定投递到某个渠道时设置 channel + to。如果 channel: "last",则使用最近的投递通道(默认回退到 WhatsApp)。要强制使用更便宜的模型,可在映射中设置 model(provider/model 或别名)。若启用 agents.defaults.models 白名单,请确保 Gmail 模型在其中。
也可为 Gmail hooks 设置默认模型与思考级别:
{
hooks: {
gmail: {
model: "openrouter/meta-llama/llama-3.3-70b-instruct:free",
thinking: "off",
},
},
}
- 映射里的
model/thinking仍会覆盖这里的默认值。 - 回退顺序:
hooks.gmail.model→agents.defaults.model.fallbacks→ primary。 - Gmail hook 内容默认带外部内容安全边界。要关闭(危险),设置
hooks.gmail.allowUnsafeExternalContent: true。
想进一步定制 payload 处理,可通过 hooks.mappings 或在 hooks.transformsDir 下放 JS/TS 转换模块(见 Webhooks)。
向导(推荐)
使用 OpenClaw 向导一键配置(macOS 会通过 brew 自动安装依赖):
openclaw webhooks gmail setup \
--account [email protected]
默认行为:
- 使用 Tailscale Funnel 作为公网推送端点。
- 写入
hooks.gmail配置,供openclaw webhooks gmail run使用。 - 启用 Gmail hook 预设(
hooks.presets: ["gmail"])。
路径说明:当启用 tailscale.mode 时,OpenClaw 会自动把 hooks.gmail.serve.path 设为 /,并把公网路径保存在 hooks.gmail.tailscale.path(默认 /gmail-pubsub),因为 Tailscale 会在代理前剥离前缀。如果希望后端收到带前缀的路径,请设置 hooks.gmail.tailscale.target(或 --tailscale-target)为完整 URL,例如 http://127.0.0.1:8788/gmail-pubsub,并匹配 hooks.gmail.serve.path。
想使用自定义公网端点,可用 --push-endpoint <url> 或 --tailscale off。平台差异:macOS 向导会安装 gcloud、gogcli、tailscale,Linux 需先手动安装。
Gateway 自动启动(推荐):
- 当
hooks.enabled=true且设置了hooks.gmail.account,Gateway 启动时会自动运行gog gmail watch serve并续约 watch。 - 如需关闭,设置
OPENCLAW_SKIP_GMAIL_WATCHER=1。 - 不要同时运行手动守护进程,否则会出现端口占用(如
listen tcp 127.0.0.1:8788: bind: address already in use)。
手动守护进程(启动 gog gmail watch serve + 自动续约):
openclaw webhooks gmail run
一次性设置
- 选择与
gogOAuth 客户端同属的 GCP 项目:gcloud auth login gcloud config set project <project-id>注意:Gmail watch 要求 Pub/Sub 主题必须在 OAuth 客户端所在的同一项目中。
- 启用 API:
gcloud services enable gmail.googleapis.com pubsub.googleapis.com - 创建主题:
gcloud pubsub topics create gog-gmail-watch - 授予 Gmail 推送权限:
gcloud pubsub topics add-iam-policy-binding gog-gmail-watch \ --member=serviceAccount:[email protected] \ --role=roles/pubsub.publisher
启动 watch
gog gmail watch start \
--account [email protected] \
--label INBOX \
--topic projects/<project-id>/topics/gog-gmail-watch
请保存输出中的 history_id(调试用)。
运行推送处理器
本地示例(共享 token 认证):
gog gmail watch serve \
--account [email protected] \
--bind 127.0.0.1 \
--port 8788 \
--path /gmail-pubsub \
--token <shared> \
--hook-url http://127.0.0.1:18789/hooks/gmail \
--hook-token OPENCLAW_HOOK_TOKEN \
--include-body \
--max-bytes 20000
--token保护推送端点(x-gog-token或?token=)。--hook-url指向 OpenClaw 的/hooks/gmail(映射为隔离运行并汇总到主会话)。--include-body与--max-bytes控制传给 OpenClaw 的正文片段。
推荐:openclaw webhooks gmail run 会封装相同流程并自动续约 watch。
暴露处理器(高级 / 不支持)
如果需要非 Tailscale 隧道,需要自行接线并在 Pub/Sub 推送订阅中填写公网 URL(官方不提供保障):
cloudflared tunnel --url http://127.0.0.1:8788 --no-autoupdate
用生成的 URL 作为 push endpoint:
gcloud pubsub subscriptions create gog-gmail-watch-push \
--topic gog-gmail-watch \
--push-endpoint "https://<public-url>/gmail-pubsub?token=<shared>"
生产环境建议:使用稳定的 HTTPS 端点并配置 Pub/Sub OIDC JWT,然后运行:
gog gmail watch serve --verify-oidc --oidc-email <svc@...>
测试
向被监听邮箱发送邮件:
gog gmail send \
--account [email protected] \
--to [email protected] \
--subject "watch test" \
--body "ping"
检查 watch 状态与历史:
gog gmail watch status --account [email protected]
gog gmail history --account [email protected] --since <historyId>
故障排除
Invalid topicName:项目不匹配(主题不在 OAuth 客户端项目中)。User not authorized:主题缺少roles/pubsub.publisher权限。- 空消息:Gmail 推送只包含
historyId,需要用gog gmail history拉取详情。
清理
gog gmail watch stop --account [email protected]
gcloud pubsub subscriptions delete gog-gmail-watch-push
gcloud pubsub topics delete gog-gmail-watch