Get Started with Number Masking Using Java

    Overview

    Phone number masking hides the phone numbers of parties in a call from each other. Many businesses find it advantageous to anonymize communication between two parties — for example, between a customer and a delivery agent on a food delivery service platform or a driver and a rider using a ride-hailing application. Businesses can implement phone number masking by sending calls through an intermediate phone number that acts as a proxy between the two parties. A Plivo number can serve as the intermediate number to connect the two parties while keeping their contact information private.

    How it works

    Implementation

    As an example, we’ll build a number-masking application for a food delivery service that lets the company connect customers with delivery agents and vice versa without revealing any actual phone numbers. To do this, you

    1. Create a customer-to-agent phone number mapping in your application’s back end.
    2. Create the number masking application using Plivo.
    3. Assign the number masking application to a Plivo number.

    Set up your Java dev environment

    In this section, we’ll walk through how to set up a Java server in five minutes. You must set up and install Java 1.8 or higher and Plivo’s Java SDK to handle incoming calls. Here’s how.

    Install Spring and Plivo Java package using IntelliJ Idea

    • Use Spring Initializr to create a boilerplate project with the Spring Boot framework.

      Create Boilerplate code

    • Choose the Spring Web dependency, give the project a friendly name, and click Generate to download the boilerplate code and open it in IntelliJ Idea.

      Boilerplate project in IntelliJ

      Note: Set the Java target as 8 or 11.
    • Install the Plivo Java SDK by adding the dependency in pom.xml.

        <dependency>
            <groupId>com.plivo</groupId>
            <artifactId>plivo-java</artifactId>
            <version>5.3.0</version>
        </dependency>
      

      Install Plivo package

    Create a 1:1 map with actual numbers

    Now create customer-to-agent phone number mapping for the application. Whenever a customer places an order, their phone number should be stored in a database for your application to access. A delivery agent will be assigned for the order, and the agent’s number will also be stored in your database, and will be mapped to the customer’s number:

    Customer's Number		<->		Agent's Number
      1-415-666-7777					1-415-666-7778
    

    We created sample mapping data in the src/main/resources/application.properties file:

    spring.main.banner-mode=off
    spring.output.ansi.enabled=ALWAYS
    logging.pattern.console=%clr(%d{yy-MM-dd E HH:mm:ss.SSS}){blue} %clr(%-5p) %clr(%logger{0}){blue} %clr(%m){faint}%n
    
    number.map={"14156667777":"14156667778", "14156667779":"14156667780", "14156667781":"14156667782"}
    

    Create a Spring application for number masking

    Open the NumberMaskingApplication.java file in the src/main/java/com.example.NumberMasking/ folder and paste into it this code.

    Note: Here, the demo application name is NumberMaskingApplication.java because the friendly name we provided in Spring Initializr was NumberMasking.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    
    package com.example.NumberMasking;
    
    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.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.*;
    import com.google.common.collect.HashBiMap;
    import java.util.Map;
    
    @SpringBootApplication
    @RestController
    public class NumberMaskingApplication {
    
    	@Value("#{${number.map}}")
    	Map<String, String> CustomerAgentMap;
    	
    	public static void main(String[] args) {
    		SpringApplication.run(NumberMaskingApplication.class, args);
    	}
    	
    	// Handle incoming calls to a Plivo number, connect agent with customer and vice versa without revealing their actual phone numbers.
    	@RequestMapping(value = "/number_masking/", produces = { "application/xml" }, method = { RequestMethod.GET, RequestMethod.POST })
    	public Response HandleIncoming(@RequestParam("From") String FromNumber, @RequestParam("To") String ToNumber)
    			throws PlivoXmlException {
    		Map<String, String> AgentCustomerMap = HashBiMap.create(CustomerAgentMap).inverse();
    		Response response = new Response();
    		if(CustomerAgentMap.containsKey(FromNumber)) { // To check if the Customer's number is existing in the Customer-Agent mapping
    			var DestNumber =  CustomerAgentMap.get(FromNumber); // This will assign the Value from the customer-agent map to DestNumber variable
    			response.children(new Dial()
    					.callerId(ToNumber) // Plivo number will be used as the caller ID for the call towards Agent
    					.children(new Number(DestNumber)));
    		}
    		else if (AgentCustomerMap.containsKey(FromNumber)) { // To check if the Agent's number is existing in the Customer-Agent mapping
    			var DestNumber =  AgentCustomerMap.get(FromNumber); // This will assign the Key from the customer-agent map to DestNumber variable
    			response.children(new Dial()
    					.callerId(ToNumber) // Plivo number will be used as the caller ID for the call towards Customer
    					.children(new Number(DestNumber)));
    		}
    		System.out.println(response.toXmlString());
    		return response;
    	}
    }
    

    Save the file and run it.

    Number Masking

    You should see your basic server app in action on http://localhost:8080/number_masking/

    Expose your local server to the internet

    To receive incoming calls, your local server should be able to connect with Plivo API services. For that, we recommend using ngrok, which exposes local servers behind NATs and firewalls to the public internet over secure tunnels.

    ngrok block diagram

    Install ngrok and run it on the command line, specifying the port that hosts the application on which you want to receive messages (8080 in this case):

    ./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.

    Sample ngrok CLI

    Now people can call your Plivo number. If an incoming call to your Plivo number is from one of the customer phone numbers in the Customer <-> Agent Map — for example, if the caller number is 14156667777 — then Plivo will send the XML response to process the incoming call as below, and you can check the XML document in your browser.

    XML Document - Customer Calls Agent

    If the incoming call to your Plivo number is from one of the agent phone numbers in the Customer <-> Agent Map — for example, if the caller number is 14156667778 — then Plivo will send the XML response to process the incoming call as below, and you can check the XML document in your browser.

    XML Document - Agent Calls Customer

    Create a Plivo application

    1. Create a Plivo application by visiting the Voice > Applications > XML page of the console and clicking on Add New Application, or by using Plivo’s Application API.

    2. Give your application a name — we used Number Masking. Enter your server URL (for example, https://61d8fb8f250c.ngrok.io/handleincoming/) in the Primary Answer URL field and set the method as GET. See our Application API docs to learn how to modify your application through our APIs.

    3. Click on Create Application to save your application.

    Create Application - Number Masking

    Assign a Plivo number to your app

    1. Navigate to the Numbers page and select the phone number you want to use for the application.
    2. Select XML Application from the Application Type drop-down list, and Number Masking (the name of the application) from the Plivo Application drop-down list.
    3. Click on Update Number to save the number assignment.

      Assign Application - Number Masking

    Test and validate

    To test the application, you need two Plivo mobile numbers. Set up one of your mobile numbers as a customer and another as an agent in the customer-to-agent mapping data in the config file. Make a call from each of your mobile numbers to the Plivo number you mapped to the application. You should see that the call is forwarded to the other number, and the incoming call has the Plivo number as the caller ID.