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)。サポートされているセットアップは、公開 HTTPS エンドポイントに Tailscale Funnel を使用します。 他のトンネルサービスも機能しますが、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 が必要な場合は、channel + to を設定します。それ以外の場合、channel: "last" は最後の配信ルートを使用します(WhatsApp にフォールバック)。

Gmail 実行に対してより安価なモデルを強制するには、マッピングに model を設定します(provider/model またはエイリアス)。agents.defaults.models を適用する場合は、そこに含めます。

Gmail フック専用のデフォルトモデルと Thinking レベルを設定するには、設定に 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.modelagents.defaults.model.fallbacks → プライマリ(認証/レート制限/タイムアウト)。
  • agents.defaults.models が設定されている場合、Gmail モデルは許可リストに含まれている必要があります。
  • Gmail hook コンテンツはデフォルトで外部コンテンツ安全境界でラップされます。 無効化するには(危険)、hooks.gmail.allowUnsafeExternalContent: true を設定します。

ペイロード処理をさらにカスタマイズするには、hooks.mappings または hooks.transformsDir の下の 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 がプロキシする前に設定パスのプレフィックスを削除するためです。 バックエンドがプレフィックス付きパスを受信する必要がある場合は、hooks.gmail.tailscale.target(または --tailscale-target)を http://127.0.0.1:8788/gmail-pubsub のような完全な URL に設定し、hooks.gmail.serve.path と一致させます。

カスタムエンドポイントが必要ですか? --push-endpoint <url> または --tailscale off を使用します。

プラットフォームの注意: macOS では、ウィザードは Homebrew 経由で gcloudgogclitailscale をインストールします。Linux では最初に手動でインストールしてください。

Gateway 自動起動(推奨):

  • hooks.enabled=truehooks.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

ワンタイムセットアップ

  1. gog が使用する OAuth クライアントを所有する GCP プロジェクトを選択します。
gcloud auth login
gcloud config set project <project-id>

注意: Gmail watch では、Pub/Sub トピックが OAuth クライアントと同じプロジェクトに存在する必要があります。

  1. API を有効化:
gcloud services enable gmail.googleapis.com pubsub.googleapis.com
  1. トピックを作成:
gcloud pubsub topics create gog-gmail-watch
  1. 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 を保存します(デバッグ用)。

プッシュハンドラーを実行

ローカル例(共有トークン認証):

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 を指します(マッピング済み。Isolated 実行 + Main への要約)。
  • --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