Integrating WhatsApp with RedwoodJS using MessageBird - code-examples -

Frequently Asked Questions

You can send WhatsApp messages within your RedwoodJS application by integrating with the MessageBird API. This involves setting up a MessageBird account, configuring a WhatsApp channel, and then using the MessageBird Node.js SDK within your RedwoodJS services to send messages programmatically. This guide provides step-by-step instructions for setting up this integration and leveraging RedwoodJS functions and services.
MessageBird acts as a Communication Platform as a Service (CPaaS) provider, simplifying the complexities of the WhatsApp Business API. It provides the necessary infrastructure and API for sending and receiving WhatsApp messages, including template messages (HSM), through a unified interface that can be accessed through its Node.js SDK and REST API from your RedwoodJS application.
MessageBird simplifies WhatsApp integration by handling the complexities of the WhatsApp Business API. Combined with RedwoodJS, it offers a structured, serverless approach for building robust WhatsApp integrations with features like database message logging and secure handling of credentials, all within a full-stack JavaScript environment.
Incoming WhatsApp messages are handled using MessageBird webhooks. Set up a dedicated RedwoodJS function as your webhook endpoint and configure this URL within your MessageBird dashboard. Ensure proper signature verification for security. This function will receive message data and status updates and should be used to update your database or trigger other logic within your application.
Integrate the WhatsApp Business API by using MessageBird as an intermediary. You'll need a MessageBird account, an approved WhatsApp Business number, and the MessageBird Node.js SDK installed in your RedwoodJS API side. Configure your RedwoodJS services to make API calls to MessageBird, enabling your RedwoodJS application to send and receive WhatsApp messages.
The guide recommends PostgreSQL or a similar relational database to store message logs. Prisma, an ORM used by RedwoodJS, handles database interactions and migrations. This enables efficient storage and retrieval of message history, metadata, and delivery status updates.
`ngrok` is useful during local development for testing your MessageBird webhook endpoint. It creates a public HTTPS tunnel to your local server, allowing MessageBird to deliver webhook events to your development environment. You will need to replace the ngrok URL with your application URL after deployment.
The webhook signing key is critical for security. This secret key, generated by you and configured within MessageBird, allows your webhook handler to verify the authenticity of incoming webhook requests. This prevents unauthorized actors from sending fake events to your application. It’s crucial to keep this key secure.
Prisma serves as the Object-Relational Mapper (ORM) in the RedwoodJS application. It simplifies database interactions, allowing you to define your data models (like MessageLog) and perform database operations (create, read, update, delete) using JavaScript instead of raw SQL. Prisma migrations also manage database schema changes.
RedwoodJS uses `.env` files for environment variables. Store your `MESSAGEBIRD_API_KEY`, `MESSAGEBIRD_WHATSAPP_CHANNEL_ID`, and `MESSAGEBIRD_WEBHOOK_SIGNING_KEY` in a `.env` file in your project root. Ensure this file is added to your `.gitignore` to avoid exposing sensitive information in version control.
The provided `sendWhatsappMessage` service handles both 'text' messages and 'hsm' (Highly Structured Messages, also known as Template Messages). Ensure your 'content' payload matches the MessageBird API specification for each type. 'text' messages require a 'text' field, while 'hsm' messages require template details like namespace, templateName, and parameters.
The RedwoodJS service encapsulates the logic for interacting with the MessageBird API. This includes sending messages, handling different message types (text, HSM), logging messages to the database, and managing API errors. It helps keep your API routes clean and your business logic centralized.
Yes, you can send template messages (HSM) with this integration. Ensure you have pre-approved templates configured in your MessageBird account. When calling the `sendWhatsappMessage` service, set the `type` to 'hsm' and provide the required template details and parameters in the `content.hsm` object.
A typical structure uses Redwood's functions to handle API requests and webhooks, services to interact with MessageBird's API and the database, and Prisma models to represent message logs. The 'api' directory houses backend code, including the MessageBird SDK. The 'web' directory handles the frontend. Your database, accessed via Prisma, stores message history and metadata.