Files
conversation-history/SquareMCP/2026-06-11-whatsapp-group-messaging-spec.md

9.1 KiB
Raw Permalink Blame History

WhatsApp Group Messaging — Engineering Spec (Conversations API)

Date: 2026-06-11 Project: SquareMCP / hermes-mcp Status: PROPOSED — needs eng review (/plan-eng-review) before implementation Related: 2026-06-11-whatsapp-63024-and-group-messaging-investigation, 2026-06-10-twilio-whatsapp-inbound-webhook, 2026-06-04-marketing-intelligence-lodge-first

Handoff note: this spec is written for a separate engineering agent to pick up and run through /plan-eng-review. It assumes familiarity with the existing 1:1 Twilio WhatsApp webhook (2026-06-10-twilio-whatsapp-inbound-webhook).


Problem / goal

The lodge-first launch wants the fraternal network ("the lodge" / masonic group) to interact with SquareMCP via a shared WhatsApp thread — weekly draft approvals, broadcasts, and the "onboard in the room" flow (TODO #5: a lodge member texts the SquareMCP number at the July meeting and gets auto-provisioned).

Constraint confirmed 2026-06-11: WhatsApp deprecated the native Groups API in 2020. A Twilio/Meta Business Platform number (+1 954-738-5805) cannot be added as a participant in an existing WhatsApp group chat the way a personal number can. The only Twilio-supported "group-like" mechanism is the Conversations API — one WhatsApp sender fans out to up to 50 participants in a Twilio-managed Conversation (architecturally distinct from a native WhatsApp group).


Decision needed (pick one before building)

  1. Shared Conversation — build on Twilio Conversations API. All lodge members are participants in one Twilio-managed thread; messages relay through +1 954-738-5805 to everyone. Real shared context, but new infrastructure (see below).
  2. Fan-out over existing 1:1 infra — no Conversations API. Each lodge member opts in 1:1 (as already done for Alex), and the weekly draft cron sends the same batch to a list of opted-in numbers. Approvals stay 1:1 per person. Near-zero new code — extends 2026-06-10-twilio-whatsapp-inbound-webhook with a participant list instead of a single pilot_owner_phone.

Recommendation: do the spike in "Open questions #1" below before committing to option 1. If Meta still requires each participant to message first regardless of Conversations API (likely), option 2 delivers most of the value for a fraction of the cost. The rest of this spec covers option 1 in full, since that's the harder case; option 2 is a strict subset (skip the Conversations sections, just generalize pilot_owner_phone to a list).


Architecture overview (Option 1 — Conversations API)

  • One Twilio Conversation per lodge (e.g. "Lodge Brothers Group"), created via the Conversations REST API, with +1 954-738-5805 bound as the WhatsApp proxy address for that Conversation's Conversations Service.
  • Lodge members are added as whatsapp:+E164 Participants. Each participant must independently complete WhatsApp opt-in (message +1 954-738-5805 first, or accept a pre-approved template-based invite) — this is a Meta platform rule, not a Twilio config option. Confirm exact mechanics in the spike.
  • New inbound webhook, separate from the existing 1:1 one: POST /webhook/twilio/conversations, configured at the Conversations Service level for onMessageAdded (and optionally onParticipantAdded).
  • New data model:
    • conversations (conversation_sid PK, label, customer_id/tenant grouping, created_at)
    • conversation_participants (conversation_sid, phone_e164, display_name, joined_at, opted_in_at)
  • Approval handler is generalized to resolve message.from against conversation_participants (multi-sender allowlist) instead of a single pilot_owner_phone.

New files

  • src/webhooks/twilio-conversations.ts — signature validation, event normalization, participant resolution
  • src/conversations/conversations-client.ts — thin wrapper over Twilio Conversations REST (create conversation, add/remove participant, send message into conversation)
  • src/conversations/schema.sql (or migration, follow 2026-06-09-mysql-backed-tracking-links migration style) — conversations, conversation_participants tables
  • src/webhooks/twilio-conversations.test.ts

Route registration in src/index.ts stays a single thin app.post line, per existing house style.

Endpoint details

POST /webhook/twilio/conversations — Twilio Conversations posts application/x-www-form-urlencoded events with ConversationSid, EventType (onMessageAdded, onParticipantAdded, ...), Author (whatsapp:+E164 for the sender), Body, Index (message sequence number), etc.

Apply the same fail-closed signature validation pattern as 2026-06-10-twilio-whatsapp-inbound-webhook:

  • X-Twilio-Signature validation against PUBLIC_WEBHOOK_BASE_URL + path + sorted params
  • TWILIO_AUTH_TOKEN unset → 503, do not process
  • Length guard before crypto.timingSafeEqual
  • Reply 200 fast, process async

Participant onboarding flow (TODO #5 tie-in)

  1. Lodge member texts +1 954-738-5805 (1:1, lands on the existing /webhook/twilio/whatsapp).
  2. Handler checks conversation_participants for this phone_e164 + the lodge conversation_sid. If not present and the message matches an opt-in trigger (any message counts as opt-in, or a specific keyword — TBD), call conversations-client to add them as a Participant and record opted_in_at = now().
  3. Reply confirms: "You're in — weekly drafts will post here going forward."
  4. Provisioning as a pilot tenant (billing/customer record) is a separate step from conversation membership — link to whatever tenant-onboarding flow exists when this is implemented; not specced here.

Approval semantics for group context

  • Sender allowlist: any row in conversation_participants with opted_in_at IS NOT NULL for the relevant conversation_sid, replacing the single-owner pilot_owner_phone check from the 1:1 flow.
  • Idempotency: dedupe on Conversations Index (or MessageSid), same Redis SET NX EX 86400 pattern as the 1:1 handler.
  • Command parsing ("1"/"2"/"3"): open product question — first valid reply from any participant wins (recommended default), vs. designated-approver-only, vs. majority vote. Whichever is chosen, broadcast a confirmation into the conversation so the rest of the group sees it was handled (avoids duplicate approvals).
  • Publish confirmation: send the tracked link back into the conversation (visible to everyone), not 1:1 as in the Alex flow.

Env additions

  • TWILIO_CONVERSATIONS_SERVICE_SID
  • LODGE_CONVERSATION_SID (or resolve by label from the conversations table once seeded — preferred for multi-tenant cleanliness)
  • Reuses TWILIO_AUTH_TOKEN, PUBLIC_WEBHOOK_BASE_URL from the existing 1:1 setup

Tests (mirror twilio-whatsapp.test.ts style)

  • Valid / invalid / missing / malformed signature on /webhook/twilio/conversations → 200 / 403 / 403 / 403 (no 500)
  • TWILIO_AUTH_TOKEN unset → 503, nothing processed
  • onMessageAdded from a known opted-in participant with "1" → approval handler fires, tracked link created, confirmation broadcast to conversation
  • onMessageAdded from a participant not in conversation_participants → ignored
  • Duplicate Index/MessageSid → second delivery ignored
  • onParticipantAdded → row created/updated in conversation_participants
  • 1:1 opt-in flow: message to +1 954-738-5805 from a new number with opt-in trigger → Conversations API addParticipant called, row inserted with opted_in_at set, confirmation reply sent
  • Approval race: two participants reply "1" near-simultaneously → only one publish, second gets "already handled" reply

Out of scope

  • Per-participant template approval mechanics for business-initiated adds (Meta gray area — resolve in spike, see below)
  • New-tenant provisioning triggered by conversation membership
  • Any change to the existing 1:1 Alex flow — it stays as-is; group support is additive

Effort estimate

Based on the 1:1 webhook precedent (~1 day per 2026-06-10-twilio-whatsapp-inbound-webhook):

Piece Estimate
Conversations client + schema/migration 0.5 day
New webhook route + signature validation + normalization 0.5 day
Participant onboarding flow (extends 1:1 webhook) 0.5 day
Group-aware approval handler 0.5–1 day
Tests 0.5 day
Total ~2.5–3 days

Plus a recommended 0.5-day spike up front (see below) — if it kills option 1, most of this spec becomes moot and the actual build is closer to a few hours (generalize pilot_owner_phone to a list in the existing 1:1 handler).

Open questions for the engineering agent / product

  1. Spike first: can a number be added as a Conversations Participant and receive messages before it has messaged +1 954-738-5805 (i.e., does Twilio/Meta allow business-initiated participant adds via template, or must every participant message first regardless)? This determines whether option 1 buys anything over option 2.
  2. Approval policy for group replies — first-reply-wins, designated approver, or majority?
  3. Is "the lodge" a single shared conversation, or per-sub-group (e.g. one per regional chapter)? Affects whether conversations needs to support many rows from day one or just one.