Gmail Pub/Sub -> OpenClaw
目标:Gmail watch -> Pub/Sub push -> gog gmail watch serve -> OpenClaw webhook。
前置条件
gcloud已安装并登录(安装指南)。gog(gogcli)已安装并授权了 Gmail 账号(gogcli.sh)。- OpenClaw hooks 已启用(见 Webhooks)。
tailscale已登录(tailscale.com)。目前支持的方案是用 Tailscale Funnel 提供公网 HTTPS 端点。其他隧道服务可以用,但属于 DIY/不提供官方支持,需要自己手动配置。目前我们只正式支持 Tailscale。
Hook 配置示例(启用 Gmail 预设映射):
{
hooks: {
enabled: true,
token: "OPENCLAW_HOOK_TOKEN",
path: "/hooks",
presets: ["gmail"],
},
}
如果想把 Gmail 摘要投递到聊天界面,用自定义 mapping 覆盖预设,设置 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}}\nSubject: {{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)。
想给 Gmail 运行用更便宜的模型,在 mapping 里设 model(provider/model 或别名)。如果启用了 agents.defaults.models 限制,确保模型在允许列表里。
要给 Gmail hook 专门设默认模型和思考级别,在配置中加 hooks.gmail.model / hooks.gmail.thinking:
{
hooks: {
gmail: {
model: "openrouter/meta-llama/llama-3.3-70b-instruct:free",
thinking: "off",
},
},
}
说明:
- mapping 里的
model/thinking仍然会覆盖这些默认值。 - 回退顺序:
hooks.gmail.model→agents.defaults.model.fallbacks→ 主模型(认证/限流/超时)。 - 如果配了
agents.defaults.models,Gmail 模型必须在允许列表中。 - Gmail hook 内容默认用外部内容安全边界包裹。要关闭(危险),设
hooks.gmail.allowUnsafeExternalContent: true。
要进一步自定义负载处理,添加 hooks.mappings 或在 ~/.openclaw/hooks/transforms 下放 JS/TS 转换模块(见 Webhooks)。
向导(推荐)
用 OpenClaw 辅助工具一键搞定(macOS 上会通过 brew 安装依赖):
openclaw webhooks gmail setup \
--account [email protected]
默认行为:
- 用 Tailscale Funnel 做公网推送端点。
- 为
openclaw webhooks gmail run写入hooks.gmail配置。 - 启用 Gmail hook 预设(
hooks.presets: ["gmail"])。
路径说明:启用 tailscale.mode 时,OpenClaw 自动把 hooks.gmail.serve.path 设为 /,公网路径保持在 hooks.gmail.tailscale.path(默认 /gmail-pubsub),因为 Tailscale 代理前会去掉 set-path 前缀。如果需要后端收到带前缀的路径,把 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 上向导通过 Homebrew 安装 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
一次性设置
- 选择拥有
gog使用的 OAuth 客户端的 GCP 项目。
gcloud auth login
gcloud config set project <project-id>
注意:Gmail watch 要求 Pub/Sub topic 和 OAuth 客户端在同一个项目中。
- 启用 API:
gcloud services enable gmail.googleapis.com pubsub.googleapis.com
- 创建 topic:
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 的隧道,手动接线并在推送订阅中使用公网 URL(不提供支持,没有防护):
cloudflared tunnel --url http://127.0.0.1:8788 --no-autoupdate
用生成的 URL 作为推送端点:
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:项目不匹配(topic 不在 OAuth 客户端所在项目中)。User not authorized:topic 缺少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