How to Receive and Respond to Incoming SMS Messages in Java with Spring Boot and Plivo

Sending an outbound message using the Plivo SMS platform is easy, but communication should be a two-way street. Customers should be able to text you, and you should acknowledge their messages and address their concerns. To do this, you can build a Java Spring Boot application to receive and respond to incoming SMS messages on a Plivo phone number. In this post, we walk you through how to implement this.

Prerequisites

Before you get started, you’ll need:

  • A Plivo account — sign up for one for free if you don’t have one already.
  • An SMS-enabled Plivo phone number as you want to receive incoming SMS messages. To search for and buy an available number, go to Phone Numbers > Buy Numbers on the Plivo console.
  • Sprint Boot and Plivo Java packages — use the Spring Initializr to create a demo project with boilerplate code.
  • ngrok — a utility that exposes your local development server to the internet over secure tunnels.

Create a Spring Boot application to receive SMS messages

Once you’ve created the Spring Boot application using Spring Initializr, you can add the Plivo Java SDK using Maven or Gradle, or Groovy as per the interface selected. Update the Java application in the created project to handle incoming SMS messages on a Plivo number. Use this code:

package com.example.SMS.Demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController

public class SmsDemoApplication {

  public static void main(String[] args) {
     SpringApplication.run(SmsDemoApplication.class, args);
  }

  @RequestMapping(value = "/receive_sms/", method = { RequestMethod.GET, RequestMethod.POST })
  public String ReceiveSms(@RequestParam("From") String fromNumber, @RequestParam("To") String toNumber, @RequestParam("Text") String Text) {
     String ReceivedParams = fromNumber + " " + toNumber + " " + Text;
     System.out.println(ReceivedParams);
     return ReceivedParams;
  }
}

Return a Message XML document to reply to incoming messages

To reply to an incoming SMS message, you need to return an XML document from the URL configured as the message_url in the application assigned to the Plivo number. The Java SDK can manage the XML document generation, and you can use the Message XML element to reply to incoming SMS messages. Use this code:

package com.example.SMS.Demo;

import com.plivo.api.exceptions.PlivoXmlException;
import com.plivo.api.xml.Message;
import com.plivo.api.xml.Response;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController

public class SmsDemoApplication {

  public static void main(String[] args) {
     SpringApplication.run(SmsDemoApplication.class, args);
  }

  @RequestMapping(value = "/reply_sms/", produces={"application/xml"}, method = { RequestMethod.GET, RequestMethod.POST })
  public String ReplySms(@RequestParam("From") String fromNumber, @RequestParam("To") String toNumber, @RequestParam("Text") String Text) throws PlivoXmlException {
     System.out.println(fromNumber + " " + toNumber + " " + Text);
     Response resp = new Response()
           .children(
                 new Message(fromNumber,toNumber,"Thank you, we have received your request.")
           );
     return resp.toXmlString();
  }
}

Test the code locally

Save the file and run the application. You should see your basic server application in action on http://localhost:8080/reply_sms/.

Expose the local server to the internet using ngrok

Once you see the application working locally, the next step is to connect the application to the internet to receive and reply to messages. For that, we recommend using ngrok, which exposes local servers behind NATs and firewalls to the public internet over secure tunnels. Install it and run ngrok on the command line, specifying the port that hosts the application on which you want to receive messages (8080 in this case, as our local Spring Boot application is running there):

$ ./ngrok http 8080

Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network.

Ngrok CLI

Test the link by opening the ngrok URL(https://80aefe4087cc.ngrok.io/reply_sms/\?From=14156667777\&To=14156667778\&Text=hi) in a browser. We used HTTPie to check the XML response from the ngrok URL.

XML document with Message XML element

Connect the Spring Boot application to a Plivo number

The final step is to configure the app as a Plivo messaging application and assign it to a Plivo number on which you want to receive SMS messages.

Go to the Plivo console and navigate to Messaging > Applications > XML, then click on the Add New Application button in the upper right.

Provide a friendly name for the app — we used “App-Incoming-SMS” — and configure the ngrok URL https://80aefe4087cc.ngrok.io/reply_sms/ as the Message URL. Select the HTTP verb as POST, then click Create Application.

Create Plivo App to handle incoming SMS messages

Now go to Phone Numbers > Your Numbers and click on the number to which you want to assign the application. From the Plivo Application drop-down, choose the message application you just created. Finally, click Update Number.

Assign the Plivo App to a Plivo Number

Test the application

Send an SMS to the Plivo number you selected. You should see that the Spring Boot application automatically sends a reply back to your mobile number.

And that’s how simple it is to receive and respond to incoming SMS messages using Plivo’s Java SDK and a Spring Boot application.

Haven’t tried Plivo yet? Getting started is easy and only takes five minutes! Sign up today.

comments powered by Disqus

By submitting this form, you agree we may contact you in the manner described in our Privacy Policy