Skip to main content

Overview

This guide shows how to write an MMS autoresponder using Plivo’s APIs and Node.js. An autoresponder can provide instant, automated replies with media based on keywords in incoming messages.

How it works

When Plivo receives an MMS on your number, it sends an HTTP request (webhook) to your application’s “Message URL.” Your server can then use the REST API to trigger a new outbound MMS in response. Your server should also send a 200 OK response to Plivo to acknowledge the incoming webhook.

Prerequisites


Create the autoresponder application

Create a file called mms_autoresponder.js and paste this code into it. This server listens for incoming messages, checks for keywords like “hi” or “bye,” and sends back an MMS with text and a GIF.
const express = require('express');
const bodyParser = require('body-parser');
const plivo = require('plivo');

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

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

    console.log(`Message received - From: ${from_number}, To: ${to_number}, Text: ${text}`);

    let reply_text;
    let media_urls;

    if (text && text.toLowerCase() === 'hi') {
        reply_text = "Hello!";
        media_urls = ["[https://media.giphy.com/media/888R35MJTmDxQfRzfS/giphy.gif](https://media.giphy.com/media/888R35MJTmDxQfRzfS/giphy.gif)"];
    } else if (text && text.toLowerCase() === 'bye') {
        reply_text = "Bye and have a nice day!";
        media_urls = ["[https://media.giphy.com/media/QM5lHSyFjz1XW/giphy.gif](https://media.giphy.com/media/QM5lHSyFjz1XW/giphy.gif)"];
    } else {
        reply_text = "I'm glad that we connected";
        media_urls = ["[https://media.giphy.com/media/888R35MJTmDxQfRzfS/giphy.gif](https://media.giphy.com/media/888R35MJTmDxQfRzfS/giphy.gif)"];
    }

    // Acknowledge the webhook from Plivo
    response.status(204).send();

    // Send the MMS reply using the REST API
    const client = new plivo.Client("<auth_id>", "<auth_token>");
    try {
        const msgResponse = await client.messages.create({
            src: to_number,   // The Plivo number
            dst: from_number, // The original sender's number
            text: reply_text,
            type: "mms",
            media_urls: media_urls
        });
        console.log("MMS reply sent:", msgResponse);
    } catch (error) {
        console.error("Error sending MMS:", error);
    }
});

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., MMS 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 MMS Autoresponder application.

Test it out

Send an MMS with the text “hi” or “bye” to your Plivo number to see the different GIF responses.