Implementing Plivo Inbound SMS and Two-Way Messaging with NestJS - code-examples -

Frequently Asked Questions

Receive SMS messages in NestJS by setting up a webhook endpoint using Plivo. Configure Plivo to forward incoming messages to this endpoint, which will then process them using the Plivo Node.js SDK. This enables two-way messaging and allows your application to react to incoming SMS.
Plivo is a cloud communications platform that provides SMS and Voice APIs, used in this NestJS application to handle inbound and outbound SMS messages. The Plivo Node.js SDK helps to build the XML responses required by Plivo and also to send outgoing messages if needed.
NestJS provides a structured, scalable, and efficient framework for building server-side applications, making it well-suited for production SMS APIs. Features like dependency injection, modularity, and TypeScript support enhance code maintainability and reliability.
ngrok is essential for local development with Plivo webhooks. Since Plivo needs a publicly accessible URL, ngrok creates a tunnel to expose your local server, allowing Plivo to reach your application during testing before deploying to a live server.
Create a dedicated controller with a POST route (e.g., '/plivo-webhook/message') in your NestJS application. Configure this URL as the 'Message URL' in your Plivo application settings. This endpoint will then receive incoming SMS messages from Plivo.
Use the PlivoSignatureGuard with the '@UseGuards' decorator on your webhook route. This guard utilizes the 'plivo.validateV3Signature' function and the 'PLIVO_AUTH_TOKEN' to verify that requests genuinely originate from Plivo and haven't been tampered with.
The Plivo MessageUUID is a unique identifier assigned to each incoming message by Plivo. Use this UUID for logging, tracking, and potentially storing messages in a database to avoid duplicates and maintain message history.
Handle 'STOP' keywords by checking the incoming message text. Implement logic to unsubscribe the user, potentially updating your database. The Plivo platform may also offer automatic opt-out management via Compliance settings.
Use the 'plivo.Response' object to construct XML containing '' elements. Set 'src' to your Plivo number and 'dst' to the sender's number. Return this XML from your webhook handler; Plivo will process it and send the reply.
Create a '.env' file in your project root to store 'PLIVO_AUTH_ID', 'PLIVO_AUTH_TOKEN', and 'PLIVO_PHONE_NUMBER'. Import and configure the '@nestjs/config' module in your app.module.ts to load these variables securely. Never commit the '.env' file.
The 'rawBody' option in NestFactory.create is crucial for Plivo signature validation. It ensures the guard receives the exact, unmodified request body bytes needed by 'plivo.validateV3Signature', which is essential for security.
For MMS messages, look for 'Type=mms' in the webhook payload. Access media via the provided 'Media_URL' parameters. Note that these URLs are temporary and may require authentication for download.
Use ngrok to expose your local development server. Configure your Plivo application's Message URL to point to the ngrok HTTPS URL plus your webhook route. Send a real SMS to your Plivo number to trigger the webhook.
Plivo signature validation can fail due to several reasons, including an incorrect 'PLIVO_AUTH_TOKEN', a mismatch between the constructed URL and the actual URL called by Plivo, especially when using a proxy, or most commonly issues with the raw body not containing the original request bytes.