Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java / JavaSE / J2SE4

IVR applications based on Voicent Gateway (Java Sample Interface)

4.33/5 (2 votes)
10 Jun 2008CPOL6 min read 1  
Since all these functions are implemented as an HTTP client communicating directly with the Voicent Gateway, they can be run on any machine that has a connection to the host running the gateway. The C# interface source code is also included.

IVR Introduction

Interactive Voice Response (IVR) applications enable callers to interact with any software, such as query and modify database information, over the telephone. Callers can use their touch-tone pad to input requests, or just say what they want to do, such as requesting account balance information. IVR systems usually employ text-to-speech software to read information back.

Voicent Gateway Background

Voicent Gateway is ideal for developing interactive telephony applications such as Voicent AutoReminder and Voicent BroadcastByPhone. Based on the W3C VoiceXML standard, Voicent Gateway enables interactive voice access to the web and enterprise systems from any telephone.

Voicent Gateway can be deployed on any Windows 2000/2003/XP/Vista PC, making it possible to develop and deploy Voicent based solutions at a cost most small business and organizations can afford. The gateway also supports multiple phone lines by using multiple modems on a single computer. For large scale applications, multiple gateways in different locations can be networked together through the web interface.

Voicent Gateway requires no additional hardware when used with Skype. Calls are made over the Internet to landline or cell phones. The gateway also supports regular analog phone lines through the use of a voice modem. You can combine Skype and voice modems together to easily increase system capacity.

Voicent Gateway contains an outbound call scheduler which can be accessed through its HTTP interface. This interface enables easy integration with almost any application and with almost any programming language.

VoiceXML tutorial and samples are provided under the developer section of this website. For setup and options, please see Voicent Gateway User Guide. A free version of the product is also available for download.

You can also use IVR Studio to develop your IVR applications. This tool enables flexible application development without any knowledge of VoiceXML. All you need is point and click to draw a call flow diagram.

Using the IVR API

Since all these functions are implemented as an HTTP client communicating directly with the 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 the Voicent Gateway Simple Outbound Call Interface.

callText

Java
String callText(String phoneno, String text, boolean selfdelete)

Make a phone call and play the specified text message. The text message is converted to voice by Voicent Gateway's text-to-speech engine.

The options are:

phonenoThe phone number to call
textThe message for the phone call
selfdeleteAsk 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

Java
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.

Java
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.

callAudio

Java
String callAudio(String phoneno, String audiofile, boolean selfdelete)

Make a phone call and play the specified audio message.

The options are:

phonenoThe phone number to call.
audiofileThe 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 the Voicent Gateway.
selfdeleteAsk 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

Java
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.

Java
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.

callStatus

Java
String callStatus(String reqId)

Check the call status of the call with <reqId>. If the call is made, the return value is 'Call Made', or if the call fails, 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

Java
String status = callStatus("11234035434");

callRemove

Java
void callRemove(String reqId)

Remove the call record of the call with reqId. If the call is not made yet, it will be removed also.

Example

Java
allRemove("11234035434");

callTillConfirm

Java
void callTillConfirm(String vcastexe, String vocfile, String wavfile, String ccode)

Keep calling a list of people until anyone enters the confirmation code. The message is the specified audio file. This is ideal for using in a phone notification escalation process.

To use this feature, the 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 invoked 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:

vcastexeThe BroadcastByPhone program. It is usually located at 'C:\Program Files\Voicent\BroadcastByPhone\bin\vcast.exe' on the gateway host.
vocfileThe BroadcastByPhone call list to use.
ccodeThe confirmation code used for the broadcast
wavfileThe audio file to use for the broadcast

Example

Java
CallTillConfirm(
"C:\Program Files\Voicent\BroadcastByPhone\bin\vcast.exe",
"C:\My calllist\escalation.voc",
"C:\My calllist\escalation.wav","911911");

This will invoke the BroadcastByPhone program on the gateway host and start calling everyone on 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.

Source Code

Java
------------------
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_;

}

Points of Interest about IVR

Ideal Inbound & Outbound IVR Solution

  • Self-service: Allows callers to get answers to standard enquiries simply and easily, and in seconds, without the need for an agent.
  • Reach the right agent: Automatically capture relevant information from your callers and direct them to the appropriate agent to handle their call.
  • 24/7 customer service: Enable your customers to get the information they need, when they need it. Your IVR application is working even when you're not, or it can transfer calls to your cell phone.
  • Automated Outbound IVR: Fully integrated with BroadcastByPhone Autodialer. Fully automated interactive outbound call applications to generate sales leads and keep in touch with your customers.

Inbound & Outbound IVR Solution Key Features

  • Point-and-click call flow design
  • Deployed on any PC with Windows 2000/2003/XP/Vista
  • Transfer call to any phone, such as your cell phone
  • Interactive touch tone response
  • Speech command response
  • Automatically convert text to speech
  • Easy integration with your website
  • (Developer feature) Integrate with any program through Java
  • Support Skype or voice modems for making calls
  • Support single phone line or multiple phone lines
  • Natural Text-to-Speech engine for playing any text over the phone

IVR History

None.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)