OpenClawSkills
GitHub
自动化 • 5 分钟阅读

Gmail 发布订阅

Gmail Pub/Sub 通过 gogcli 推送有线到 OpenClaw webhooks

目标:Gmail 监视 -> Pub/Sub 推送 -> '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'gog gmail watch serve'</code>' -> OpenClaw webhook。

Tutorial.step

先决条件

- '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'gcloud'</code>' 安装并登录('<a href="https://docs.cloud.google.com/sdk/docs/install-sdk" className="text-emerald-400 hover:text-emerald-300 transition-colors">'安装指南'</a>')。

- 为 Gmail 帐户安装并授权 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'gog'</code>' (gogcli) ('<a href="https://gogcli.sh/" className="text-emerald-400 hover:text-emerald-300 transition-colors">'gogcli.sh'</a>')。

- 启用 OpenClaw 挂钩(请参阅 '<a href="/automation/webhook" className="text-emerald-400 hover:text-emerald-300 transition-colors">'Webhooks'</a>')。

- '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'tailscale'</code>' 已登录 ('<a href="https://tailscale.com/" className="text-emerald-400 hover:text-emerald-300 transition-colors">'tailscale.com'</a>')。支持的设置使用 Tailscale Funnel 作为公共 HTTPS 端点。

其他隧道服务也可以工作,但是是 DIY/不受支持的,并且需要手动接线。

目前,我们支持 Tailscale。

示例挂钩配置(启用 Gmail 预设映射):

Json5
{
  hooks: {
    enabled: true,
    token: "OPENCLAW_HOOK_TOKEN",
    path: "/hooks",
    presets: ["gmail"],
  },
}

要将 Gmail 摘要传送到聊天界面,请使用映射覆盖预设

设置 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'deliver'</code>' + 可选 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'channel'</code>'/'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'to'</code>' :

Json5
{
  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}}
Subject: {{messages[0].subject}}
{{messages[0].snippet}}
{{messages[0].body}}",
        model: "openai/gpt-5.2-mini",
        deliver: true,
        channel: "last",
        // to: "+15551234567"
      },
    ],
  },
}

如果您想要固定频道,请设置 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'channel'</code>' + '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'to'</code>'。否则'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'channel: &quot;last&quot;'</code>'

使用最后的传送路线(回退到 WhatsApp)。

要强制 Gmail 运行更便宜的模型,请在映射中设置 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'model'</code>'

('<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'provider/model'</code>' 或别名)。如果您强制执行 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'agents.defaults.models'</code>',请将其包含在那里。

要专门为 Gmail 挂钩设置默认模型和思维级别,请添加

配置中的 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.gmail.model'</code>' / '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.gmail.thinking'</code>' :

Json5
{
  hooks: {
    gmail: {
      model: "openrouter/meta-llama/llama-3.3-70b-instruct:free",
      thinking: "off",
    },
  },
}

注意事项:

- 映射中的每个钩子 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'model'</code>'/'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'thinking'</code>' 仍会覆盖这些默认值。

- 后备顺序:'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.gmail.model'</code>' → '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'agents.defaults.model.fallbacks'</code>' → 主要(身份验证/速率限制/超时)。

- 如果设置了 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'agents.defaults.models'</code>',则 Gmail 型号必须位于允许列表中。

- 默认情况下,Gmail 挂钩内容使用外部内容安全边界进行包装。

要禁用(危险),请设置 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.gmail.allowUnsafeExternalContent: true'</code>'。

要进一步自定义有效负载处理,请添加 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.mappings'</code>' 或 JS/TS 转换模块

在 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.transformsDir'</code>' 下(请参阅 '<a href="/automation/webhook" className="text-emerald-400 hover:text-emerald-300 transition-colors">'Webhooks'</a>')。

Tutorial.step

向导(推荐)

使用 OpenClaw 助手将所有内容连接在一起(通过brew 在 macOS 上安装 deps):

Bash
openclaw webhooks gmail setup   --account [email protected]

默认值:

- 使用 Tailscale Funnel 作为公共推送端点。

- 为 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'openclaw webhooks gmail run'</code>' 写入 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'openclaw webhooks gmail run'</code>' 配置。

- 启用 Gmail 挂钩预设 ('<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.presets: [&quot;gmail&quot;]'</code>')。

路径说明:当'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'tailscale.mode'</code>'启用时,OpenClaw自动设置

'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.gmail.serve.path'</code>' 到 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'/'</code>' 并将公共路径保持在

'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.gmail.tailscale.path'</code>'(默认 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'/gmail-pubsub'</code>'),因为 Tailscale

在代理之前去除 set-path 前缀。

如果需要后端接收前缀路径,请设置

'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.gmail.tailscale.target'</code>' (或 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'--tailscale-target'</code>') 到完整的 URL,例如

'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'http://127.0.0.1:8788/gmail-pubsub'</code>' 并匹配 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.gmail.serve.path'</code>'。

Want custom endpoint? Use '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'--push-endpoint &lt;url&gt;'</code>' or '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'--tailscale off'</code>'.

平台说明:在 macOS 上,向导会安装 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'gcloud'</code>'、'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'gogcli'</code>' 和 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'tailscale'</code>'

通过自制软件;在 Linux 上首先手动安装它们。

网关自动启动(推荐):

- 当设置'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.enabled=true'</code>'和'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'hooks.gmail.account'</code>'时,网关启动

'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'gog gmail watch serve'</code>' 启动并自动更新手表。

- 设置 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'OPENCLAW_SKIP_GMAIL_WATCHER=1'</code>' 以选择退出(如果您自己运行守护程序,则很有用)。

- 不要同时运行手动守护进程,否则你会遇到

'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'listen tcp 127.0.0.1:8788: bind: address already in use'</code>'。

手动守护进程(启动 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'gog gmail watch serve'</code>' + 自动续订):

Bash
openclaw webhooks gmail run
Tutorial.step

一次性设置

1. 选择拥有 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'gog'</code>' 使用的 OAuth 客户端**的 GCP 项目。

Bash
gcloud auth login
gcloud config set project <project-id>

注意:Gmail 手表要求 Pub/Sub 主题与 OAuth 客户端位于同一项目中。

2. 启用API:

Bash
gcloud services enable gmail.googleapis.com pubsub.googleapis.com

3. 创建主题:

Bash
gcloud pubsub topics create gog-gmail-watch

4.允许Gmail推送发布:

Bash
gcloud pubsub topics add-iam-policy-binding gog-gmail-watch   --member=serviceAccount:[email protected]   --role=roles/pubsub.publisher
Tutorial.step

启动手表

Bash
gog gmail watch start   --account [email protected]   --label INBOX   --topic projects/<project-id>/topics/gog-gmail-watch

保存输出中的 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'history_id'</code>' (用于调试)。

Tutorial.step

运行推送处理程序

本地示例(共享令牌身份验证):

Bash
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

注意事项:

- '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'--token'</code>' 保护推送端点('<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'x-gog-token'</code>' 或 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'?token='</code>')。

- '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'--hook-url'</code>' 指向 OpenClaw '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'/hooks/gmail'</code>' (映射;隔离运行 + 对主程序的摘要)。

- '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'--include-body'</code>' 和 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'--max-bytes'</code>' 控制发送到 OpenClaw 的正文片段。

推荐:'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'openclaw webhooks gmail run'</code>' 包装相同的流程并自动更新手表。

Tutorial.step

公开处理程序(高级,不支持)

如果您需要非 Tailscale 隧道,请手动连接并在推送中使用公共 URL

订阅(不支持,无护栏):

Bash
cloudflared tunnel --url http://127.0.0.1:8788 --no-autoupdate

使用生成的 URL 作为推送端点:

Bash
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,然后运行:

Bash
gog gmail watch serve --verify-oidc --oidc-email <svc@...>
Tutorial.step

测试

向观看的收件箱发送消息:

Bash
gog gmail send   --account [email protected]   --to [email protected]   --subject "watch test"   --body "ping"

检查手表状态和历史记录:

Bash
gog gmail watch status --account [email protected]
gog gmail history --account [email protected] --since <historyId>
Tutorial.step

故障排除

- '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'Invalid topicName'</code>':项目不匹配(主题不在 OAuth 客户端项目中)。

- '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'User not authorized'</code>':该主题缺少 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'roles/pubsub.publisher'</code>'。

- 空消息:Gmail推送仅提供'<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'historyId'</code>';通过 '<code className="bg-white/10 px-1.5 py-0.5 rounded text-emerald-300 text-sm">'gog gmail history'</code>' 获取。

Tutorial.step

清理

Bash
gog gmail watch stop --account [email protected]
gcloud pubsub subscriptions delete gog-gmail-watch-push
gcloud pubsub topics delete gog-gmail-watch