Integrating RedwoodJS with WhatsApp via AWS SNS - code-examples -

Frequently Asked Questions

Integrate WhatsApp with RedwoodJS using AWS SNS and AWS End User Messaging Social. This involves setting up a RedwoodJS API function as a webhook endpoint to receive incoming WhatsApp messages and send replies via the AWS SDK.
AWS End User Messaging Social is an AWS service that connects your WhatsApp Business Account (WABA) to your AWS account, enabling seamless integration with other AWS services like SNS for message handling.
AWS SNS acts as a pub/sub messaging service, allowing AWS End User Messaging Social to publish incoming WhatsApp messages to an SNS topic, which your RedwoodJS application can subscribe to for real-time message processing.
Use pre-approved WhatsApp Message Templates when replying to users outside the 24-hour free-form message window. Within 24 hours of a user's last message, you can send free-form text replies.
Yes, you can store WhatsApp message details in a database using Prisma, RedwoodJS's default ORM. Define a schema in `schema.prisma` and use the Prisma client in your API function to save incoming and outgoing message data.
Create a RedwoodJS API function (e.g., `whatsappWebhook`) and deploy it to a public HTTPS URL. Subscribe this URL to your AWS SNS topic, which receives messages from AWS End User Messaging Social linked to your WhatsApp Business Account.
WhatsApp allows free-form text replies only within 24 hours of a user's last message. Outside this window, use pre-approved Message Templates, which have a different structure within the AWS Social Post Messaging API.
Within your RedwoodJS webhook function, validate that the `TopicArn` in the `SubscriptionConfirmation` message matches your expected ARN, then use the AWS SDK's `ConfirmSubscriptionCommand` to programmatically confirm the subscription.
Zod is used for validating the incoming WhatsApp payload structure within the RedwoodJS function. It ensures data integrity by checking against a predefined schema, protecting your app from unexpected data formats.
Use the AWS SDK for JavaScript v3, specifically the `SocialPostMessagingClient` (or similar) and `SendWhatsappMessageCommand` with the correct parameters to send replies from your RedwoodJS API function to users' WhatsApp numbers.
Your RedwoodJS function needs AWS credentials to interact with AWS services (SNS, End User Messaging Social, etc.). Securely manage these using environment variables, IAM roles (recommended for production), or secrets management tools.
SNS signature verification validates that incoming webhook requests originate from AWS SNS, ensuring security. Use the `aws-sns-verify` library in your RedwoodJS function to verify the signature before processing any message data.
A DLQ (SQS queue) captures messages that SNS fails to deliver to your RedwoodJS webhook after multiple retries. This enables inspecting and potentially reprocessing failed messages, preventing data loss.
Implement `try...catch` blocks and logging for robust error handling. Return appropriate HTTP status codes (e.g., 403 for invalid signature, 400 for bad payload, 500 for processing errors or failed subscription confirmation to allow retries) to manage SNS retries and DLQ routing.