跳到內容

將 Gmail 整合至 OpenClaw:透過 Pub/Sub 自動化郵件處理

每天被淹沒在 Gmail 郵件堆裡,卻又不想手動去檢查有沒有重要通知?如果你想實現自動化的郵件處理流程,最優雅的方式就是透過 Google Cloud 的 Pub/Sub 機制即時推送通知到你的 OpenClaw。

這篇指南會教你如何串接 Gmail 與 OpenClaw,讓你能在郵件到達的瞬間,就透過 AI 進行摘要並發送到你常用的聊天軟體。

目標: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 摘要傳送到聊天介面,可以用包含 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 執行強制使用較便宜的模型,可以在映射中設定 model(provider/model 或別名)。如果你有強制執行 agents.defaults.models,請記得將它包含在內。

要為 Gmail hooks 設定特定的預設模型和思考等級,可以在你的配置中加入 hooks.gmail.model / hooks.gmail.thinking:

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

注意:

  • 映射中個別 hook 的 model/thinking 設定優先權仍高於這些預設值。
  • 回退順序:hooks.gmail.model → agents.defaults.model.fallbacks → 主要設定(auth/rate-limit/timeouts)。
  • 如果設定了 agents.defaults.models,Gmail 使用的模型必須在允許清單中。
  • 預設情況下,Gmail hook 內容會被外部內容安全邊界包裝。 若要停用(具危險性),請設定 hooks.gmail.allowUnsafeExternalContent: true。

若要進一步自定義 payload 處理,可以新增 hooks.mappings 或在 hooks.transformsDir 下新增 JS/TS 轉換模組(參考 Webhooks)。

使用 OpenClaw 輔助工具來完成所有串接(在 macOS 上會透過 brew 安裝依賴):

Terminal window
openclaw webhooks gmail setup \
--account openclaw@gmail.com

預設行為:

  • 使用 Tailscale Funnel 作為公開的 push 端點。
  • 為 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 在轉發前會剝離設定的路徑前綴。 如果你需要後端接收帶有前綴的路徑,請將 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 可以選擇退出(如果你想自己執行 daemon 的話這很有用)。
  • 不要同時執行手動 daemon,否則你會遇到 listen tcp 127.0.0.1:8788: bind: address already in use 錯誤。

手動 daemon(啟動 gog gmail watch serve + 自動續期):

Terminal window
openclaw webhooks gmail run
  1. 選擇擁有 gog 所使用的 OAuth client 的 GCP 專案。
Terminal window
gcloud auth login
gcloud config set project <project-id>

注意:Gmail watch 要求 Pub/Sub topic 必須與 OAuth client 位於同一個專案中。

  1. 啟用 API:
Terminal window
gcloud services enable gmail.googleapis.com pubsub.googleapis.com
  1. 建立 topic:
Terminal window
gcloud pubsub topics create gog-gmail-watch
  1. 允許 Gmail push 進行發布:
Terminal window
gcloud pubsub topics add-iam-policy-binding gog-gmail-watch \
--member=serviceAccount:gmail-api-push@system.gserviceaccount.com \
--role=roles/pubsub.publisher
Terminal window
gog gmail watch start \
--account openclaw@gmail.com \
--label INBOX \
--topic projects/<project-id>/topics/gog-gmail-watch

儲存輸出中的 history_id(供除錯使用)。

本地範例(使用共享 token 認證):

Terminal window
gog gmail watch serve \
--account openclaw@gmail.com \
--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 用於保護 push 端點(x-gog-token 或 ?token=)。
  • --hook-url 指向 OpenClaw 的 /hooks/gmail。
  • --include-body 和 --max-bytes 控制發送到 OpenClaw 的郵件內文片段大小。

推薦做法:openclaw webhooks gmail run 包裝了相同的流程並會自動續期 watch。

暴露處理器(進階、非官方支援)

Section titled “暴露處理器(進階、非官方支援)”

如果你需要使用非 Tailscale 的隧道,請手動串接並在 push 訂閱中使用公開 URL(無官方支援,無防護欄):

Terminal window
cloudflared tunnel --url http://127.0.0.1:8788 --no-autoupdate

使用產生的 URL 作為 push 端點:

Terminal window
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,然後執行:

Terminal window
gog gmail watch serve --verify-oidc --oidc-email <svc@...>

發送一封郵件到受監控的收件匣:

Terminal window
gog gmail send \
--account openclaw@gmail.com \
--to openclaw@gmail.com \
--subject "watch test" \
--body "ping"

檢查 watch 狀態與歷史紀錄:

Terminal window
gog gmail watch status --account openclaw@gmail.com
gog gmail history --account openclaw@gmail.com --since <historyId>
  • Invalid topicName:專案不匹配(topic 不在 OAuth client 專案中)。
  • User not authorized:topic 缺少 roles/pubsub.publisher 權限。
  • 訊息內容為空:Gmail push 僅提供 historyId;需透過 gog gmail history 獲取內容。
Terminal window
gog gmail watch stop --account openclaw@gmail.com
gcloud pubsub subscriptions delete gog-gmail-watch-push
gcloud pubsub topics delete gog-gmail-watch

有任何設定上的問題嗎?隨時詢問 AI Setup Assistant。

OpenClaw

OpenClaw Expert

還是卡住了?

如果這篇文件沒有解決你的情境,直接問 OpenClaw Expert,拿到可執行步驟。