Get Started with Number Masking using Ruby
Overview
Phone number masking is the process of establishing a call between two parties without revealing their contact information. Many businesses often require a technique to anonymize communication between two parties involved on the platform, for example, a customer and a delivery agent on a food delivery service platform. This technique involves an intermediate phone number that acts as a proxy between the two parties on the call. In this guide, we will walk you through the implementation of number masking with the help of a Plivo number which will act as the intermediate number to connect both the parties on a call anonymously.
Outline
Implementation
In this example, we will build a number masking application for a food delivery service. Through this implementation, the company can connect the customers with the delivery agents and vice versa without revealing their actual phone numbers.
Following are the steps involved in the implementation phase:
- Create a Customer<->Agent phone number mapping in your application backend
- Create the number masking application using the Plivo Voice platform.
- Assign the number masking app to a Plivo number.
Set Up Your Ruby on Rails Dev Environment
You must install rails, create a rails project and add Plivo’s Ruby SDK to make your first call. Here’s how.
Install Rails
Use the below command to install Rails
$ gem install rails
Create a Rails Project
As we have Rails and its dependencies installed, we can use them to create a new Rails project. As the initial step, using rails we can auto-generate code in the ruby on rails folder structure. Use the below command to start your Rails project.
$ rails new numbermasking
This will create a numbermasking
directory with the necessary folders & files for development.
Install Plivo
As we have the rails application created, now, let’s add Plivo-Ruby by modifying the Gemfile. Open the Gemfile in any IDE/text-editor and add the following line:
gem 'plivo', '~> 4.13.0'
You can use the below command to install the Plivo-Ruby gem into the bundle:
$ bundle install
Create a 1:1 map with actual numbers
- Whenever a customer places an order, the customer’s phone number is stored on your app’s database.
- A delivery executive will be assigned for that order, and the agent’s number is also stored on your database and mapped with the customer’s number. For example,
Customer's Number <-> Agent's Number 1-415-666-7777 1-415-666-7778
- In this guide, we have created a sample mapping data in
config/application.rb
file like below:# customer <> agent map config.base_map = {"14156667777" => "14156667778", "14156667779" => "14156667780", "14156667781" => "14156667782"}
Create a Rails Controller for Number Masking
Change the directory to our newly created project directory, i.e, numbermasking
directory and run the below command to create a rails controller for outbound call.
$ rails generate controller Numbermasking
This will generate a controller named numbermasking_controller in the app/controllers/ directory. Now, You have to open the app/controllers/numbermasking_controller.rb file and add the following code:
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
include Plivo
include Plivo::XML
include Plivo::Exceptions
# Handle incoming calls to a Plivo number, connect agent with customer and vice versa without revealing their actual phone numbers.
class NumbermaskingController < ApplicationController
def handle_incoming
customer_agent_map = Rails.application.config.base_map
agent_customer_map = customer_agent_map.invert
from_number = params[:From]
to_number = params[:To]
response = Response.new()
customer_to_agent = customer_agent_map.include?(from_number) # To check if the Customer's number is existing in the Customer-Agent mapping
agent_to_customer = agent_customer_map.include?(from_number) # To check if the Agent's number is existing in the Customer-Agent mapping
if(customer_to_agent == true)
dest_number = customer_agent_map[from_number] # This will assign the Value from the customer-agent array to dest_number variable
params = {
'callerId' => to_number # Plivo number will be used as the caller ID for the call towards Agent
}
dial = response.addDial(params)
dial.addNumber(dest_number)
elsif(agent_to_customer == true)
dest_number = agent_customer_map[from_number] # This will assign the Key from the customer-agent array to dest_number variable
params = {
'callerId' => to_number # Plivo number will be used as the caller ID for the call towards Customer
}
dial = response.addDial(params)
dial.addNumber(dest_number)
end
xml = PlivoXML.new(response)
puts xml.to_xml()
render xml: xml.to_xml
end
end
Add a Route
You need to add a route for the handle_incoming function in NumbermaskingController class, open the config/routes.rb file and add the below line after the outbound route:
get 'numbermasking/handle_incoming'
Now the numbermasking_controller
is ready to handle incoming calls to a Plivo number, connect agent with customer and vice versa without revealing their actual phone numbers. You can use the below command to start the Rails server to forward incoming calls and to handle callbacks.
$ rails server
And you should see your basic server app in action on http://localhost:3000/numbermasking/handle_incoming/
Exposing your local server to the internet
To receive Incoming Calls and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.
You can download and install ngrok from here. Follow the detailed configuration instructions to get started.
# Whitelist Ngrok domain
config.hosts << /[a-z0-9]+\.ngrok\.io/
Run ngrok on the port which currently hosts your application. For example, if your port number is 3000, run the following command:
./ngrok http 3000
This will give you a UI with links that look like ngrok.io/*
which you can use to access your local server using the public network.
If the incoming call to your Plivo number is from one of the phone numbers of the customer in the Customer <-> Agent Map, for example, if the caller number is “14156667777” then the XML response to process the incoming call will be as below, you can check the XML document in your browser.
If the incoming call to your Plivo number is from one of the phone numbers of the agent in the Customer <-> Agent Map, for example, if the caller number is “14156667778” then the XML response to process the incoming call will be as below, you can check the XML document in your browser.
Create a Plivo Application
- Create an Application by visiting the Application Page and click on
New Application
or by using Plivo’s Application API. - Give your application a name. Let’s call it
Phone IVR
. Enter your server URL (e.g., https://68b9f8c8da2b.ngrok.io/numbermasking/handle_incoming/) in theAnswer URL
field and set the method asGET
. See our Application API docs to learn how to modify your application through our APIs. Click on
Create
to save your application.
Assign a Plivo number to your app
- Navigate to the Numbers page and select the phone number you want to use for this app.
- Select
Phone IVR
(name of the app) from the Plivo App dropdown list. Click on ‘Update Number’ to save.
Test and validate
To test how “Number Masking” works, you need two mobile numbers. You can set one of your mobile numbers as a customer and another one as an agent in the customer: agent mapping data in the config file and make a call to your Plivo phone number. If you call from “mobile number 1” to the Plivo number, you will see that the call is forwarded to your “mobile number2” with the Plivo number as the caller ID. The same setup will work for calls from “mobile number2” to “mobile number1” with the Plivo number as the caller ID.