How to Build a Call Forwarding App in Java Using Spring Boot and Plivo

How to Build a Call Forwarding App in Java Using Spring Boot and Plivo

Businesses use call forwarding all the time to route incoming calls to available agents, extensions, or departments that cater to the caller’s needs. Creating a call forwarding app is simple when you use Plivo’s Java SDK. This guide shows you how to receive incoming calls on Plivo numbers and manage the call flow once a call reaches the Plivo voice platform. To see how to do this, we’ll build a Java Spring Boot application to forward the call to a mobile number using the Dial XML element.

Prerequisites

Before you get started, you’ll need:

  • A Plivo account — sign up for one for free if you don’t have one already.
  • A voice-enabled Plivo phone number if you want to receive incoming calls. To search for and buy an available number, go to Phone Numbers > Buy Numbers on the Plivo console.
Buy a New Plivo Number
  • 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 forward incoming calls

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 calls on a Plivo number. To handle an incoming call, you need to return an XML document from the URL configured as the Answer URL in the application assigned to the Plivo number. The Java SDK can manage the XML document generation, and you can use the Dial XML element to forward the call to a mobile number. Use this code:

package com.example.CallForward;

    import com.plivo.api.exceptions.PlivoValidationException;
    import com.plivo.api.exceptions.PlivoXmlException;
    import com.plivo.api.xml.Dial;
    import com.plivo.api.xml.Response;
    import com.plivo.api.xml.Number;
    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 CallForwardApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(CallForwardApplication.class, args);
        }
    
        @RequestMapping(value = "/forward_call/", produces = {
            "application/xml"
        }, method = {
            RequestMethod.GET, RequestMethod.POST
        })
        public String ReceiveCall(@RequestParam("From") String from_number) throws 
        PlivoXmlException, PlivoValidationException {
            Response res = new Response()
                .children(
                    new Dial()
                    .callerId(from_number)
                    .children(new Number("14156667777"))
                );
            // Returns the XML
            return res.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/forward_call/.

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 return the XML document to process the incoming call. 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 forward incoming calls (8080 in this case, as our local spring boot application is running there):

$ ./ngrok http 8080
Ngrok CLI

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

Test the link by opening the ngrok URL (https://7ec95b6fd810.ngrok.io/forward_call/) in a browser or use HTTPie to check the XML response from the ngrok URL.

XML document with Dial XML element

Connect the spring boot application to a Plivo number

The final step is to configure the application as a Plivo voice application and assign it to a Plivo number on which you want to forward incoming calls.

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

Provide a friendly name for the application — we used “App-call-forward” — and configured the ngrok URL https://7ec95b6fd810.ngrok.io/forward_call/ as the Answer URL. Select the HTTP verb as POST, then click Create Application.

Create Plivo App to forward incoming calls

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 voice application you just created. Finally, click Update Number.

Assign the call forward Plivo App to a Plivo Number

Test the application

Make a phone call to the Plivo number you selected. You should see that the spring boot application automatically forwards the call to the phone number configured in the call forwarding app.

And that’s how simple it is to receive an incoming call on a Plivo number and forward it using XML documents using Plivo’s Java SDK and a spring boot application. You can implement other use cases on the Plivo Voice platform, such as phone system IVR, receive DTMF/Speech inputs, and number masking, as your business requires.

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

The State of Marketing in 2024

HubSpot's Annual Inbound Marketing Trends Report

Frequently asked questions

No items found.
footer bg

Subscribe to Our Newsletter

Get monthly product and feature updates, the latest industry news, and more!

Thank you icon
Thank you!
Thank you for subscribing
Oops! Something went wrong while submitting the form.