C# API | Simple Gateway API

  1. CallText
  2. CallAudio
  3. CallStatus
  4. CallRemove
  5. CallTillConfirm
  6. Source Code

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.

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.

C# IVR Interface

Call Text

Synopsis - CallText

string CallText(string phoneno, string text, bool selfdelete)

Description - CallText

Objective

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.

Parameters
phoneno The phone number to call.
textmessage The message for the phone call. This message will be converted to audio using Voicent's text-to-speech software.
selfdelete Ask the gateway to automatically delete the call request after the call is made if it is set to '1'.
Returns
reqId The return value is the call request ID.

Example - CallText


                        CallText("123-4567", "Hello, how are you doing", true)
                    

Makes a call to the phone number '123-4567' and says 'Hello, how are you doing'. Since the selfdelete bit is set to 1, the call request record in the gateway will be removed automatically after the call.



                        string reqId = CallText('123-4567', 'Hello, how are you doing', false)
                    

Make a call to phone number '123-4567' and say 'Hello, how are you doing'. Since the selfdelete bit is set to 0, 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.

Call Audio

Synopsis - CallAudio

string CallAudio (string phoneno, string audiofile, bool selfdelete)

Description - CallAudio

Objective

Make a phone call and play the specified audio message.

Parameters
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'.
Returns
reqId The return value is the call request ID.

Example - CallAudio


                        CallAudio("123-4567", "C:\my audios\hello.wav",true)
                    

Makes a call to the phone number '123-4567' and says 'Hello, how are you doing'. Since the selfdelete bit is set to 1, the call request record in the gateway will be removed automatically after the call.



                        string reqId = CallAudio("123-4567", "C:\my audios\hello.wav", false)
                    

Make a call to phone number '123-4567' and say 'Hello, how are you doing'. Since the selfdelete bit is set to 0, 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.

Call Status

Synopsis - CallStatus

CallStatus(string reqId)

Description - CallStatus

Objective

Check the call status given the reqId.

Parameters
reqId The call request ID.
Returns
'Call Made' Returns the string 'Call Made' if the call was made.
'Call Failed' Returns the string 'Call Failed' if the call failed.
' ' Returns an empty string if the call is in progress or if the call was neither made or failed.

Example - CallStatus


                        string status = CallStatus("11234035434")
                    

Call Remove

Synopsis - CallRemove

void CallRemove(string reqId)

Description - CallRemove

Objective

Remove the call record given the <reqId>. If the call is not made yet, it will also be removed.

Parameters
<reqId> The call request ID.

Example - CallRemove


                        CallRemove("11234035434")
                    

Call Till Confirm

Synopsis - CallTillConfirm

void CallTillConfirm(string vcastexe, string vocfile, string wavfile, string ccode)

Description - CallTillConfirm

Objective

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

Parameters
vcastexe The BroadcastByPhone program. It is usually C:\Program Files\Voicent\BroadcastByPhone\bin\vcast.exe on the gateway host.
vcastdoc The BroadcastByPhone call list to use.
wavfile The audio file to use for the broadcast.
ccode The confirm code to use for the broadcast.

Example - CallTillConfirm


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

Source Code


                        ----------------
                        File Voicent.cs:
                        ----------------
                        using System;
                        using System.Net;
                        using System.IO;


                        namespace Voicent
                        {
                        /// 
                            /// Interface class for making telephone calls using the simple
                            /// API of Voicent Gateway.
                            /// 
                        public class Voicent
                        {
                        /// 
                            /// Default constructor use http://localhost:8155
                            /// 
                        public Voicent()
                        {
                        m_host = "localhost";
                        m_port = 8155;
                        }

                        /// 
                            /// Constructor with host and port
                            /// 
                        /// Hostname of Voicent Gateway
                        /// port number of Voicent Gateway
                        public Voicent(string host, int port)
                        {
                        m_host = host;
                        m_port = port;
                        }

                        /// 
                            /// Call the specified phone number and play the text using
                            /// text-to-speech engine
                            /// 
                        /// telephone number to call
                        /// text message to play
                        /// if set to one, automatically remove call record on
                        /// gateway after the call is made
                        /// Call Request ID on gateway
                        public string CallText(string phoneno, string text, bool selfdelete)
                        {
                        // call request url
                        string urlstr = "/ocall/callreqHandler.jsp";

                        // setting the http post string
                        string poststr = "info=Simple Text Call " + phoneno;

                        poststr += "&phoneno=" + phoneno;
                        poststr += "&firstocc=10";
                        poststr += "&selfdelete=";
                        poststr += (selfdelete ? "1" : "0");

                        poststr += "&txt=" + text;

                        // Send Call Request
                        String rcstr = PostToGateway(urlstr, poststr);
                        return GetRequestID(rcstr);
                        }

                        /// 
                            /// Call the specified phone number and play the audio file
                            /// 
                        /// telephone number to call
                        /// audio file path name
                        /// if set to one, automatically remove call record on
                        /// gateway after the call is made
                        /// Call Request ID on gateway
                        public string CallAudio(string phoneno, string filename, bool selfdelete)
                        {
                        // call request url
                        string urlstr = "/ocall/callreqHandler.jsp";

                        // setting the http post string
                        string poststr = "info=Simple Audio Call " + phoneno;

                        poststr += "&phoneno=" + phoneno;
                        poststr += "&firstocc=10";
                        poststr += "&selfdelete=";
                        poststr += (selfdelete ? "1" : "0");

                        poststr += "&audiofile=" + filename;

                        // Send Call Request
                        String rcstr = PostToGateway(urlstr, poststr);
                        return GetRequestID(rcstr);
                        }

                        /// 
                            /// Get call request status
                            /// 
                        /// Call request ID
                        /// status code
                        public string CallStatus(string reqID)
                        {
                        // call status url
                        string urlstr = "/ocall/callstatusHandler.jsp";
                        string poststr = "reqid=" + reqID;

                        // Send Call Request
                        String rcstr = PostToGateway(urlstr, poststr);

                        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 "";
                        }

                        /// 
                            /// Remove the call request on the gateway
                            /// 
                        /// Call Request ID
                        public void CallRemove(string reqID)
                        {
                        // call status url
                        string urlstr = "/ocall/callremoveHandler.jsp";
                        string poststr = "reqid=" + reqID;

                        // Send Call Request
                        PostToGateway(urlstr, poststr);
                        }

                        /// 
                            /// Invoke Voicent BroadcastByPhone and start the call-till-confirm escalation process
                            /// 
                        /// BroadcastByPhone executable file path
                        /// BroadcastByPhone call list file path
                        /// Audio file, must be PCM 8KHz, 16bit, mono wave file format
                        /// Confirmation code, numbers only
                        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=Simple Call till Confirm";
                        poststr += "&phoneno=1111111"; // any number
                        poststr += "&firstocc=10";
                        poststr += "&selfdelete=0";
                        poststr += "&startexec=" + vcastexe;

                        string cmdline = "\"" + vocfile + "\" -startnow";
                        cmdline += " -confirmcode " + ccode;
                        cmdline += " -wavfile " + "\"" + wavfile + "\"";

                        // add -cleanstatus if necessary

                        poststr += "&cmdline=" + cmdline;

                        PostToGateway(urlstr, poststr);
                        }


                        protected string PostToGateway(string urlstr, string poststr)
                        {
                        Uri url = new Uri("http://" + m_host + ":" + m_port.ToString() + urlstr);
                        HttpWebRequest HttpWRequest = (HttpWebRequest) WebRequest.Create(url);

                        HttpWRequest.Headers.Set("Pragma", "no-cache");
                        HttpWRequest.Timeout = 60000;
                        HttpWRequest.Method = "POST";
                        HttpWRequest.ContentType = "application/x-www-form-urlencoded";

                        byte[] PostData = System.Text.Encoding.ASCII.GetBytes(poststr);
                        HttpWRequest.ContentLength = PostData.Length;
                        Stream tempStream = HttpWRequest.GetRequestStream();
                        tempStream.Write(PostData,0,PostData.Length);
                        tempStream.Close();

                        HttpWebResponse HttpWResponse = (HttpWebResponse) HttpWRequest.GetResponse();
                        Stream receiveStream = HttpWResponse.GetResponseStream();
                        StreamReader readStream = new StreamReader(receiveStream);

                        string rcstr = "";
                        Char[] read = new Char[256];
                        int count = 0;
                        while ((count = readStream.Read(read, 0, 256)) > 0) {
                        rcstr += new String(read, 0, count);
                        }
                        HttpWResponse.Close();
                        readStream.Close();

                        return rcstr;
                        }

                        protected string GetRequestID(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 - index1);
                        }


                        private string m_host;
                        private int m_port;
                        }
                        }
                        --------------------
                        File TextVoicent.cs:
                        --------------------
                        using System;
                        using System.Threading;
                        using Voicent;

                        namespace csapi
                        {
                        /// 
                            /// Simple class to test Voicent C# Simple Interface
                            /// 
                        class TestVoicent
                        {
                        /// 
                            /// The main entry point for the application.
                            /// 
                        [STAThread]
                        static void Main(string[] args)
                        {
                        string phoneno = "8147838"; // Replace it with your number

                        Voicent.Voicent voicent = new Voicent.Voicent();

                        // Test CallText
                        string reqId = voicent.CallText(phoneno, "Hello, how are you", true);
                        Console.WriteLine("Call request ID = " + reqId);

                        // Test CallAudio
                        reqId = voicent.CallAudio(phoneno, "C:/Program Files/Voicent/MyRecordings/sample_message.wav", false);
                        Console.WriteLine("Call request ID = " + reqId);

                        // try to get status
                        while (true) {
                        Thread.Sleep(20000); // wair for 20 seconds
                        string status = voicent.CallStatus(reqId);
                        Console.WriteLine("Call Status: " + status);
                        if (status.Length != 0)
                        break;
                        }

                        // remove the call request on the gateway
                        voicent.CallRemove(reqId);

                        // Test call-till-confirm
                        voicent.CallTillConfirm("C:/Program Files/Voicent/BroadcastByPhone/bin/vcast.exe",
                        "C:/temp/testctf.voc",
                        "C:/Program Files/Voicent/MyRecordings/sample_message.wav",
                        "12345");
                        }
                        }
                        }