Voicent Gateway Java Simple 
              Interface
              The Voicent Java Simple Interface class contains the following functions. 
              
              Since all these functions are implemented as a HTTP client 
              communicating directly with Voicent Gateway, they can be run on 
              any machine that has a connection to the host running the gateway. 
              The Java interface source code is 
              included at the end of this section. 
              This Java Simple Interface is developed based on
              Voicent Gateway Simple Outbound Call 
              Interface. 
              
              Work with Interactive Applications
              For many applications, it is desirable to get responses from 
              callees in addition to deliver a message. The interaction can be 
              implemented by using the low level gateway VoiceXML interface. 
              However, using the low level API is a time consuming and error 
              prone task. A better solution is to utilize Voicent IVR Studio to 
              create an interactive application, and use the extended Simple 
              Call Interface to trigger a phone call. 
              
               
              SYNOPSIS 
              
                - 
                String callText(String phoneno, String text, boolean selfdelete)
 
               
              DESCRIPTION 
              
                - Make a phone call and play the specified text message. The 
                text message is convert to voice by Voicent Gateway's 
                text-to-speech engine.
 
  
                - The options are:
 
                -  
                  
                    |  phoneno | 
                    The phone number to call | 
                   
                  
                    |  
                    text | 
                    The message for the phone call | 
                   
                  
                    |  
                    selfdelete | 
                    Ask the gateway to automatically delete the 
                    call request after the call is made if it is set to '1' | 
                   
                 
                The return value is the call request id <reqId>.  
               
              EXAMPLE 
              
                - callText("123-4567", "Hello, how are you doing", 1);
 
                 
                Make a call to phone number '123-4567' and say 'Hello, how are 
                you doing'. Since the  selfdelete 
                bit is set, the call request record in the gateway will be 
                removed automatically after the call. 
  
                - String reqId = 
                callText("123-4567", "Hello, how are you", 0);
 
  
                - Make a call to phone number '123-4567' and say 'Hello, how 
                are you'. Since the  selfdelete 
                bit is not set, the call request record in the gateway will not 
                be removed after the call. You can then use 
                callStatus to get the call status, or use
                callRemove to remove the call record.
 
  
               
               
              SYNOPSIS 
              
                -  String 
                callAudio(String phoneno, String audiofile, boolean selfdelete)
 
               
              DESCRIPTION 
              
                - Make a phone call and play the specified audio message.
 
  
                - The options are:
 
                -  
                  
                    |  phoneno | 
                    The phone number to call | 
                   
                  
                    |  
                    audiofile | 
                    The audio message for the phone call. The 
                    format must be PCM 16bit, 8KHz, mono wave file. The audio 
                    file must be on the same host as Voicent Gateway. | 
                   
                  
                    |  
                    selfdelete | 
                    Ask the gateway to automatically delete the 
                    call request after the call is made if it is set to '1' | 
                   
                 
                The return value is the call request id <reqId>.  
               
              EXAMPLE 
              
                - callAudio("123-4567", "C:\my audios\hello.wav", 1);
 
                 
                Make a call to phone number '123-4567' and play the hello.wav 
                file. Since the  selfdelete 
                bit is set, the call request record in the gateway will be 
                removed automatically after the call. 
  
                - String reqId = 
                callAudio("123-4567", "C:\my audios\hello.wav", 0);
 
  
                - Make a call to phone number '123-4567' and play the 
                hello.wav file. Since the  selfdelete 
                bit is not set, the call request record in the gateway will not 
                be removed after the call. You can then use 
                callStatus to get the call status, or use
                callRemove to remove the call record.
 
  
                -  
 
                 
               
              SYNOPSIS 
              
                - String 
                callStatus(String reqId)
 
               
              DESCRIPTION 
              
                - Check the call status of the call with  <reqId>. 
                If the call is made, the return value is
                'Call Made', 
                or if the call is failed, the return value is
                'Call Failed', 
                or if the call will retry, the return value is "Call Will 
                Retry", and for any other status, the return value is
                "".
 
                 
                Please note that an empty string is returned if the call is 
                still in progress. You'll need to wait and then poll the status 
                again. 
               
              EXAMPLE 
              
                - String status = 
                callStatus("11234035434");
 
                -  
 
               
               
              SYNOPSIS 
              
                - void callRemove(String reqId)
 
               
              DESCRIPTION 
              
                - Remove the call record of the call with  reqId. 
                If the call is not made yet, it will be removed also.
 
               
              EXAMPLE 
              
                - callRemove("11234035434");
 
                -  
 
               
               
              SYNOPSIS 
              
                -  
                void callTillConfirm(String vcastexe, String vocfile, String 
                wavfile, String ccode)
 
               
              DESCRIPTION 
              
                - Keep calling a list of people until anyone enters the 
                confirmation code. The message is the specified audio file. This 
                is ideal for using it in a phone notification escalation 
                process.
 
                 
                To use this feature, Voicent BroadcastByPhone Professional 
                version has to be installed. This function is similar to the 
                command line interface BroadcastByPhone has. But since the 
                command cannot be invoke over a remote machine, this perl 
                function uses the gateway to schedule an event, which in turn 
                invokes the command on the gateway host. 
                 
                The options are: 
                 
 
                  
                    |  vcastexe | 
                    The BroadcastByPhone program. It is usually 
                    'C:\Program Files\Voicent\BroadcastByPhone\bin\vcast.exe' on the 
                    gateway host. | 
                   
                  
                    |  
                    vocfile | 
                    The BroadcastByPhone call list to use. | 
                   
                  
                    |  
                    ccode | 
                    The confirmation code use for the broadcast | 
                   
                  
                    |  
                    wavfile | 
                    The audio file to use for the broadcast | 
                   
                 
                
                 
               
              EXAMPLE 
              
                -  
                CallTillConfirm(
 
    "C:\Program Files\Voicent\BroadcastByPhone\bin\vcast.exe", 
    "C:\My calllist\escalation.voc", 
    "C:\My calllist\escalation.wav", 
                -    
                 
                "911911");
 
               
              
                - 
                
 
                This will invoke BroadcastByPhone program on the gateway host 
                and start calling everyone one the call list defined in  
                'C:\My calllist\escalation.voc'. The audio message played 
                is  'C:\My 
                calllist\escalation.wav'. And as soon as anyone on the 
                list enters the confirmation code  
                '911911', the call will stop automatically. 
                -  
 
               
               
              
                
                ------------------ 
                File Voicent.java: 
                ------------------ 
                 
                import java.net.URL; 
                import java.net.URLEncoder; 
                import java.net.HttpURLConnection; 
                import java.io.PrintWriter; 
                import java.io.InputStream; 
                 
                 
                public class Voicent 
                { 
  /** 
   * Constructor with default localhost:8155 
   */ 
  public Voicent() 
  { 
    host_ = "localhost"; 
    port_ = 8155; 
  } 
                 
  /** 
   * Constructor with Voicent gateway hostname and port. 
   * @param host Voicent gateway host machine 
   * @param port Voicent gateway port number 
   */ 
  public Voicent(String host, int 
                port) 
  { 
    host_ = host; 
    port_ = port; 
  } 
                 
  /** 
   * Make a call to the number specified and play the text message 
   * using text-to-speech engine. 
   * 
   * @param phoneno Phone number to call, exactly as it should be 
                dialed 
   * @param text Text to play over the phone using text-to-speech 
   * @param selfdelete After the call, delete the call request 
                automatically if set to 1 
   * @return Call request ID 
   */ 
  public String callText(String 
                phoneno, String text, boolean selfdelete) 
  { 
    // call request url 
    String urlstr = "/ocall/callreqHandler.jsp"; 
                 
    // setting the http post string 
    String poststr = "info="; 
    poststr += URLEncoder.encode("Simple Text Call " + phoneno); 
                 
    poststr += "&phoneno="; 
    poststr += phoneno; 
                 
    poststr += "&firstocc=10"; 
                 
    poststr += "&selfdelete="; 
    poststr += (selfdelete ? "1" : "0"); 
                 
    poststr += "&txt="; 
    poststr += URLEncoder.encode(text); 
                 
    // Send Call Request 
    String rcstr = postToGateway(urlstr, poststr); 
                 
    return getReqId(rcstr);  
                } 
                 
  /** 
   * Make a call to the number specified and play the audio file. The 
   * audio file should be of PCM 8KHz, 16bit, mono. 
   * 
   * @param phoneno Phone number to call, exactly as it should be 
                dialed 
   * @param audiofile Audio file path name 
   * @param selfdelete After the call, delete the call request 
                automatically if set to 1 
   * @return Call request ID 
   */ 
  public String callAudio(String 
                phoneno, String audiofile, boolean selfdelete) 
  { 
    // call request url 
    String urlstr = "/ocall/callreqHandler.jsp"; 
                 
    // setting the http post string 
    String poststr = "info="; 
    poststr += URLEncoder.encode("Simple Audio Call " + phoneno); 
                 
    poststr += "&phoneno="; 
    poststr += phoneno; 
                 
    poststr += "&firstocc=10"; 
                 
    poststr += "&selfdelete="; 
    poststr += (selfdelete ? "1" : "0"); 
                 
    poststr += "&audiofile="; 
    poststr += URLEncoder.encode(audiofile); 
                 
    // Send Call Request 
    String rcstr = postToGateway(urlstr, poststr); 
                 
    return getReqId(rcstr); 
                } 
                 
  /** 
   * Get call status of the call with the reqID. 
   * 
   * @param reqID Call request ID on the gateway 
   * @return call status 
   */ 
  public String callStatus(String 
                reqID) 
  { 
    // call status url 
    String urlstr = "/ocall/callstatusHandler.jsp"; 
                 
    // setting the http post string 
    String poststr = "reqid="; 
    poststr += URLEncoder.encode(reqID); 
                 
    // Send Call Request 
    String rcstr = postToGateway(urlstr, poststr); 
                 
    return getCallStatus(rcstr); 
  } 
                 
  /** 
   * Remove all request from the gateway 
   * 
   * @param reqID Call request ID on the gateway 
   * @return call status 
   */ 
  public void callRemove(String reqID) 
  { 
    // call status url 
    String urlstr = "/ocall/callremoveHandler.jsp"; 
                 
    // setting the http post string 
    String poststr = "reqid="; 
    poststr += URLEncoder.encode(reqID); 
                 
    // Send Call remove post 
    postToGateway(urlstr, poststr); 
  } 
                 
  /** 
   * Invoke BroadcastByPhone and start the call-till-confirm process 
   * 
   * @param vcastexe Executable file vcast.exe, BroadcastByPhone path 
                name 
   * @param vocfile BroadcastByPhone call list file 
   * @param wavfile Audio file used for the broadcast 
   * @param ccode Confirmation code 
   */ 
  public void callTillConfirm(String 
                vcastexe, String vocfile, String wavfile, String ccode) 
  { 
    // call request url 
    String urlstr = "/ocall/callreqHandler.jsp"; 
                 
    // setting the http post string 
    String poststr = "info="; 
    poststr += URLEncoder.encode("Simple Call till Confirm"); 
                 
    poststr += "&phoneno=1111111"; // any number 
                 
    poststr += "&firstocc=10"; 
    poststr += "&selfdelete=0"; 
                 
    poststr += "&startexec="; 
    poststr += URLEncoder.encode(vcastexe); 
                 
    String cmdline = "\""; 
    cmdline += vocfile; 
    cmdline += "\""; 
    cmdline += " -startnow"; 
    cmdline += " -confirmcode "; 
    cmdline += ccode; 
    cmdline += " -wavfile "; 
    cmdline += "\""; 
    cmdline += wavfile; 
    cmdline += "\""; 
                 
    // add -cleanstatus if necessary 
                 
    poststr += "&cmdline="; 
    poststr += URLEncoder.encode(cmdline); 
                 
    // Send like a Call Request 
    postToGateway(urlstr, poststr); 
  } 
                 
  private String postToGateway(String 
                urlstr, String poststr) 
  { 
    try { 
      URL url = new URL("http", host_, port_, urlstr); 
      HttpURLConnection conn = (HttpURLConnection) 
                url.openConnection(); 
                 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      conn.setRequestMethod("POST"); 
                 
      PrintWriter out = new 
                PrintWriter(conn.getOutputStream()); 
      out.print(poststr); 
      out.close(); 
                 
      InputStream in = conn.getInputStream(); 
                 
      StringBuffer rcstr = new StringBuffer(); 
      byte[] b = new byte[4096]; 
      int len; 
      while ((len = in.read(b)) != -1) 
        rcstr.append(new String(b, 0, len)); 
      return rcstr.toString(); 
    } 
    catch (Exception e) { 
      e.printStackTrace(); 
      return ""; 
    } 
  } 
                 
  private String getReqId(String 
                rcstr) 
  { 
    int index1 = rcstr.indexOf("[ReqId="); 
    if (index1 == -1) 
      return ""; 
    index1 += 7; 
                 
    int index2 = rcstr.indexOf("]", index1); 
    if (index2 == -1) 
      return ""; 
                 
    return rcstr.substring(index1, index2); 
  } 
                 
  private String getCallStatus(String 
                rcstr) 
  { 
    if (rcstr.indexOf("^made^") != -1) 
      return "Call Made"; 
                 
    if (rcstr.indexOf("^failed^") != -1) 
      return "Call Failed"; 
                 
    if (rcstr.indexOf("^retry^") != -1) 
      return "Call Will Retry"; 
                 
    return ""; 
  } 
                 
                 
  /* test usage */ 
  public static void main(String args[]) 
    throws InterruptedException 
  { 
    String mynumber = "1112222"; // replace with your own 
                 
    Voicent voicent = new Voicent(); 
    String reqId = voicent.callText(mynumber, 
                "hello, how are you", true); 
    System.out.println("callText: " + reqId); 
                 
    reqId = voicent.callAudio(mynumber, 
                "C:/Program    Files/Voicent/MyRecordings/sample_message.wav", 
                false); 
    System.out.println("callAudio: " + reqId); 
                 
    while (true) { 
      Thread.currentThread().sleep(30000); 
      String status = voicent.callStatus(reqId); 
      if (status.length() > 0) { 
        System.out.println(status); 
        voicent.callRemove(reqId); 
        break; 
      } 
    } 
                 
    voicent.callTillConfirm("C:/Program    
                Files/Voicent/BroadcastByPhone/bin/vcast.exe", 
          "C:/temp/testctf.voc", 
          "C:/Program 
                Files/Voicent/MyRecordings/sample_message.wav", 
          "1234"); 
                } 
                 
                 
  private String host_; 
  private int port_; 
                } 
 
               |