Skip to main content

Overview

This guide shows how to conduct an SMS survey using Plivo’s APIs and Node.js. This involves two parts: sending an initial question via the REST API, and then handling the user’s reply via a webhook.

How it works

  1. Initiate Survey: Your application makes a POST request to a /send-survey/ endpoint. This triggers Plivo’s Send Message API to send the first survey question to the user.
  2. Handle Response: The user replies to the SMS. Plivo sends a webhook with their reply to a /survey-response/ endpoint. Your application processes their answer (“Yes” or “No”) and replies with a thank-you message using Plivo XML.

Prerequisites


Create the survey application

Create a file named survey.js and paste in this code. It sets up an Express server with two endpoints: one to start the survey and one to handle replies.
const express = require('express');
const plivo = require('plivo');
const app = express();
const port = 3000;

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// This endpoint triggers the initial survey question via the REST API
app.post('/send-survey/', async (req, res) => {
    const client = new plivo.Client('<auth_id>', '<auth_token>');
    try {
        const response = await client.messages.create({
            src: '<sender_id>',
            dst: '<destination_number>',
            text: 'Did you find out all the information you needed? Please reply "Yes" or "No"'
        });
        res.status(200).send(response);
    } catch (error) {
        console.error(error);
        res.status(500).send(error);
    }
});

// This endpoint handles the user's reply via a webhook
app.post('/survey-response/', (req, res) => {
    const from_number = req.body.From;
    const to_number = req.body.To;
    const text = req.body.Text;
    console.log(`Message received - From: ${from_number}, To: ${to_number}, Text: ${text}`);

    let message_body;
    if (text && text.toLowerCase() === "yes") {
        message_body = "Thank you for your feedback";
    } else if (text && text.toLowerCase() === "no") {
        message_body = "We apologize for the inconvenience. A representative will contact you to assist you";
    } else {
        message_body = `Response received was "${text}", which is invalid. Please reply with either "Yes" or "No"`;
    }

    const plivoxml = plivo.Response();
    const params = { 'src': to_number, 'dst': from_number };
    plivoxml.addMessage(message_body, params);

    res.type('application/xml');
    res.status(200).send(plivoxml.toXML());
});

app.listen(port, () => {
    console.log(`SMS survey app listening on port ${port}`);
});

Test

  1. Run the application: node survey.js.
  2. Expose your local server to the internet using ngrok: ngrok http 3000.
  3. Create a Plivo Application with the Message URL set to https://<your-ngrok-url>.ngrok.io/survey-response/.
  4. Assign a Plivo number to the application.
  5. Use a tool like Postman to send a POST request to http://localhost:3000/send-survey/ to start the survey.
  6. Reply “Yes” or “No” from the destination phone to see the auto-response.