Skip to main content

Overview

This guide shows how to write an SMS autoresponder using Plivo’s APIs and Node.js. An autoresponder can streamline marketing campaigns and subscription signups by providing instant, keyword-based replies.

How it works

When Plivo receives an SMS on your number, it makes an HTTP request to your application’s “Message URL.” Your server receives the message content and can perform logic based on keywords. To reply, your server must respond to the request with a Plivo XML document containing a <Message> element.

Prerequisites


Create the autoresponder application

Create a file called autoresponder.js and paste this code into it. This server listens for incoming messages, checks for the keyword “interested,” and sends back a different reply based on whether the keyword is found.
const plivo = require('plivo');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

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

app.all('/autoresponder/', (request, response) => {
    const from_number = request.body.From || request.query.From;
    const to_number = request.body.To || request.query.To;
    const text = request.body.Text || request.query.Text;

    let body;
    if (text && text.toLowerCase() === 'interested') {
        body = 'Thank you for showing interest. One of our agents will contact you.'
    } else {
        body = 'Reply "Interested" to connect with our agents'
    }

    const r = plivo.Response();
    const params = {
        'src': to_number, // The Plivo number
        'dst': from_number, // The original sender's number
    };
    r.addMessage(body, params);

    response.type('application/xml');
    response.end(r.toXML());
});

app.listen(3000, () => {
    console.log('Node app is running on port 3000');
});

Create and configure a Plivo application

  1. Create an Application: Go to Messaging > Applications and click Add New Application.
  2. Configure the URL: Name the application (e.g., Autoresponder). In the Message URL field, enter your server URL (e.g., https://<yourdomain>.com/autoresponder/) and set the method to POST. Click Create Application.
  3. Assign a Number: Go to the Numbers page, select your number, and link it to the Autoresponder application you just created.

Test it out

Send any SMS to your Plivo number to get the default reply. Then, send the word “Interested” to get the confirmation message.