How to Record a Phone Call using Twilio Java SDK?
Recording incoming and outbound phone conversations could be beneficial in multiple use cases for your company. The ability to record phone conversations with ease is critical for any company’s customer support operations. Analyzing the conversations could help in improving the customer support experience. In addition to it, you could record a customer’s query instead of relying on a voice mailbox when support executives are not available for live support.
In this tutorial, I will walk you through the process of recording a phone call using Twilio’s voice APIs as part of its Java SDK.
Introduction
Twilio provides powerful voice APIs that can be used to build an engaging voice experience that can be easily scaled to millions of users. The voice APIs provide a myriad of features including interactive voice response, speech recognition, and voice recording. In this tutorial, we
Prerequisites
You will require a Twilio account for this tutorial. You can sign up for a trial account here if you don’t already have an account. Also, since we will be using Twilio’s Java SDK, you need to have a Java development environment set up on your device. You can download and install Java from their official site. In addition to Java, you will also need Apache Maven installed on your device to execute the Java code that we write.
We will be testing our Java servlet by using Ngrok, so you can optionally sign up for Ngrok and install it on your device. You can skip installing Ngrok and proceed to directly deploying your Java web app using a cloud provider if you are already familiar with the process.
Setting up a Twilio Phone Number
We need to buy a Twilio phone number using the Twilio console before we can use the Twilio voice APIs. On the console, you can search and buy a phone number from around the world. Choose Phone Numbers > Manage > Buy a number from the left sidebar of the Twilio console to buy a new phone number.
On the next screen that opens up, you can update the search criteria based on your preferences and choose a number that you would like to use. Click on the Buy button to confirm the purchase. Ensure that the phone number you selected supports voice capability since we would need it to record phone conversations.
Note, that different phone numbers might have different monthly fees associated with them. Check the pricing structure before buying the number.
We will come back to the Twilio console later in the tutorial to configure the phone number with a webhook URL. First, let us build a Java servlet to record a part of an incoming phone call.
Record a part of incoming phone call
In this section, we will write a Java servlet that can serve as a webhook for our Twilio phone number and handle incoming HTTP requests. I will walk you through the entire process of developing and deploying a Java servlet since I struggled to find a good tutorial for the same and if you are a beginner, you might otherwise get stuck.
Create a new Java project
Let us first create a new Java project that uses Maven as its build system. Create a new directory for your java project.
mkdir twilio-phone-recorder
Next, let us add a pom.xml
file to the Java project with a standard configuration with required dependencies and plugins for creating a Java servlet.
Notice, that we are using the maven-war-plugin
for collecting all artifacts and dependencies for the web application. Also, we are using the jetty-maven-plugin
which facilitates rapid development and testing.
Define a web app
Next, let us define a simple web app by adding a index.html
page in the src/main/webapp
directory.
Add Twilio dependency
Next, let us add a dependency for Twilio’s Java SDK in the pom.xml file by adding the following code snippet in the dependencies block.
Define Java Servlet
Next, let us finally define a Java servlet to handle incoming HTTP requests. Let us define RecordServlet.java
class in the src/main/java/com/maskaravivek
directory. In the servlet, we are using Twilio’s Twiml which is a mark-up language that lets us use special tags that can be served as instructions while handling a phone call. Using the VoiceResponse
object we set the welcome message and set the record
property to record the voice call.
Notice that the Record.Builder
offers multiple options that can be used to customize how the recording is performed.
Run the Maven project
Finally, let us run the Maven project and see if it works. Open up the terminal and execute the following command in the root directory of your project.
mvn jetty:run
Executing the above command will make the servlet available at localhost. You can navigate to the following URL to see your servlet in action.
Since our /recordIncoming
servlet is of type POST, you can’t open it directly in the browser. Instead, you can use CURL or Postman to test it out.
curl -X POST http://localhost:8080/recordIncoming
Executing the above command should return an XML output similar to the one shown below.
<?xml version=”1.0" encoding=”UTF-8"?><Response><Say>Hello. Please leave a message after the tone and our customer executives will get back to you.</Say><Record/><Hangup/></Response>
Notice, that the above response contains all the options that we set in the handler.
Expose Servlet using Ngrok for testing
Next, let us expose our Java servlet to a public IP using Ngrok. First, obtain the Ngrok token from the Ngrok dashboard and set it by executing the following command.
ngrok authtoken <token>
Next, simply execute the following command to forward HTTP port 8080 to a public IP.
ngrok http 8080
Running the above command should result in an output similar to the one shown below. Grab one of the Forwarding URLs and try opening it in the browser. It should show you the contents of your Java servlet.
Set incoming call webhook using Twilio console
Finally, let us use the Ngrok public URL and set it in the A CALL COMES IN section of your phone number and click Save.
Once you set the webhook, whenever your Twilio phone number receives a phone call, it will trigger this webhook. Since the webhook returns a Twiml response, it will result in the welcome message being read out to the caller and the caller’s response being recorded.
Record outbound phone calls
In this section, we will see the steps for recording outbound phone calls using Twilio’s Java SDK. Let us jump right into it and see how it can be done.
Define a servlet for outbound call response
Similar to how we defined a servlet to handle incoming calls, let us define a GET servlet that returns a Twiml that we will use while making an outbound call. Let us define a simple servlet function that speaks out a preset message.
The servlet will be available at the following URL.
http://localhost:8080/outbound
Fetch account SID and auth token
First, let us fetch the account SID and auth token from the Twilio console and set them as environment variables.
Refer to Twilio’s documentation for steps to set the environment variables on your device.
echo “export TWILIO_ACCOUNT_SID=’ACXXXXXXXXXXXX’” > twilio.env
echo “export TWILIO_AUTH_TOKEN=’your_auth_token’” >> twilio.env
source ./twilio.env
Define script to record outbound calls
Next, let us write a script to record outbound calls using Twilio’s Java SDK. In the code snippet defined below, we first init the Twilio SDK using the account SID and auth token by calling the Twilio.init function. We will use the /outbound
servlet that we just created to provide instructions on how the outbound call should be handled.
Notice, that the first parameter in the Call.creator
method is the to
phone number and the second param is the from
phone number.
As soon as you execute this code snippet, the to
phone number will receive a phone call with a preset message as defined in the /outbound
servlet.
Note, that it is not necessary to define a servlet for the outbound call instructions. If you already have the Twiml hosted somewhere, you could provide that URL instead.
Conclusion
In this tutorial, we saw how Twilio simplifies the process of recording incoming and outbound calls using Twiml and Twilio’s Java SDK. In this tutorial, we exposed the Java servlets using Ngrok but for a production app, you could use a cloud provider such as AWS to deploy the web application. Check out the complete source code used in this tutorial on Github.
You can access your call recordings using the Twilio console. Twilio Voice APIs offer many other features that could help you automate other flows in your company.