IVR Using Twilio

Posted By : Satyam Goel | 31-May-2021

IVR System generate custom voice menus and route calls to the person using the Twilio API.

 

Adding an Interactive Voice Response (IVR) system to our existing call center is simple with the web language. It helps us to create a custom phone tree and route our callers intelligently to deliver a experience.

 

Various steps needs to be taken to make an IVR System

 

1. ANSWER A PHONE CALL
To initiate the IVR, when receiving a call, configure one of our Twilio numbers to send an HTTP request to our web app.

 

2. RESPOND TO TWILIO REQUEST
Our web app responds to Twilio’s HTTP request with a top-level menu written in TwiML. Twilio then plays the menu options and gathers input from the caller.

 

3. RECEIVE CALLER’S INPUT
Twilio reads the input (DTMF tones) from the user and then sends an HTTP request to the server for further details and instructions on how to respond.

 

4. PROCESS THE CALLER’S SELECTION
Depending on the caller’s selection, our web app instructs Twilio to play another menu, gather more information, or connect the call to another phone number.

 

Let's see example how to use IVR in Iava Applications

 

- Make an Outbound Calls

In this step, we use Programmable Voice to make outbound phone calls from our Java applications.

public static void makeCalls() {


Twilio.init(ACCOUNT_SID, AUTH_TOKEN);


Call call = Call.creator(


new com.twilio.type.PhoneNumber("+14155551212"),


new com.twilio.type.PhoneNumber("+15017122661"),


new com.twilio.type.Twiml("<Response><Say>Ahoy, World!</Say></Response>"))


.create();


System.out.println(call.getSid());


}

 

- Respond to Incoming Phone Calls

In this step, we use Programmable Voice to Respond to Incoming Phone Calls from our Java applications.

 

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {


TwiMLResponse twiml = new TwiMLResponse();


try {


Say message = new Say("hello world!");


message.setVoice("alice");


twiml.append(message);


} catch (TwiMLException e) {


e.printStackTrace();


}

 

- Gather User Input via Keypad (DTMF Tones)

In this step, we get user input through a phone call via the phone's keypad (using DTMF tones) in the Java application.

 

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {


TwiMLResponse twiml = new TwiMLResponse();

Gather gather = new Gather();


gather.setNumDigits(1);


try {


gather.append(new Say("For sales, press 1. For support, press 2."));


twiml.append(gather);


twiml.append(new Redirect("/voice"));


} catch (TwiMLException e) {


throw new RuntimeException(e);


}

About Author

Author Image
Satyam Goel

Satyam Goel is an experienced backend developer with expertise in Java technology. He possesses a solid understanding and practical knowledge of Core Java, Spring-Boot, Hibernate, JUnit, and relational databases like MySQL, PostgreSQL, and more. Satyam is proficient in API implementations, web services, development testing, deployment, and code enhancements. His contributions to various client projects, such as SEI, Herdsy, Bhaasha, and Ignitiv, based on e-commerce and other domains. Satyam's creative mind and analytical skills have enabled him to deliver high-quality solutions. He also has knowledge of cloud services such as AWS, Google Cloud, and Microsoft Azure, making him a versatile and valuable member of any development team.

Request for Proposal

Name is required

Comment is required

Sending message..