Integrating Infobip WhatsApp with RedwoodJS - code-examples -

Frequently Asked Questions

Integrate Infobip WhatsApp with RedwoodJS by setting up a new RedwoodJS project, configuring Infobip credentials as environment variables, implementing a service to handle sending messages, creating a GraphQL mutation to expose the sending functionality, setting up a webhook function to receive incoming messages and delivery reports, and optionally setting up a database schema to log message interactions. This guide uses TypeScript, GraphQL, Prisma, and Node.js to achieve the integration.
`ngrok` creates a secure tunnel to your local RedwoodJS development server, making it publicly accessible for receiving webhooks from Infobip. This is necessary because Infobip needs a public URL to send real-time message and delivery report notifications to during development.
Create a RedwoodJS service function that constructs the message payload, including recipient number, template name, placeholders, and language. This function then calls the Infobip API using an HTTP client like `fetch` or `axios`. You'll need your Infobip API key, base URL, and WhatsApp sender number, all configured as environment variables.
Infobip is an official Business Solution Provider (BSP) for the WhatsApp Business API. They provide the necessary infrastructure and API access to connect your application with the WhatsApp network, enabling you to send and receive messages and manage templates.
RedwoodJS offers a robust, full-stack, serverless framework with built-in features like GraphQL and Prisma, simplifying development and deployment. Its integrated frontend/backend structure streamlines the process of connecting user interfaces with WhatsApp communication flows.
Incoming messages are forwarded from WhatsApp through the Infobip platform to a RedwoodJS function acting as a webhook. This function processes the message, potentially interacts with a database to log the message, and acknowledges receipt to Infobip. Security measures like shared secrets or signature validation protect the webhook.
The guide provides a basic security example using a shared secret passed in an HTTP header. For production, you must implement robust signature validation (e.g., HMAC) according to Infobip's documentation to prevent unauthorized access to your webhook.
You will need Node.js, Yarn, the RedwoodJS CLI, an Infobip account, a registered WhatsApp Sender number, and basic understanding of RedwoodJS, GraphQL, and TypeScript. `ngrok` is necessary for local webhook development. An active and approved template on Infobip is required to send templated messages.
`InfobipTemplatePlaceholder` is a TypeScript interface defined to enforce the structure of data required by WhatsApp message templates. It ensures that the message sent to the Infobip API contains the correct placeholders for variables like body text, buttons, and headers, which get populated with dynamic values when sending the message.
Yes, the provided code can be extended to send free-form messages. Ensure the free-form messages comply with WhatsApp Business API guidelines and consider that initiating a conversation usually requires a pre-approved message template first, followed by a 24-hour window.
Infobip sends delivery reports (sent, delivered, read, failed) to the same webhook as inbound messages. The webhook handler must distinguish between message types by analyzing the payload structure. You can then update message status in your database based on the delivery reports received.
The guide recommends defining a Prisma schema with a `MessageLog` model to store message details (sender, recipient, status, content preview). Update your service and webhook functions to create and update `MessageLog` entries when sending and receiving messages and delivery reports. Be mindful of PII when logging content.
While the example uses Node's native `fetch`, a dedicated HTTP client like `axios` might be beneficial for more complex API interactions, features like interceptors for managing auth tokens or retries, or if you're more familiar with its API.
Use the language code that aligns with your approved WhatsApp message template and your target audience's language. The guide uses 'en' for English, but change this to match your template's language, e.g., 'es' for Spanish, 'fr' for French.