From 7fc0c27f17e920772b9b03f1bf0bfefd6a97f570 Mon Sep 17 00:00:00 2001 From: ngqile Date: Wed, 2 Dec 2026 11:56:45 +0800 Subject: [PATCH] docs(whatsapp): deployment steps for the inbox and Meta setup Adds the service image, the SQL, the WhatsApp variables shared by mobile-api and service-whatsapp, the webhook route, the Meta console steps, the role pairing and the go-live limits to docs/deployment.md. Ticks 5.1, 5.2 and 6.1 with the live-check results. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs --- docs/deployment.md | 80 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/docs/deployment.md b/docs/deployment.md index 0e19bda..742d1c6 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -19,6 +19,7 @@ CI (`.gitea/workflows/ci.yml`) builds every module on each push to `main`, runs | `co-lite-service-member` | `service-member` | 80 | No | | `co-lite-service-log` | `service-log` | 80 | No | | `co-lite-service-email` | `service-email` | 80 | No | +| `co-lite-service-whatsapp` | `service-whatsapp` | 80 | No (Meta reaches mobile-api's webhook, section 8) | The Service names matter: - The gateway routes to `http://service-auth`, `http://service-system` and so on, on port 80. The names are fixed in its config. @@ -38,12 +39,13 @@ The Service names matter: 2. Create the Pub/Sub topics and the service-account key (section 5). 3. Create the Secrets and mounted files (sections 6 and 7). 4. Deploy, in this order: - 1. `service-system`, `service-auth`, `service-counter`, `service-member`, `service-log`, `service-email`; + 1. `service-system`, `service-auth`, `service-counter`, `service-member`, `service-log`, `service-email`, `service-whatsapp`; 2. `api-gateway`; 3. `mobile-api`; 4. `admin-portal`. -5. Add the Traefik route (section 8). -6. Sign in as the first admin at `https://co-lite.containerverse.dev` and set the real password. Then create the staff users under Users. The admin types each user's first password and passes it on in person. Each user must choose their own password at first sign-in. No email is needed. +5. Add the Traefik routes (section 8). +6. Connect WhatsApp in Meta (section 11). This can wait: until it is done, the inbox says "WhatsApp is not configured". +7. Sign in as the first admin at `https://co-lite.containerverse.dev` and set the real password. Then create the staff users under Users. The admin types each user's first password and passes it on in person. Each user must choose their own password at first sign-in. No email is needed. ## 3. Database (for the DBA) @@ -125,10 +127,17 @@ Run it when the new services-system and mobile-api go live. Staff who are signed Run both, in order, before the new images go live: the new services-counter reads the new columns on every event and event lead query and fails without them. `01` needs a user that may alter tables. If `01` stops partway, check which columns already exist before running it again: only its `CREATE TABLE` is safe to repeat. `99-rollback.sql` in the same folder undoes them and deletes the guest lists and attendance times. +| File | What it does | +|---|---| +| `openspec/changes/archive/-add-whatsapp-inbox/sql/01-whatsapp-schema.sql` | Creates the schema `service-whatsapp` with the conversation, message, read-marker and template tables. DDL: run it with a user that may create databases and tables. | +| `openspec/changes/archive/-add-whatsapp-inbox/sql/02-whatsapp-modules.sql` | Adds the WhatsApp modules `BO_WHATSAPP` (the inbox) and `BO_WHATSAPP_ADMIN` (every chat of the company, assigning owners, template sync). No role gets them; super admins grant them under Roles. | + +Run both before `service-whatsapp` starts. Give the services' database user the same rights on `service-whatsapp` as on the other schemas. `99-rollback.sql` in the same folder removes the modules and drops the schema with every stored chat. + **Review the privacy notice before go-live.** The seeded text is a starting template written for the PDPA, not legal advice. INS and CV should each have theirs checked; a super admin changes it under **Companies → Privacy notice**. A company with no notice has no working QR codes. **Database user:** -- Create one user with `SELECT, INSERT, UPDATE, DELETE, EXECUTE` on the five schemas. EXECUTE is needed: service-member calls a stored function. +- Create one user with `SELECT, INSERT, UPDATE, DELETE, EXECUTE` on the five schemas, and on `service-whatsapp` once that exists. EXECUTE is needed: service-member calls a stored function. - Every Java service reads the same variables (section 6): `MYSQLHOST`, `MYSQLPORT`, `MYSQLUSER`, `MYSQLPWD`. - Each service connects to its own schema. @@ -207,6 +216,20 @@ The keys: | `CUSTOMER_PORTAL_BASE_URL` | Plain | The customer portal is not deployed in phase 1. When unset, it falls back to a Tastefully address that only the member referral link uses, which Co-Lite does not use. Set it once a customer portal exists. | | `ADMIN_PORTAL_BASE_URL` | Plain | Required. The public address of the admin portal, with no trailing slash (for example `https://admin.example.com`). Invite emails link to `/i/`. Links that staff copy or print in the portal (guest links, event and personal codes, the check-in code) use the address the staff member has open, so keep the two the same. mobile-api does not start without it. | +**`mobile-api` and `service-whatsapp`, for WhatsApp.** Both read the same variables; set them identically on both. Leave `WHATSAPP_COMPANIES` unset to keep WhatsApp off. A company whose four values are not all set is skipped with a warning in the log. + +| Variable | Kind | Value | +|---|---|---| +| `WHATSAPP_COMPANIES` | Plain | The company codes that use WhatsApp, comma-separated, e.g. `INS,CV`. Each must match a company code in Co-Lite. | +| `WHATSAPP__PHONE_NUMBER_ID` | Plain | The company's WhatsApp phone number id from Meta (WhatsApp, API Setup) | +| `WHATSAPP__WABA_ID` | Plain | The company's WhatsApp Business Account id | +| `WHATSAPP__ACCESS_TOKEN` | Secret | A permanent System User token with `whatsapp_business_messaging` and `whatsapp_business_management` on that account. Not the 24-hour test token. | +| `WHATSAPP__APP_SECRET` | Secret | The Meta app's App Secret (App settings, Basic). It checks Meta's signature on every webhook call. Two companies in one Meta app share it. | +| `WHATSAPP_VERIFY_TOKEN` | Secret | Random, at least 32 characters. Typed into Meta once, when the webhook is set up. | +| `WHATSAPP_GRAPH_BASE_URL` | Plain | Optional (default `https://graph.facebook.com/v21.0`). Only `service-whatsapp` reads it. | + +`service-whatsapp` also reads `MYSQLHOST`, `MYSQLPORT`, `MYSQLUSER` and `MYSQLPWD`. It uses no Pub/Sub, Redis or `key.json`. mobile-api reads the secrets on each request, so a rotated secret needs no restart there; `service-whatsapp` reads them at startup. + **`service-log`:** | Variable | Kind | Value | @@ -259,8 +282,12 @@ Generate each random value separately, for example with `openssl rand -base64 48 Route `co-lite.containerverse.dev` to the `admin-portal` Service on port 3000, with TLS. Only the admin portal is public: the browser talks to the portal, and the portal talks to `mobile-api` inside the cluster. Do not expose `mobile-api`, `api-gateway` or the services. +**WhatsApp webhook.** Meta must reach exactly one mobile-api path from the internet. Add a second route on the same host, for the path `/api/public/whatsapp/webhook` only (Traefik rule `Host(\`co-lite.containerverse.dev\`) && Path(\`/api/public/whatsapp/webhook\`)`), to the `mobile-api` Service on port 80. Expose no other mobile-api path. Every call on it is checked: a GET needs the verify token, and a POST needs Meta's signature under one of the configured app secrets (mobile-api checks it, and `service-whatsapp` checks it again). + QR registration pages (`/r/`) are served by the admin portal on the same host, so nothing new is routed. mobile-api rate-limits visitors by the address the portal sends: the last entry of `X-Forwarded-For`, which is the one Traefik adds. That works whether Traefik appends to or replaces the header. If another proxy or load balancer sits in front of Traefik, the last entry is that proxy's address and every visitor shares one limit; the portal's `clientIp` would then have to change (or use the PROXY protocol). If the portal sees no `X-Forwarded-For` and no `X-Real-Ip` at all, every visitor shares the portal pod's limit. +**Public pages on their own hostname.** The owner wants the public pages (`/r`, `/i`, `/c`) served on their own hostname, for example `go.`, so that printed QR links survive a later move to the customer portal. Route that hostname to the same `admin-portal` Service with TLS, and set `ADMIN_PORTAL_BASE_URL` to the address the invitation links should carry. + ## 9. Secret templates There is no template yet for `service-email` (its keys wait for the email provider) or `service-system` (its Jumio keys stay unset). Kubernetes Secrets hold base64 values. Encode each one with `printf '%s' 'value' | base64 -w0`. These are templates: replace every `<…>` and never commit the filled-in files. @@ -306,6 +333,19 @@ data: --- apiVersion: v1 kind: Secret +metadata: + name: co-lite-whatsapp +type: Opaque +data: + # mounted into both mobile-api and service-whatsapp; one ACCESS_TOKEN and APP_SECRET pair per company code + WHATSAPP_INS_ACCESS_TOKEN: + WHATSAPP_INS_APP_SECRET: + WHATSAPP_CV_ACCESS_TOKEN: + WHATSAPP_CV_APP_SECRET: + WHATSAPP_VERIFY_TOKEN: +--- +apiVersion: v1 +kind: Secret metadata: name: co-lite-gateway-appkeys type: Opaque @@ -337,3 +377,35 @@ data: - **Other Tastefully defaults.** The sender addresses in `service-email` and the `CUSTOMER_PORTAL_BASE_URL` fallback point at Tastefully. Override them as described in section 6. - **Leaked SMS key.** An SMS360 API key from Tastefully was in `service-email`'s config and is in git history. It has been removed from the config, but it must be rotated with SMS360 before go-live, by whoever owns that Tastefully account. - **Deploy by SHA, not `latest`.** CI pushes the images one by one, so if a push fails halfway, `latest` can differ between images. +- **WhatsApp roles.** Staff need `BO_WHATSAPP` to see the inbox and their own chats. A WhatsApp admin needs both `BO_WHATSAPP` and `BO_WHATSAPP_ADMIN`: the menu entry and the page need the first, and the second adds every chat of the company, owner changes and template sync. A chat's owner is set automatically to the salesperson of the newest lead with that mobile number; an admin can set an owner by hand, which sticks until "Automatic" is chosen again. A user who is disabled or loses `BO_WHATSAPP` loses their chats to Unassigned. +- **WhatsApp limits at go-live.** + - Lists and chats refresh by polling (30 s for the list and the badge, 10 s for an open chat), not instantly. + - Meta keeps media for about 30 days; older photos and documents show "Media expired". + - The chat list shows the contact's WhatsApp name and number, not the lead's name; the chat itself links to the lead. + - For a WhatsApp admin the sidebar badge counts unread messages in every chat they can see, not only their own. + - A super admin who syncs templates with no company picked gets nothing back; pick the company first (the inbox does this by itself when syncing from a chat). + - A send that Meta accepts without a message id is stored as failed. A send that never gets an answer stays "Sending"; nothing sweeps it. + - Media is streamed with `;charset=UTF-8` added to its content type; browsers ignore it. + - Meta's media download address is not checked to be a Meta host before the access token is sent with it. +- **WhatsApp webhook and outages.** If `service-whatsapp` is down or answers with an error, the webhook answers 503 and Meta sends the event again, for up to 7 days; a message is never stored twice. The same happens right after `service-whatsapp` starts while `service-system` is unreachable. Keep `service-system` up before `service-whatsapp` (section 2). +- **Client addresses for rate limits.** mobile-api takes a caller's address for its rate limits from the first entry of `X-Forwarded-For` when the header is present (`HttpReqRespUtils` in core-libs). On the webhook route, a caller can set that header itself and so pick its own limit bucket. The webhook still rejects anything Meta did not sign, so this only weakens the flood limit (6000 calls per 10 minutes per address). Check that Traefik replaces an `X-Forwarded-For` sent by an untrusted caller instead of passing it on (`forwardedHeaders.insecure` off, `trustedIPs` limited to real proxies). + +## 11. Connecting WhatsApp (Meta) + +Do this per company, after the services run with the variables of section 6. A Meta admin of the company's WhatsApp Business Account does it in Meta's developer console. + +1. **Number and account.** In the Meta app, add the WhatsApp product and the company's business number. Note the phone number id and the WhatsApp Business Account id (WhatsApp, API Setup) for `WHATSAPP__PHONE_NUMBER_ID` and `_WABA_ID`. +2. **Token.** In Business Settings, create a System User, give it the WhatsApp account and the app, and generate a token that does not expire, with `whatsapp_business_messaging` and `whatsapp_business_management`. That is `WHATSAPP__ACCESS_TOKEN`. +3. **App secret.** App settings, Basic, App Secret: `WHATSAPP__APP_SECRET`. +4. **Webhook.** WhatsApp, Configuration, Webhook: + - Callback URL: `https://co-lite.containerverse.dev/api/public/whatsapp/webhook` (the route of section 8); + - Verify token: the value of `WHATSAPP_VERIFY_TOKEN`; + - Verify and save. Meta calls the URL once; mobile-api answers only when the token matches. + - Under Webhook fields, subscribe to `messages`. That one field carries incoming messages and the sent, delivered, read and failed updates. + + If INS and CV use two Meta apps, set the same callback URL and verify token in both. mobile-api accepts a call signed with either app's secret. +5. **Templates.** Create the message templates in WhatsApp Manager and wait for Meta to approve them. Only approved templates appear in Co-Lite; a template Co-Lite cannot fill in (a media header or a header with a parameter, buttons with a parameter or copy-code buttons, named parameters) is listed but cannot be sent. Use numbered body parameters (`{{1}}`, `{{2}}`) only. Co-Lite syncs them nightly at 03:30; a WhatsApp admin can sync at once with **Sync from Meta** in the template picker. +6. **Roles.** Grant `BO_WHATSAPP` to the sales roles, and `BO_WHATSAPP` with `BO_WHATSAPP_ADMIN` to the roles that manage the inbox (section 10). +7. **First real message.** From a personal phone, send a WhatsApp message to the company's number. It must appear under WhatsApp within 30 seconds, in the right company only. Reply from Co-Lite and check that it arrives. Outside 24 hours after the customer's last message, Meta allows only templates; Co-Lite then offers only the template picker. + +If nothing arrives: the mobile-api log says `WhatsApp webhook forward failed: ` when it could not hand an event on, and Meta's webhook page shows failed deliveries. A 401 there means the app secret does not match the app that sends.