Integrating WhatsApp with RedwoodJS and Sinch - code-examples -

Frequently Asked Questions

You can send WhatsApp messages by creating a GraphQL mutation in your RedwoodJS application that calls a service function. This service function utilizes the Sinch Conversation API and Node.js SDK to send text messages via an authenticated API endpoint. The mutation requires the recipient's phone number and the message text as input.
The Sinch Conversation API is a unified API provided by Sinch, allowing interaction with various messaging channels, including WhatsApp. It simplifies the complexities of the WhatsApp Business API and enables potential expansion to other messaging channels like SMS and RCS in the future.
Sinch provides a managed WhatsApp Business API solution, handling infrastructure and compliance. This simplifies integration compared to working directly with Meta's API. RedwoodJS offers a structured, full-stack JavaScript framework, making development and deployment easier.
Using a job queue like BullMQ, Redis SMQ, or AWS SQS is crucial for production reliability when handling incoming WhatsApp messages. A queue ensures messages aren't lost if processing fails after acknowledging receipt of the webhook from Sinch.
Yes, you can receive WhatsApp messages by setting up a webhook handler (a RedwoodJS function) to process incoming messages sent to your dedicated Sinch number. The handler parses the message content and logs it in your database.
First, install the Sinch Node.js SDK in your RedwoodJS API side. Then, configure environment variables for your Sinch Project ID, Key ID, Key Secret, App ID, WhatsApp Sender ID, and Webhook Secret. Finally, initialize a reusable Sinch client instance using these credentials in `api/src/lib/sinch.ts`.
RedwoodJS services encapsulate the core business logic of your application, separating concerns from other parts of your codebase. In this WhatsApp integration, the service handles the interaction with the Sinch SDK and database operations for storing messages.
Use the 'crypto' module's `createHmac` and `timingSafeEqual` functions to verify the signature of incoming webhook requests. Compare the signature with a hash generated using your `SINCH_WEBHOOK_SECRET` to ensure requests genuinely originate from Sinch.
The auth provider header, such as 'auth-provider: dbAuth', signifies the authentication method used in your RedwoodJS application. Its usage depends on the specific provider, like 'dbAuth', 'clerk', or 'supabase', and is needed when making authenticated requests to your GraphQL API.
The guide recommends using Node.js version 18.x or later for compatibility with the RedwoodJS framework and Sinch Node.js SDK.
The `processIncomingWhatsAppMessage` function provides basic handling for text messages. For media messages (images, videos), the provided example logs the message, however, you'll have to write custom logic for their processing in the service. Consider adding more sophisticated handling for locations and other message types in your application as needed.
The WhatsApp channel through the Sinch Conversation API requires a postpay account due to the associated costs and billing mechanisms for sending and receiving WhatsApp messages. Trial accounts typically have limitations that prevent WhatsApp integration.
The integration logs key information about incoming messages, including the sender's phone number, the message content (text or a placeholder for media), the Sinch Message ID, the Sinch Contact ID, the webhook payload and timestamps.
You can find your Sinch Project ID and Key ID in the Sinch Customer Dashboard under 'Access Keys' -> 'API Credentials'. The Key Secret is shown only once during creation, so store it securely.