package tut; import java.util.Locale; import com.ibm.telephony.beans.directtalk.ApplicationProperties; import com.ibm.telephony.beans.media.*; import com.ibm.telephony.wvr.*; /** * * A simple WVR application */ public class InApp extends WVRApplication { // Define the category for all the voice segments in this application. private static final String CATEGORY = "Tutorials"; // Define the menu items private static final String ITEM1_MSG = "Message"; private static final String ITEM2_NUMBER = "Number"; private static final String ITEM3_ORDER = "Order"; private static final String ITEM4_OPERATOR = "Operator"; private static final String ITEM5_HANGUP = "Hangup"; // Create an array to store the menu items private static final String[] MENU_ITEMS = { ITEM1_MSG, ITEM2_NUMBER, ITEM3_ORDER, ITEM4_OPERATOR, ITEM5_HANGUP }; // Create an array to represent the keys that will be used to select the menu items private static final String[] MENU_KEYS = { "1", "2", "3", "4", "5" }; // Create a number of Voice Segment objects that are shared amongst all instances of this // application within the JVM. // Create the Welcome and Difficulties segment objects private static final VoiceSegment VS_WELCOME = new VoiceSegment(CATEGORY, "Welcome"); private static final VoiceSegment VS_DIFFICULTIES = new VoiceSegment(CATEGORY, "Difficulties"); // Create the segment objects for the menu items, and store them in an array private static final MediaType[] MENU_PROMPTS = { new VoiceSegment(CATEGORY, "Message"), new VoiceSegment(CATEGORY, "Number"), new VoiceSegment(CATEGORY, "Order"), new VoiceSegment(CATEGORY, "Operator"), new VoiceSegment(CATEGORY, "Hangup") }; // Create the segment objects required for the menu properties private static final VoiceSegment VS_MENU_INVALID = new VoiceSegment(CATEGORY, "InvalidKey"); private static final VoiceSegment VS_MENU_ERROR = new VoiceSegment(CATEGORY, "Error"); private static final VoiceSegment VS_MENU_ONEMORE = new VoiceSegment(CATEGORY, "OneMore"); private static final VoiceSegment VS_MENU_HEADER = new VoiceSegment(CATEGORY, "MenuHeader"); private static final VoiceSegment VS_MENU_FOOTER = new VoiceSegment(CATEGORY, "MenuFooter"); // Create the segment objects for the hangup menu item private static final VoiceSegment VS_THANKCALLING = new VoiceSegment(CATEGORY, "ThankCalling"); // Create the segment objects for the record a message menu item private static final VoiceSegment VS_RECORD = new VoiceSegment(CATEGORY, "Record"); private static final VoiceSegment VS_PRESS_KEY = new VoiceSegment(CATEGORY, "PressKey"); private static final VoiceSegment VS_CALLER_MSG = new VoiceSegment(CATEGORY, "CallerMsg"); private static final VoiceSegment VS_RESPOND = new VoiceSegment(CATEGORY, "Respond");
// Create the segment objects for the key in a telephone number menu item private static final VoiceSegment VS_TEL_NO = new VoiceSegment(CATEGORY, "TelNo"); private static final VoiceSegment VS_THANK_YOU = new VoiceSegment(CATEGORY, "ThankYou"); private static final VoiceSegment VS_ONE_MORE_TIME = new VoiceSegment(CATEGORY, "OneMoreTime"); private static final VoiceSegment VS_NO_VALID_INPUT = new VoiceSegment(CATEGORY, "NoValidInput"); private static final VoiceSegment VS_NUMBER_INVALID = new VoiceSegment(CATEGORY, "NumberInvalid"); private static final VoiceSegment VS_NOT_ENOUGH = new VoiceSegment(CATEGORY, "NotEnough"); private static final VoiceSegment VS_CALL_BACK = new VoiceSegment(CATEGORY, "CallBack");
// Create an InputValidator to validate the caller's telephone number private static final InputValidator PHONE_NUM_CHECKER = new InputValidator() { public String validate(InputResult result) { String value = result.getValue(); if (value.substring(0, 1).equals("2")) return value; return null; } }; // Create some attribute objects for the menu private PlayAttributes menuPlayAtts = new PlayAttributes(); private MenuAttributes menuAtts = new MenuAttributes(MENU_PROMPTS, MENU_ITEMS, MENU_KEYS, null); private DTMFAttributes menuDTMFAtts = new DTMFAttributes(); private RecoAttributes menuRecoAtts = null;
// Create some attribute objects for getting the caller's telephone number private PlayAttributes telNoPlayAtts = new PlayAttributes(); private InputAttributes telNoInputAtts = new InputAttributes(); private DTMFAttributes telNoDTMFAtts = new DTMFAttributes(); private RecoAttributes telNoRecoAtts = null; // Create a flag to control whether we continue to loop around in the voiceMain() method taking calls private boolean keepTakingCalls = true; // Create a constructor method to set up the attributes public InApp() { // Set the input attributes of the menu menuAtts.setHeaderMessage(VS_MENU_HEADER); menuAtts.setFooterMessage(VS_MENU_FOOTER); menuAtts.setInvalidInputMessage(VS_MENU_INVALID); menuAtts.setOneMoreRepeatMessage(VS_MENU_ONEMORE); menuAtts.setNoMoreRepeatsMessage(VS_MENU_ERROR); menuAtts.setTimeout(10); menuAtts.setNumberOfRepeats(2); // Set the DTMF attributes of the menu menuDTMFAtts.setDelimiterKeys(""); menuDTMFAtts.setMaximumKeys(1);
// Set the input attributes for getting the telephone number telNoInputAtts.setMessage(VS_TEL_NO); telNoInputAtts.setTimeoutMessage(VS_NOT_ENOUGH); telNoInputAtts.setInvalidInputMessage(VS_NUMBER_INVALID); telNoInputAtts.setOneMoreRepeatMessage(VS_ONE_MORE_TIME); telNoInputAtts.setNoMoreRepeatsMessage(VS_NO_VALID_INPUT); telNoInputAtts.setTimeout(10); telNoInputAtts.setNumberOfRepeats(2); telNoInputAtts.setValidator(PHONE_NUM_CHECKER);
// Set the DTMF attributes for getting the telephone number
telNoDTMFAtts.setDelimiterKeys("");
telNoDTMFAtts.setMaximumKeys(6);
}//InApp
/**
* @see com.ibm.telephony.wvr.WVRApplication#voiceMain()
*/
public void voiceMain() throws WVRException {
// Define the Call object
Call call = null;
// Set the application properties
ApplicationProperties applicationProperties = null;
applicationProperties = this.getApplicationProperties();
// Create the WVR object and set the wait time
WVR wvr = new WVR(this, applicationProperties);
wvr.setWaitTime(-1);
// Create a variable which will be used to give each call a unique number
int callNumber = 0;
try {
while (keepTakingCalls) {
// Increment callNumber
callNumber++;
// Wait for a call
call = wvr.waitForCall("Call: " + callNumber);
// Create a flag to indicate whether the call is in progress
boolean callInProgress = true;
try {
// Play welcome message
call.play(VS_WELCOME);
while (callInProgress) {
// Play the menu and get the caller's response.
InputResult input = call.playAndGetInput(menuPlayAtts, menuAtts, menuDTMFAtts, menuRecoAtts);
String choice = input.getValue();
// Check which menu option has been selected and take // the appropriate action. Each action returns a boolean // stating whether the call should continue or not if (choice.equals(ITEM1_MSG)) { // Menu item 1 - record a message callInProgress = recordMessage(call); }
else if (choice.equals(ITEM2_NUMBER)) { // Menu item 2 - leave a telephone number callInProgress = leavePhoneNumber(call); } else if (choice.equals(ITEM5_HANGUP)) { // Menu item 5 - hang up callInProgress = hangUp(call); } else { // Print out the caller's choice. System.out.println("Caller chose option " + choice); }//if }//while } catch (WVRHungUpException e) { // Caller has hung up - ignore exception, program will go to finally block } catch (WVRInvalidInputException e) { // Caller has entered invalid input - ignore exception, application will go to finally block } catch (WVRException e) { // Check if Call object has been created. If yes, play the Difficulties message. if (call.isActive()) call.play(VS_DIFFICULTIES); // Print debugging information e.printStackTrace(); } finally { // Return the call call.returnCall(); }//try }//while } catch (WVRException e) { // Print debugging information e.printStackTrace(); }//try }//voiceMain() /** * Method recordMessage. * @param call * @return boolean */ private boolean recordMessage(Call call) throws WVRException { // Play segments to instruct the caller on how to record their message call.play(VS_RECORD); call.play(VS_PRESS_KEY); // Record the message call.record(VS_CALLER_MSG, 180); // Play a message to acknowledge the recording call.play(VS_RESPOND); return true; }//recordMessage()
/** * Method leavePhoneNumber. * @param call * @return boolean */
private boolean leavePhoneNumber(Call call) throws WVRException {
try {
// Get the caller's input - the Input Validator class created earlier will check to make sure the // number begins with a 2 InputResult result = call.playAndGetInput(telNoPlayAtts, telNoInputAtts, telNoDTMFAtts, telNoRecoAtts); } catch (WVRInvalidInputException e) {
return true; }//try
// Play the ThankYou message to acknowledge a valid number call.play(VS_THANK_YOU); call.play(VS_CALL_BACK); return true; }//leavePhoneNumber() /** * Method hangUp. * @param call * @return boolean */ private boolean hangUp(Call call) throws WVRException { // Thank the user for calling call.play(VS_THANKCALLING); return false; }//hangUp() // Tell the application to stop taking calls when the node shuts down public void voiceAppStop() { keepTakingCalls = false; } }//InApp