Home > java > IVR applications based on Voicent Gateway

IVR applications based on Voicent Gateway

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

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:

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.

callAudio

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

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

callStatus

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

String status = callStatus("11234035434");

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

Example

allRemove("11234035434");

callTillConfirm

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:

vcastexe The BroadcastByPhone program. It is usually located at ‘C:\Program Files\Voicent\BroadcastByPhone\bin\vcast.exe‘ on the gateway host.
vocfile The BroadcastByPhone call list to use.
ccode The confirmation code used for the broadcast
wavfile The audio file to use for the broadcast

Example

CallTillConfirm(
"C:\SRCNBLGC_VIRTUAL\Voicent\BroadcastByPhone\bin\vcast.exe",
"C:\SRCNBLGC_VIRTUAL\My calllist\escalation.voc",
"C:\SRCNBLGC_VIRTUAL\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:\SRCNBLGC_VIRTUAL\My calllist\escalation.voc‘. The audio message played is ‘C:\SRCNBLGC_VIRTUAL\My calllist\escalation.wav‘. And as soon as anyone on the list enters the confirmation code ‘911911’, the call will stop automatically.

Source Code

------------------
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:/SRCNBLGC_VIRTUAL/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:/SRCNBLGC_VIRTUAL/Voicent/"+
"BroadcastByPhone/bin/vcast.exe",
                            "C:/temp/testctf.voc",
                            "C:/SRCNBLGC_VIRTUAL/Voicent/MyRecordings/sample_message.wav",
                            "1234");
    }

    private String host_;
    private int port_;

}
Advertisements
Categories: java Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: