IVR: Screening & Recording with Java and Servlets

ET Phone Home

 This Java Servlets sample application is modeled after a typical call center experience, but with more Reese's Pieces.

Stranded aliens can call an agent and receive instructions on how to get off of Earth safely. In this tutorial, we'll show you the key bits of code that allow an agent to send a caller to voicemail, and later read transcripts and listen to voicemails.

To run this sample app yourself, download the code and follow the instructions on GitHub.

Read how Hulu built an IVR on Twilio that lets customers waiting on hold schedule a call back.

Route the Call to an Agent

 When our alien caller reaches our call center, we need to figure out where to route the call. Depending on their input we will route this call to an extension. Extensions are used to look up an agent. Any string can be used to define an extension.

Once we look up the agent, we can use the <Dial> verb to dial the agent's phone number and try to connect the call.

Loading Code Samples...
Language
package com.twilio.ivrrecording.servlet.extension;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.models.Agent;
import com.twilio.ivrrecording.repositories.AgentRepository;
import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.*;
import com.twilio.twiml.Number;

public class ConnectServlet extends WebAppServlet {

  private AgentRepository agentRepository;

  public ConnectServlet() {
    this(new AgentRepository());
  }

  public ConnectServlet(AgentRepository agentRepository) {
    this.agentRepository = agentRepository;
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String selectedOption = request.getParameter("Digits");

    Agent agent = findAgentByExtension(selectedOption);
    if (agent == null) {
      redirectToMenu(response);
    } else {
      Say say = new Say.Builder("You'll be connected shortly to your planet.")
          .voice(Say.Voice.ALICE)
          .language(Say.Language.EN_GB)
          .build();

      Number number = new Number.Builder(agent.getPhoneNumber())
          .url("/agents/screen-call")
          .build();

      Dial dial = new Dial.Builder()
          .action(String.format("/agents/call?agentId=%s", agent.getId()))
          .number(number)
          .build();

      VoiceResponse voiceResponse = new VoiceResponse.Builder().say(say).dial(dial).build();

      respondTwiML(response, voiceResponse);
    }
  }

  private void redirectToMenu(HttpServletResponse response) throws IOException {
    Redirect redirect = new Redirect.Builder().url("/ivr/welcome").build();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().redirect(redirect).build();

    respondTwiML(response, voiceResponse);
  }

  private Agent findAgentByExtension(String extension) {
    Map<String, String> planetsExtensions = new HashMap<>();
    planetsExtensions.put("2", "Brodo");
    planetsExtensions.put("3", "Dagobah");
    planetsExtensions.put("4", "Oober");

    return planetsExtensions.containsKey(extension)
        ? agentRepository.findByExtension(planetsExtensions.get(extension)) : null;
  }
}
src/main/java/com/twilio/ivrrecording/servlet/extension/ConnectServlet.java
Route the call to an agent

src/main/java/com/twilio/ivrrecording/servlet/extension/ConnectServlet.java

With this information, we present aliens with a list of available agents so they can pick one. Let's see how we look up an agent.

Look up an agent

When we receive a call from an alien we give them a set of options. In this case the options are:

  • For Brodo, press 2
  • For Dagobah, press 3
  • For Oober, press 4

When our alien caller has made their choice we use the key-press to lookup an Agent.

Loading Code Samples...
Language
package com.twilio.ivrrecording.servlet.extension;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.models.Agent;
import com.twilio.ivrrecording.repositories.AgentRepository;
import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.*;
import com.twilio.twiml.Number;

public class ConnectServlet extends WebAppServlet {

  private AgentRepository agentRepository;

  public ConnectServlet() {
    this(new AgentRepository());
  }

  public ConnectServlet(AgentRepository agentRepository) {
    this.agentRepository = agentRepository;
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String selectedOption = request.getParameter("Digits");

    Agent agent = findAgentByExtension(selectedOption);
    if (agent == null) {
      redirectToMenu(response);
    } else {
      Say say = new Say.Builder("You'll be connected shortly to your planet.")
          .voice(Say.Voice.ALICE)
          .language(Say.Language.EN_GB)
          .build();

      Number number = new Number.Builder(agent.getPhoneNumber())
          .url("/agents/screen-call")
          .build();

      Dial dial = new Dial.Builder()
          .action(String.format("/agents/call?agentId=%s", agent.getId()))
          .number(number)
          .build();

      VoiceResponse voiceResponse = new VoiceResponse.Builder().say(say).dial(dial).build();

      respondTwiML(response, voiceResponse);
    }
  }

  private void redirectToMenu(HttpServletResponse response) throws IOException {
    Redirect redirect = new Redirect.Builder().url("/ivr/welcome").build();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().redirect(redirect).build();

    respondTwiML(response, voiceResponse);
  }

  private Agent findAgentByExtension(String extension) {
    Map<String, String> planetsExtensions = new HashMap<>();
    planetsExtensions.put("2", "Brodo");
    planetsExtensions.put("3", "Dagobah");
    planetsExtensions.put("4", "Oober");

    return planetsExtensions.containsKey(extension)
        ? agentRepository.findByExtension(planetsExtensions.get(extension)) : null;
  }
}
src/main/java/com/twilio/ivrrecording/servlet/extension/ConnectServlet.java
Find an agent by extension

src/main/java/com/twilio/ivrrecording/servlet/extension/ConnectServlet.java

Now that our user has chosen their agent, our next step is to connect the call to that agent.

Call the agent

This code begins the process of transferring the call to our agent.

By passing a url to the <Number> noun, we are telling Twilio to make a POST request to the agents/screen-call route after the agent has picked up but before connecting the two parties.

Essentially, we are telling Twilio to execute some TwiML that only the agent will hear.

Loading Code Samples...
Language
package com.twilio.ivrrecording.servlet.extension;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.models.Agent;
import com.twilio.ivrrecording.repositories.AgentRepository;
import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.*;
import com.twilio.twiml.Number;

public class ConnectServlet extends WebAppServlet {

  private AgentRepository agentRepository;

  public ConnectServlet() {
    this(new AgentRepository());
  }

  public ConnectServlet(AgentRepository agentRepository) {
    this.agentRepository = agentRepository;
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String selectedOption = request.getParameter("Digits");

    Agent agent = findAgentByExtension(selectedOption);
    if (agent == null) {
      redirectToMenu(response);
    } else {
      Say say = new Say.Builder("You'll be connected shortly to your planet.")
          .voice(Say.Voice.ALICE)
          .language(Say.Language.EN_GB)
          .build();

      Number number = new Number.Builder(agent.getPhoneNumber())
          .url("/agents/screen-call")
          .build();

      Dial dial = new Dial.Builder()
          .action(String.format("/agents/call?agentId=%s", agent.getId()))
          .number(number)
          .build();

      VoiceResponse voiceResponse = new VoiceResponse.Builder().say(say).dial(dial).build();

      respondTwiML(response, voiceResponse);
    }
  }

  private void redirectToMenu(HttpServletResponse response) throws IOException {
    Redirect redirect = new Redirect.Builder().url("/ivr/welcome").build();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().redirect(redirect).build();

    respondTwiML(response, voiceResponse);
  }

  private Agent findAgentByExtension(String extension) {
    Map<String, String> planetsExtensions = new HashMap<>();
    planetsExtensions.put("2", "Brodo");
    planetsExtensions.put("3", "Dagobah");
    planetsExtensions.put("4", "Oober");

    return planetsExtensions.containsKey(extension)
        ? agentRepository.findByExtension(planetsExtensions.get(extension)) : null;
  }
}
src/main/java/com/twilio/ivrrecording/servlet/extension/ConnectServlet.java
Connect the call to an Agent

src/main/java/com/twilio/ivrrecording/servlet/extension/ConnectServlet.java

Our agent can now be called, but how does our agent interact with this feature? Let's dig into what is happening in the agent's screening call.

The agent screens the call

When our agent picks up the phone, we use a <Gather> verb to ask them if they want to accept the call.

If the agent responds by entering any digit, the response will be processed by our agents/message route. This will <Say> a quick message and continue with the original <Dial> command to connect the two parties.

Loading Code Samples...
Language
package com.twilio.ivrrecording.servlet.agent;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.Gather;
import com.twilio.twiml.Hangup;
import com.twilio.twiml.Say;
import com.twilio.twiml.VoiceResponse;

public class ScreenCallServlet extends WebAppServlet {

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

    String from = request.getParameter("From");

    String incomingCallMessage = "You have an incoming call from: " + getSpelledPhoneNumber(from);
    String gatherMessage = incomingCallMessage + ".Press any key to accept";

    Say sayInGather = new Say.Builder(gatherMessage).build();
    Gather gather = new Gather.Builder()
        .numDigits(1)
        .action("/agents/message")
        .say(sayInGather)
        .build();

    Say say = new Say.Builder("Sorry. Did not get your response").build();
    Hangup hangup = new Hangup();

    VoiceResponse voiceResponse = new VoiceResponse.Builder()
        .gather(gather)
        .say(say)
        .hangup(hangup)
        .build();

    respondTwiML(response, voiceResponse);
  }

  private String getSpelledPhoneNumber(String phoneNumber) {
    return String.join(", ", phoneNumber.split(""));
  }
}
src/main/java/com/twilio/ivrrecording/servlet/agent/ScreenCallServlet.java
Allow agent to screen a call, accept, and connect to the caller

src/main/java/com/twilio/ivrrecording/servlet/agent/ScreenCallServlet.java

Now our agent can interact with the call, but what if our agent is currently out? In these cases it's helpful to have voicemail set up.

Send the caller to voicemail

 When Twilio makes a request to our Call action method, it will pass a DialCallStatus argument to tell us the call status. If the status is "completed", we hang up. Otherwise, we need to <Say> a quick prompt and then <Record> a voicemail from the alien caller.

We also specify an action for <Record>. This route will be called after the call and recording have finished. The route will say "Goodbye" and then <Hangup>.

Loading Code Samples...
Language
package com.twilio.ivrrecording.servlet.agent;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.Hangup;
import com.twilio.twiml.Record;
import com.twilio.twiml.Say;
import com.twilio.twiml.VoiceResponse;

public class CallServlet extends WebAppServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String dialCallStatus = request.getParameter("DialCallStatus");
    String agentId = request.getParameter("agentId");

    if (Objects.equals(dialCallStatus, "completed")) {
      respondContent(response, "");
      return;
    }

    Say say1 = new Say.Builder(
        "It appears that no agent is available. " + "Please leave a message after the beep")
            .language(Say.Language.EN_GB)
            .voice(Say.Voice.ALICE)
            .build();

    Record record = new Record.Builder()
        .maxLength(20)
        .action("/agents/hangup")
        .transcribeCallback(String.format("/records/create?agentId=%s", agentId))
        .build();

    Say say2 = new Say.Builder("No record received. Goodbye")
        .language(Say.Language.EN_GB)
        .voice(Say.Voice.ALICE)
        .build();

    Hangup hangup = new Hangup();

    VoiceResponse voiceResponse = new VoiceResponse.Builder()
        .say(say1)
        .record(record)
        .say(say2)
        .hangup(hangup)
        .build();

    respondTwiML(response, voiceResponse);
  }
}
src/main/java/com/twilio/ivrrecording/servlet/agent/CallServlet.java
Redirect a call to voicemail and end the call

src/main/java/com/twilio/ivrrecording/servlet/agent/CallServlet.java

Now let's take a step back to see how to actually record the call.

Record the caller

When we tell Twilio to record, we have a few options we can pass to the <Record> verb.

Here we instruct <Record> to stop the recording at 20 seconds, to transcribe the call, and to send the transcription to the agent when it's complete.

Notice that we redirect to a URL that is specific to this agent. This is a convenient way to specify which agent was called to produce the voice message. This way we can also save the associated agent together with the voicemail.

Loading Code Samples...
Language
package com.twilio.ivrrecording.servlet.agent;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.Hangup;
import com.twilio.twiml.Record;
import com.twilio.twiml.Say;
import com.twilio.twiml.VoiceResponse;

public class CallServlet extends WebAppServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String dialCallStatus = request.getParameter("DialCallStatus");
    String agentId = request.getParameter("agentId");

    if (Objects.equals(dialCallStatus, "completed")) {
      respondContent(response, "");
      return;
    }

    Say say1 = new Say.Builder(
        "It appears that no agent is available. " + "Please leave a message after the beep")
            .language(Say.Language.EN_GB)
            .voice(Say.Voice.ALICE)
            .build();

    Record record = new Record.Builder()
        .maxLength(20)
        .action("/agents/hangup")
        .transcribeCallback(String.format("/records/create?agentId=%s", agentId))
        .build();

    Say say2 = new Say.Builder("No record received. Goodbye")
        .language(Say.Language.EN_GB)
        .voice(Say.Voice.ALICE)
        .build();

    Hangup hangup = new Hangup();

    VoiceResponse voiceResponse = new VoiceResponse.Builder()
        .say(say1)
        .record(record)
        .say(say2)
        .hangup(hangup)
        .build();

    respondTwiML(response, voiceResponse);
  }
}
src/main/java/com/twilio/ivrrecording/servlet/agent/CallServlet.java
Record a call

src/main/java/com/twilio/ivrrecording/servlet/agent/CallServlet.java

Finally, we will see how to view an agent's voicemail.

Viewing an agent's voicemail

Once we look up the Agent, all we need to do is display their recordings. We bind the agent, along with their recordings, to a View.

It is possible to look up recordings via the Twilio REST API, but since we have all of the data we need in the transcribeCallback request, we can easily store it ourselves and save a roundtrip.

Loading Code Samples...
Language
package com.twilio.ivrrecording.servlet.agent;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.models.Agent;
import com.twilio.ivrrecording.repositories.AgentRepository;
import com.twilio.ivrrecording.servlet.WebAppServlet;

@SuppressWarnings("unused")
public class AgentsServlet extends WebAppServlet {

  private AgentRepository agentRepository;

  public AgentsServlet() {
    this(new AgentRepository());
  }

  public AgentsServlet(AgentRepository agentRepository) {
    this.agentRepository = agentRepository;
  }

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

    Iterable<Agent> agents = agentRepository.findAll();

    request.setAttribute("agents", agents);
    request.getRequestDispatcher("/agents.jsp").forward(request, response);
  }
}
src/main/java/com/twilio/ivrrecording/servlet/agent/AgentsServlet.java
Get the agent index page

src/main/java/com/twilio/ivrrecording/servlet/agent/AgentsServlet.java

That's it! We've just implemented an IVR with real Agents, call screening and voicemail.

Where to Next?

If you're a Java developer working with Twilio, you might want to check out these other tutorials.

Part 1 of this Tutorial: ET Phone Home Service - IVR Phone Trees

Increase your rate of response by automating the workflows that are key to your business.

SMS and MMS Notifications

Send out SMS (and MMS) notifications to a list of server administrators.

Did this Help?

Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Connect with us on Twitter and let us know what you build!

Mario Celi
Kat King
Agustin Camino
Andrew Baker

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

1 / 1
Loading Code Samples...
package com.twilio.ivrrecording.servlet.extension;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.models.Agent;
import com.twilio.ivrrecording.repositories.AgentRepository;
import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.*;
import com.twilio.twiml.Number;

public class ConnectServlet extends WebAppServlet {

  private AgentRepository agentRepository;

  public ConnectServlet() {
    this(new AgentRepository());
  }

  public ConnectServlet(AgentRepository agentRepository) {
    this.agentRepository = agentRepository;
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String selectedOption = request.getParameter("Digits");

    Agent agent = findAgentByExtension(selectedOption);
    if (agent == null) {
      redirectToMenu(response);
    } else {
      Say say = new Say.Builder("You'll be connected shortly to your planet.")
          .voice(Say.Voice.ALICE)
          .language(Say.Language.EN_GB)
          .build();

      Number number = new Number.Builder(agent.getPhoneNumber())
          .url("/agents/screen-call")
          .build();

      Dial dial = new Dial.Builder()
          .action(String.format("/agents/call?agentId=%s", agent.getId()))
          .number(number)
          .build();

      VoiceResponse voiceResponse = new VoiceResponse.Builder().say(say).dial(dial).build();

      respondTwiML(response, voiceResponse);
    }
  }

  private void redirectToMenu(HttpServletResponse response) throws IOException {
    Redirect redirect = new Redirect.Builder().url("/ivr/welcome").build();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().redirect(redirect).build();

    respondTwiML(response, voiceResponse);
  }

  private Agent findAgentByExtension(String extension) {
    Map<String, String> planetsExtensions = new HashMap<>();
    planetsExtensions.put("2", "Brodo");
    planetsExtensions.put("3", "Dagobah");
    planetsExtensions.put("4", "Oober");

    return planetsExtensions.containsKey(extension)
        ? agentRepository.findByExtension(planetsExtensions.get(extension)) : null;
  }
}
package com.twilio.ivrrecording.servlet.extension;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.models.Agent;
import com.twilio.ivrrecording.repositories.AgentRepository;
import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.*;
import com.twilio.twiml.Number;

public class ConnectServlet extends WebAppServlet {

  private AgentRepository agentRepository;

  public ConnectServlet() {
    this(new AgentRepository());
  }

  public ConnectServlet(AgentRepository agentRepository) {
    this.agentRepository = agentRepository;
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String selectedOption = request.getParameter("Digits");

    Agent agent = findAgentByExtension(selectedOption);
    if (agent == null) {
      redirectToMenu(response);
    } else {
      Say say = new Say.Builder("You'll be connected shortly to your planet.")
          .voice(Say.Voice.ALICE)
          .language(Say.Language.EN_GB)
          .build();

      Number number = new Number.Builder(agent.getPhoneNumber())
          .url("/agents/screen-call")
          .build();

      Dial dial = new Dial.Builder()
          .action(String.format("/agents/call?agentId=%s", agent.getId()))
          .number(number)
          .build();

      VoiceResponse voiceResponse = new VoiceResponse.Builder().say(say).dial(dial).build();

      respondTwiML(response, voiceResponse);
    }
  }

  private void redirectToMenu(HttpServletResponse response) throws IOException {
    Redirect redirect = new Redirect.Builder().url("/ivr/welcome").build();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().redirect(redirect).build();

    respondTwiML(response, voiceResponse);
  }

  private Agent findAgentByExtension(String extension) {
    Map<String, String> planetsExtensions = new HashMap<>();
    planetsExtensions.put("2", "Brodo");
    planetsExtensions.put("3", "Dagobah");
    planetsExtensions.put("4", "Oober");

    return planetsExtensions.containsKey(extension)
        ? agentRepository.findByExtension(planetsExtensions.get(extension)) : null;
  }
}
package com.twilio.ivrrecording.servlet.extension;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.models.Agent;
import com.twilio.ivrrecording.repositories.AgentRepository;
import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.*;
import com.twilio.twiml.Number;

public class ConnectServlet extends WebAppServlet {

  private AgentRepository agentRepository;

  public ConnectServlet() {
    this(new AgentRepository());
  }

  public ConnectServlet(AgentRepository agentRepository) {
    this.agentRepository = agentRepository;
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String selectedOption = request.getParameter("Digits");

    Agent agent = findAgentByExtension(selectedOption);
    if (agent == null) {
      redirectToMenu(response);
    } else {
      Say say = new Say.Builder("You'll be connected shortly to your planet.")
          .voice(Say.Voice.ALICE)
          .language(Say.Language.EN_GB)
          .build();

      Number number = new Number.Builder(agent.getPhoneNumber())
          .url("/agents/screen-call")
          .build();

      Dial dial = new Dial.Builder()
          .action(String.format("/agents/call?agentId=%s", agent.getId()))
          .number(number)
          .build();

      VoiceResponse voiceResponse = new VoiceResponse.Builder().say(say).dial(dial).build();

      respondTwiML(response, voiceResponse);
    }
  }

  private void redirectToMenu(HttpServletResponse response) throws IOException {
    Redirect redirect = new Redirect.Builder().url("/ivr/welcome").build();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().redirect(redirect).build();

    respondTwiML(response, voiceResponse);
  }

  private Agent findAgentByExtension(String extension) {
    Map<String, String> planetsExtensions = new HashMap<>();
    planetsExtensions.put("2", "Brodo");
    planetsExtensions.put("3", "Dagobah");
    planetsExtensions.put("4", "Oober");

    return planetsExtensions.containsKey(extension)
        ? agentRepository.findByExtension(planetsExtensions.get(extension)) : null;
  }
}
package com.twilio.ivrrecording.servlet.agent;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.Gather;
import com.twilio.twiml.Hangup;
import com.twilio.twiml.Say;
import com.twilio.twiml.VoiceResponse;

public class ScreenCallServlet extends WebAppServlet {

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

    String from = request.getParameter("From");

    String incomingCallMessage = "You have an incoming call from: " + getSpelledPhoneNumber(from);
    String gatherMessage = incomingCallMessage + ".Press any key to accept";

    Say sayInGather = new Say.Builder(gatherMessage).build();
    Gather gather = new Gather.Builder()
        .numDigits(1)
        .action("/agents/message")
        .say(sayInGather)
        .build();

    Say say = new Say.Builder("Sorry. Did not get your response").build();
    Hangup hangup = new Hangup();

    VoiceResponse voiceResponse = new VoiceResponse.Builder()
        .gather(gather)
        .say(say)
        .hangup(hangup)
        .build();

    respondTwiML(response, voiceResponse);
  }

  private String getSpelledPhoneNumber(String phoneNumber) {
    return String.join(", ", phoneNumber.split(""));
  }
}
package com.twilio.ivrrecording.servlet.agent;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.Hangup;
import com.twilio.twiml.Record;
import com.twilio.twiml.Say;
import com.twilio.twiml.VoiceResponse;

public class CallServlet extends WebAppServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String dialCallStatus = request.getParameter("DialCallStatus");
    String agentId = request.getParameter("agentId");

    if (Objects.equals(dialCallStatus, "completed")) {
      respondContent(response, "");
      return;
    }

    Say say1 = new Say.Builder(
        "It appears that no agent is available. " + "Please leave a message after the beep")
            .language(Say.Language.EN_GB)
            .voice(Say.Voice.ALICE)
            .build();

    Record record = new Record.Builder()
        .maxLength(20)
        .action("/agents/hangup")
        .transcribeCallback(String.format("/records/create?agentId=%s", agentId))
        .build();

    Say say2 = new Say.Builder("No record received. Goodbye")
        .language(Say.Language.EN_GB)
        .voice(Say.Voice.ALICE)
        .build();

    Hangup hangup = new Hangup();

    VoiceResponse voiceResponse = new VoiceResponse.Builder()
        .say(say1)
        .record(record)
        .say(say2)
        .hangup(hangup)
        .build();

    respondTwiML(response, voiceResponse);
  }
}
package com.twilio.ivrrecording.servlet.agent;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.servlet.WebAppServlet;
import com.twilio.twiml.Hangup;
import com.twilio.twiml.Record;
import com.twilio.twiml.Say;
import com.twilio.twiml.VoiceResponse;

public class CallServlet extends WebAppServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String dialCallStatus = request.getParameter("DialCallStatus");
    String agentId = request.getParameter("agentId");

    if (Objects.equals(dialCallStatus, "completed")) {
      respondContent(response, "");
      return;
    }

    Say say1 = new Say.Builder(
        "It appears that no agent is available. " + "Please leave a message after the beep")
            .language(Say.Language.EN_GB)
            .voice(Say.Voice.ALICE)
            .build();

    Record record = new Record.Builder()
        .maxLength(20)
        .action("/agents/hangup")
        .transcribeCallback(String.format("/records/create?agentId=%s", agentId))
        .build();

    Say say2 = new Say.Builder("No record received. Goodbye")
        .language(Say.Language.EN_GB)
        .voice(Say.Voice.ALICE)
        .build();

    Hangup hangup = new Hangup();

    VoiceResponse voiceResponse = new VoiceResponse.Builder()
        .say(say1)
        .record(record)
        .say(say2)
        .hangup(hangup)
        .build();

    respondTwiML(response, voiceResponse);
  }
}
package com.twilio.ivrrecording.servlet.agent;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twilio.ivrrecording.models.Agent;
import com.twilio.ivrrecording.repositories.AgentRepository;
import com.twilio.ivrrecording.servlet.WebAppServlet;

@SuppressWarnings("unused")
public class AgentsServlet extends WebAppServlet {

  private AgentRepository agentRepository;

  public AgentsServlet() {
    this(new AgentRepository());
  }

  public AgentsServlet(AgentRepository agentRepository) {
    this.agentRepository = agentRepository;
  }

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

    Iterable<Agent> agents = agentRepository.findAll();

    request.setAttribute("agents", agents);
    request.getRequestDispatcher("/agents.jsp").forward(request, response);
  }
}