|
| 1 | +--- |
| 2 | +summary: "Gmail Pub/Sub push wired into Clawdis webhooks via gogcli" |
| 3 | +read_when: |
| 4 | + - Wiring Gmail inbox triggers to Clawdis |
| 5 | + - Setting up Pub/Sub push for agent wake |
| 6 | +--- |
| 7 | + |
| 8 | +# Gmail Pub/Sub -> Clawdis |
| 9 | + |
| 10 | +Goal: Gmail watch -> Pub/Sub push -> `gog gmail watch serve` -> Clawdis webhook. |
| 11 | + |
| 12 | +## Prereqs |
| 13 | + |
| 14 | +- `gcloud` installed and logged in. |
| 15 | +- `gog` (gogcli) installed and authorized for the Gmail account. |
| 16 | +- Clawdis hooks enabled (see `docs/webhook.md`). |
| 17 | +- `tailscale` logged in if you want a public HTTPS endpoint via Funnel. |
| 18 | + |
| 19 | +Example hook config (enable Gmail preset mapping): |
| 20 | + |
| 21 | +```json5 |
| 22 | +{ |
| 23 | + hooks: { |
| 24 | + enabled: true, |
| 25 | + token: "CLAWDIS_HOOK_TOKEN", |
| 26 | + path: "/hooks", |
| 27 | + presets: ["gmail"] |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +To customize payload handling, add `hooks.mappings` or a JS/TS transform module |
| 33 | +under `hooks.transformsDir` (see `docs/webhook.md`). |
| 34 | + |
| 35 | +## Wizard (recommended) |
| 36 | + |
| 37 | +Use the Clawdis helper to wire everything together (installs deps on macOS via brew): |
| 38 | + |
| 39 | +```bash |
| 40 | +clawdis hooks gmail setup \ |
| 41 | + |
| 42 | +``` |
| 43 | + |
| 44 | +Defaults: |
| 45 | +- Uses Tailscale Funnel for the public push endpoint. |
| 46 | +- Writes `hooks.gmail` config for `clawdis hooks gmail run`. |
| 47 | +- Enables the Gmail hook preset (`hooks.presets: ["gmail"]`). |
| 48 | + |
| 49 | +Want a custom endpoint? Use `--push-endpoint <url>` or `--tailscale off`. |
| 50 | + |
| 51 | +Platform note: on macOS the wizard installs `gcloud`, `gogcli`, and `tailscale` |
| 52 | +via Homebrew; on Linux install them manually first. |
| 53 | + |
| 54 | +Run the daemon (starts `gog gmail watch serve` + auto-renew): |
| 55 | + |
| 56 | +```bash |
| 57 | +clawdis hooks gmail run |
| 58 | +``` |
| 59 | + |
| 60 | +## One-time setup |
| 61 | + |
| 62 | +1) Select the GCP project **that owns the OAuth client** used by `gog`. |
| 63 | + |
| 64 | +```bash |
| 65 | +gcloud auth login |
| 66 | +gcloud config set project <project-id> |
| 67 | +``` |
| 68 | + |
| 69 | +Note: Gmail watch requires the Pub/Sub topic to live in the same project as the OAuth client. |
| 70 | + |
| 71 | +2) Enable APIs: |
| 72 | + |
| 73 | +```bash |
| 74 | +gcloud services enable gmail.googleapis.com pubsub.googleapis.com |
| 75 | +``` |
| 76 | + |
| 77 | +3) Create a topic: |
| 78 | + |
| 79 | +```bash |
| 80 | +gcloud pubsub topics create gog-gmail-watch |
| 81 | +``` |
| 82 | + |
| 83 | +4) Allow Gmail push to publish: |
| 84 | + |
| 85 | +```bash |
| 86 | +gcloud pubsub topics add-iam-policy-binding gog-gmail-watch \ |
| 87 | + --member=serviceAccount: [email protected] \ |
| 88 | + --role=roles/pubsub.publisher |
| 89 | +``` |
| 90 | + |
| 91 | +## Start the watch |
| 92 | + |
| 93 | +```bash |
| 94 | +gog gmail watch start \ |
| 95 | + |
| 96 | + --label INBOX \ |
| 97 | + --topic projects/<project-id>/topics/gog-gmail-watch |
| 98 | +``` |
| 99 | + |
| 100 | +Save the `history_id` from the output (for debugging). |
| 101 | + |
| 102 | +## Run the push handler |
| 103 | + |
| 104 | +Local example (shared token auth): |
| 105 | + |
| 106 | +```bash |
| 107 | +gog gmail watch serve \ |
| 108 | + |
| 109 | + --bind 127.0.0.1 \ |
| 110 | + --port 8788 \ |
| 111 | + --path /gmail-pubsub \ |
| 112 | + --token <shared> \ |
| 113 | + --hook-url http://127.0.0.1:18789/hooks/gmail \ |
| 114 | + --hook-token CLAWDIS_HOOK_TOKEN \ |
| 115 | + --include-body \ |
| 116 | + --max-bytes 20000 |
| 117 | +``` |
| 118 | + |
| 119 | +Notes: |
| 120 | +- `--token` protects the push endpoint (`x-gog-token` or `?token=`). |
| 121 | +- `--hook-url` points to Clawdis `/hooks/gmail` (mapped; isolated run + summary to main). |
| 122 | +- `--include-body` and `--max-bytes` control the body snippet sent to Clawdis. |
| 123 | + |
| 124 | +Recommended: `clawdis hooks gmail run` wraps the same flow and auto-renews the watch. |
| 125 | + |
| 126 | +## Expose the handler (dev) |
| 127 | + |
| 128 | +For local testing, tunnel the handler and use the public URL in the push subscription: |
| 129 | + |
| 130 | +```bash |
| 131 | +cloudflared tunnel --url http://127.0.0.1:8788 --no-autoupdate |
| 132 | +``` |
| 133 | + |
| 134 | +Use the generated URL as the push endpoint: |
| 135 | + |
| 136 | +```bash |
| 137 | +gcloud pubsub subscriptions create gog-gmail-watch-push \ |
| 138 | + --topic gog-gmail-watch \ |
| 139 | + --push-endpoint "https://<public-url>/gmail-pubsub?token=<shared>" |
| 140 | +``` |
| 141 | + |
| 142 | +Production: use a stable HTTPS endpoint and configure Pub/Sub OIDC JWT, then run: |
| 143 | + |
| 144 | +```bash |
| 145 | +gog gmail watch serve --verify-oidc --oidc-email <svc@...> |
| 146 | +``` |
| 147 | + |
| 148 | +## Test |
| 149 | + |
| 150 | +Send a message to the watched inbox: |
| 151 | + |
| 152 | +```bash |
| 153 | +gog gmail send \ |
| 154 | + |
| 155 | + |
| 156 | + --subject "watch test" \ |
| 157 | + --body "ping" |
| 158 | +``` |
| 159 | + |
| 160 | +Check watch state and history: |
| 161 | + |
| 162 | +```bash |
| 163 | +gog gmail watch status --account [email protected] |
| 164 | +gog gmail history --account [email protected] --since <historyId > |
| 165 | +``` |
| 166 | + |
| 167 | +## Troubleshooting |
| 168 | + |
| 169 | +- `Invalid topicName`: project mismatch (topic not in the OAuth client project). |
| 170 | +- `User not authorized`: missing `roles/pubsub.publisher` on the topic. |
| 171 | +- Empty messages: Gmail push only provides `historyId`; fetch via `gog gmail history`. |
| 172 | + |
| 173 | +## Cleanup |
| 174 | + |
| 175 | +```bash |
| 176 | +gog gmail watch stop --account [email protected] |
| 177 | +gcloud pubsub subscriptions delete gog-gmail-watch-push |
| 178 | +gcloud pubsub topics delete gog-gmail-watch |
| 179 | +``` |
0 commit comments