WhatsApp Business API (WABA) Platform Integration¶
Overview¶
This document outlines the implementation requirements for integrating WhatsApp Business API (Cloud API) as a STATELESS platform in the Messcat platform-sdk with co-existence support.
Platform Type: STATELESS (API-based, no persistent connection)
API Docs: https://developers.secure.facebook.com/documentation/business-messaging/whatsapp/overview
TODO User IDs: https://developers.facebook.com/documentation/business-messaging/whatsapp/business-scoped-user-ids
Client¶
Use the package directly: https://github.com/gokapso/whatsapp-cloud-api-js for types and client interaction. We'll not use their proxy. Do not copy in types or API methods, use the client directly for API calls.
1. Platform Registration¶
Configuration¶
import type { StatelessPlatformRegistration } from '@messcat/platform-sdk'
export const WABusinessPlatform: StatelessPlatformRegistration = {
type: 'STATELESS',
config: {
id: 'wb',
display_name: 'WhatsApp Business',
version: '0.0.1',
sendable_id_types: ['PHONE_NUMBER'],
supports_fetching_msgs_from_primary_source: false,
supports_thread_imgs: false,
supports_presence: false,
supports_quoted_messages: true,
delete_for_everyone_time_limit_s: undefined,
edit_message_time_limit_s: undefined,
typing_duration_s: 0,
supported_assets: [{ name: 'ATTACHMENT', kind: 'STATELESS' }],
max_message_concurrency: 10,
},
migrations: { directory: './sql' },
api: WhatsAppBusinessAPI,
}
Meta App Configuration Table¶
Meta allows connecting WABAs via a Meta App, which requires storing app credentials and configuration details. We'll allow multiple Meta apps to co-exist, with a default app used for new connections.
Requires: platform-sdk sql/01.core.ddl.sql (for org_id_type domain)
Schema: msngr
SET search_path TO msngr, pgmb;
CREATE TABLE waba_config(
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
org_id org_id_type NOT NULL,
name VARCHAR(255) NOT NULL,
-- ensure only one default app per org
app_id VARCHAR(255) NOT NULL UNIQUE,
app_secret TEXT NOT NULL,
verify_token TEXT NOT NULL,
webhook_token TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
WABA Table¶
Each WABA can contain multiple phone numbers, & each platform connection corresponds to a single phone number. The phoneNumberId is used as the unique identifier for the connection.
CREATE TABLE waba_connections (
id VARCHAR(128) PRIMARY KEY, -- waba id from Meta
org_id org_id_type NOT NULL,
-- user-friendly name for the connection (e.g., "My Business WhatsApp")
-- populate from Meta
name VARCHAR(255) NOT NULL,
-- access token for API calls
access_token TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
);
2. Credentials & Metadata¶
Platform Connection Metadata¶
Each phone number connection requires storing the WhatsApp Business Phone Number ID and a reference to the WABA it belongs to. This allows us to manage multiple phone numbers under the same WABA and handle API calls correctly.
Stored in platform_connections.metadata:
interface WhatsAppBusinessMetadata {
phoneNumberId: string // WhatsApp Business Phone Number ID
// WhatsApp Business Account (WABA) ID,
// reference to WABA table for additional details
wabaId: string
}
Credentials¶
platform_connections.credentials will store an empty JSON, as credentials are managed at the WABA level (app credentials and access tokens).
4. Authentication & Setup¶
TODO
4.2 Webhook Setup¶
| Webhook Field | Purpose |
|---|---|
messages |
Receive incoming messages |
smb_message_echoes |
Messages from WhatsApp Business App (coexistence) |
messages_update |
Status updates (sent, delivered, read, failed) |
account_update |
Account status changes |
5. Message Templates¶
Templates are required to initiate conversations. Each template is scoped to a WABA, so we'll need to fetch templates for the specific WABA associated with the phone number connection.
We'll expose a Graphile plugin to manage templates, via API calls to Meta:
- Upsert a template
- Delete a template
For submitting the body of templates in our GraphQL API, we'll use the existing "MessageContent" type.
See: https://postgraphile.org/postgraphile/5/extend-schema#mutation-example See API docs: https://developers.secure.facebook.com/documentation/business-messaging/whatsapp/templates/template-management
6. Co-Existence Support¶
- Same phone number on both WhatsApp Business App and API
smb_message_echoeswebhook receives messages from the App- Track message origin to avoid duplicates
- Implement history & contact sync too.
7. File Structure¶
├── src/
│ ├── index.ts # StatelessPlatformRegistration export
│ ├── api.ts # Main API class
│ ├── routes (folder) # Webhook handler, Oauth2 callbacks (expressJS)
│ └── types (folder) # Platform-specific types
├── sql/ # SQL DDL
├── package.json
└── tsconfig.json
8. Testing¶
Mocks need to be created for Meta API calls, webhooks. We'll use the native Node.js test runner for unit and integration tests. Focus on testing:
- Webhook handling (message reception, status updates)
- Message sending
- Marking messages as read
- sending typing indicators
9. Messages¶
Messages are sent in "platform-sdk" format, see 01.core.ddl.sql for the message schema. The "original" field will store the raw WhatsApp message object as JSON, which contains all the details needed for processing and mapping to our internal format.
A single platform-sdk message may map to multiple WhatsApp messages in cases of multiple attachments.
Look at the packages/super-baileys/src/Platform/utils.ts file to see how to split a single message with multiple attachments into multiple messages for the platform-sdk format.
For template messages: if the "template_id" is populated, assume the whole message is a template message, and only map the "template_id" and "variables" to sendTemplate type msg.
Client API Guide: https://docs.kapso.ai/docs/whatsapp/typescript-sdk/messages#mark-as-read