Gmail Pub/Sub -> OpenClaw
目標:Gmail watch -> Pub/Sub 推播 -> gog gmail watch serve -> OpenClaw webhook。
前置需求
gcloud已安裝並登入(安裝指南)。gog(gogcli)已安裝並對 Gmail 帳戶完成授權(gogcli.sh)。- OpenClaw hooks 已啟用(參閱 Webhooks)。
tailscale已登入(tailscale.com)。支援的設定使用 Tailscale Funnel 作為公開的 HTTPS 端點。 其他隧道服務可以運作,但屬於自行處理/不受支援的範疇,需要手動配線。 目前我們支援的是 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 hook 設定預設模型和思考層級,在設定中加入 hooks.gmail.model / hooks.gmail.thinking:
{
hooks: {
gmail: {
model: "openrouter/meta-llama/llama-3.3-70b-instruct:free",
thinking: "off",
},
},
}
說明:
- 對應中的個別
model/thinking仍然會覆寫這些預設值。 - 退回順序:
hooks.gmail.model->agents.defaults.model.fallbacks-> 主要模型(驗證/速率限制/逾時)。 - 如果設定了
agents.defaults.models,Gmail 模型必須在允許清單中。 - Gmail hook 內容預設會加上外部內容安全邊界包裝。
若要停用(危險),設定
hooks.gmail.allowUnsafeExternalContent: true。
若要進一步自訂 payload 處理,可新增 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 主題與 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 隧道,請手動配線並在推播訂閱中使用公開 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:專案不匹配(主題不在 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