Gmail PubSub
Gmail Pub/Sub -> OpenClaw
Section titled “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 摘要傳送到聊天介面,請使用設定 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→ primary (auth/rate-limit/timeouts)。 - 如果設定了
agents.defaults.models,Gmail 模型必須在允許列表中。 - Gmail hook 內容預設會以外部內容安全邊界包裝。
若要停用(危險),請設定
hooks.gmail.allowUnsafeExternalContent: true。
若要進一步自訂處理 Payload,請在 ~/.openclaw/hooks/transforms 下新增 hooks.mappings 或 JS/TS 轉換模組
(請參閱 Webhooks)。
精靈(建議)
Section titled “精靈(建議)”使用 OpenClaw 輔助工具將一切連接起來(在 macOS 上透過 brew 安裝相依套件):
openclaw webhooks gmail setup \預設值:
- 使用 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
在代理之前會移除設定的路徑前綴。
如果您需要後端接收帶有前綴的路徑,請將
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 上,請先手動安裝它們。
閘道自動啟動(建議):
- 當設定
hooks.enabled=true和hooks.gmail.account時,閘道會在開機時啟動gog gmail watch serve並自動續期監控。 - 設定
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 logingcloud config set project <project-id>注意:Gmail 監控要求 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 \ --role=roles/pubsub.publishergog gmail watch start \ --label INBOX \ --topic projects/<project-id>/topics/gog-gmail-watch儲存輸出中的 history_id(用於除錯)。
執行推播處理程式
Section titled “執行推播處理程式”本機範例(共用 Token 驗證):
gog gmail watch serve \ --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 封裝了相同的流程並自動續期監聽。
公開處理程序 (進階,不支援)
Section titled “公開處理程序 (進階,不支援)”如果您需要非 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 \ --subject "watch test" \ --body "ping"檢查監聽狀態與歷史記錄:
Invalid topicName:專案不符 (主題不在 OAuth 用戶端專案中)。User not authorized:主題上缺少roles/pubsub.publisher。- 空訊息:Gmail 推送僅提供
historyId;透過gog gmail history取得。
gcloud pubsub subscriptions delete gog-gmail-watch-pushgcloud pubsub topics delete gog-gmail-watch