3. Interactive Voice Response system
Note:
The code presented on this page is complete and needs no modifications to function. We are providing it for educational purposes and to highlight how it works and what it's doing. You may modify the code to customize the behavior if you want, but this tutorial assumes the code remains as-is to complete successfully and behave as documented.
An Interactive Voice Response (or IVR) system is basically just a series of messages played and options offered, and the user is able to navigate through various menus and submenus based on which options are selected. If you've ever called a business or a store and a computer answered, that's an example of an IVR.
Since we want to call a phone number and then qualify them, that is, see if they want to speak to a sales representative, that's what we'll have our IVR do. Consider the following method, pulled from the code sample in the right panel:private SVAMLControl humanResponse() {
String SIP_MENU_OPTION = "1";
String NON_SIP_MENU_OPTION = "2";
String mainPrompt =
String.format(
"#tts[Hi, you awesome person! Press '%s' if you have performed this tutorial using a"
+ " sip infrastructure. Press '%s' if you have not used a sip infrastructure. Press"
+ " any other digit to end this call.]",
SIP_MENU_OPTION, NON_SIP_MENU_OPTION);
String repeatPrompt =
String.format(
"#tts[Again, simply press '%s' if you have used sip, press '%s' if you have not, or"
+ " press any other digit to end this call.]",
SIP_MENU_OPTION, NON_SIP_MENU_OPTION);
MenuOption option1 =
MenuOption.builder()
.setDtfm(DualToneMultiFrequency.valueOf(SIP_MENU_OPTION))
.setAction(MenuOptionAction.from(MenuOptionActionType.RETURN, SIP_MENU))
.build();
MenuOption option2 =
MenuOption.builder()
.setDtfm(DualToneMultiFrequency.valueOf(NON_SIP_MENU_OPTION))
.setAction(MenuOptionAction.from(MenuOptionActionType.RETURN, NON_SIP_MENU))
.build();
Collection<MenuOption> options = Arrays.asList(option1, option2);
return SVAMLControl.builder()
.setAction(
ActionRunMenu.builder()
.setBarge(false)
.setMenus(
Collections.singletonList(
Menu.builder()
.setId("main")
.setMainPrompt(mainPrompt)
.setRepeatPrompt(repeatPrompt)
.setRepeats(2)
.setOptions(options)
.build()))
.build())
.build();
}
SVAMLControl
object to create a SVAML response that plays the RunMenu
action. This action is configured to give a couple of options:- First, if you have a SIP infrastructure, you can press
1
on your phone to connect to that SIP address you configured earlier. - Second, if you don't have a SIP infrastructure, but you want to simulate what it would be like to connect, you can press
2
and a message will play before hanging up the call. - Third, you can press any other number and you will be thanked for your time and the call will end.
For the purposes of this tutorial, we kept the IVR very straightforward, but you could do a lot of complex routing and behavior if you wanted.
Serializing the response
Before we move on to testing, it's worth pointing out that the Sinch servers expect a JSON response to the call events. In theController.java
file, in the method that returns the responses, we have this line:String serializedResponse = webhooks.serializeWebhooksResponse(response.get());
If we didn't include this, the Sinch server wouldn't accept our responses.
Next steps
Now that you know about IVRs, let's get on to testing this app!