Developers: IVR, SMS, CRM, Dialers

C++ Interface

The Voicent C++ module 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.

Comparing with the SDK C++ API, this interface is quite limited

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.

Visit the C++ IVR API: Call Ivr

Call Text

Synopsis - CallText

CallText(const char* phoneno, const char* text, BOOL selfdelete = false)

Description - CallText

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.
<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', 1)
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.

CString reqId = CallText('123-4567', 'Hello, how are you doing', 0)
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 call_status to get the call status, or use call_remove to remove the call record.

Call Audio

Synopsis - CallAudio

CallAudio (const char* phoneno, const char* audiofile, BOOL selfdelete = false)

Description - CallAudio

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 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', 1)
Make a call to phone number '123-4567' and play the hello.wav file. Since the selfdelete bit is set to 1, the call request record in the gateway will be removed automatically after the call.


CString reqId = CallAudio('123-4567', 'C:\my audios\hello.wav', 0)
Make a call to phone number '123-4567' and play hello.wav. 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 call_status to get the call status, or use call_remove to remove the call record.

Call Status

Synopsis - CallStatus

CallStatus(const char* reqId)

Description - CallStatus

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', and for any other status, the return value is ' ' (empty string).

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

Cstring status = CallStatus('11234035434')

Call Remove

Synopsis - CallRemove

CallRemove(const char* reqId)

Description - CallRemove

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

Example - CallRemove

CallRemove('11234035434')

Call Till Confirm

Synopsis - CallTillConfirm

CallTillConfirm(const char* vcastexe, const char* vocfile, const char* confirmcode, const char* wavfile)

Description - CallTillConfirm

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.

Parameters

<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.
<confirmcode> - The confirmation code use for the broadcast.
<wavfile> - The audio file to use for the broadcast.

Example - CallTillConfirm

CallTillConfirm('C:\Program Files\Voicent\BroadcastByPhone\bin\vcast.exe', 'C:\My calllist\escalation.voc', '911911', 'C:\My calllist\escalation.wav');
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.

Source Code

---------------
File Voicent.h:
---------------

class Voicent
{
public:
  Voicent(const char* host, int port);
  ~Voicent();

  // ** Standard Operations **

  // Call Request. Return reqId of this call record on the gateway

  CString
CallText(
    const char* phoneno, // phone number
    const char* text,    // text message using text-to-speech
    BOOL selfDelete = false); // delete call record after call made

  // Call Request. Return reqId of this call record on the gateway

  CString
CallAudio(
    const char* phoneno, // phone number
    const char* wavfile, // audio file to play (PCM 16bit, 8KHz, mono)
    BOOL selfDelete = false); // delete call record after call made

  // Get call status

  CString
CallStatus(const char* reqId); // Call record reqId

  // Remove call record

  void
CallRemove(const char* reqId); // Call record ReqId

  // ** BroadcastByPhone **

  // Invoke BroadcastByPhone
  void
CallTillConfirm(
    const char* vcastexe, // BroadcastByPhone executable path
    const char* vocfile, // Broadcast file
    const char* ccode, // confirmation code
    const char* wavfile); // audio file (PCM 16bit, 8KHz, mono)

private:
  CString m_host;
  int m_port;
};
-----------------
File voicent.cpp:
-----------------
#include "stdafx.h"
#include "afxinet.h"
#include "voicent.h"


///////////////////////////////////////////////////////////////
// Utility Routines

// simple URLEncoding

static CString
URLEncode(CString ToCode)
{
  int max = (unsigned int)ToCode.GetLength();

  CString RetStr;
  for(int i=0;i<max;i++) {
    unsigned char c = ToCode[i];
    unsigned short asc = c;

    if(asc > 47 && asc < 58)
      RetStr += c;
    else if(asc > 64 && asc < 91)
      RetStr += c;
    else if(asc > 96 && asc < 123)
      RetStr+=c;
    else if (asc == 32)
      RetStr+="+";
    else {
      CString AddStr;
      AddStr.Format("%%%2x", asc);
      int iv = (int)AddStr.GetAt(1);
      if((int)AddStr.GetAt(1) == 32) {
        AddStr.SetAt(1,'0');
      }
      RetStr+=AddStr;
    }
  }

  return RetStr;
}

static BOOL
PostToGateway(
          const char* host, // host name
          int port, // port number
          const CString& urlstr, // call scheduler url
          const CString& poststr, // http post string
          CString& rcstr) // returned html page from gateway
{
  CInternetSession sess;
  try {
    CHttpConnection* conn = sess.GetHttpConnection(host, (INTERNET_PORT)port);
    CHttpFile* pFile = conn->OpenRequest(CHttpConnection::HTTP_VERB_POST, urlstr);
    pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded\r\n");
    pFile->SendRequest(NULL, 0, (void*)(const char*)poststr, strlen(poststr));

    DWORD dwRet;
    pFile->QueryInfoStatusCode(dwRet);
    if (dwRet >= 300) {
      rcstr = "Failed to connect to Voicent Gateway. Make sure it is started.";
      delete conn;
      delete pFile;
      return false;
    }

    // parse return value
    CString strRetBufLen;
    pFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, strRetBufLen);
    long max = atol((LPCSTR)strRetBufLen);
    if(max <= 0)
    max = pFile->GetLength();

    // Read Data
    char buf[1024];
    int len = pFile->Read(buf, 1024);
    rcstr = CString(buf, len);

    pFile->Close();
    conn->Close();
    delete pFile;
    delete conn;

    return true;
  }
  catch (CInternetException* ex) {
    char errbuf[1024];
    ex->GetErrorMessage(errbuf, 1024);
    rcstr = errbuf;
    ex->Delete();
    return false;
  }
}

// search for pattern [ReqId=*]
static CString
GetReqId(const CString& rcstr)
{
  int index1 = rcstr.Find("[ReqId=");
  if (index1 == -1)
    return "";
  index1 += 7;

  int index2 = rcstr.Find("]", index1);
  if (index2 == -1)
    return "";

  return rcstr.Mid(index1, index2 - index1);
}

// search for pattern ^made^ or ^failed^
static CString
GetCallStatus(const CString& rcstr)
{
  if (rcstr.Find("^made^") != -1)
    return "Call Made";

  if (rcstr.Find("^failed^") != -1)
    return "Call Failed";

  if (rcstr.Find("^retry^") != -1)
    return "Call Will Retry";

  return "";
}



////////////////////////////////////////////////////////////////////
// Voicent

Voicent::Voicent(const char* host, int port)
{
  m_host = host;
  m_port = port;
}

Voicent::~Voicent()
{
}


// Please refer to http://www.voicent.com/devnet/docs/simpleintf.htm
// for detailed information on these parameters

CString Voicent::
CallText(const char* phoneno, const char* text, BOOL selfDelete)
{
  // call request url
  CString urlstr = "/ocall/callreqHandler.jsp";

  // setting the http post string
  CString poststr;

  poststr += "info=";
  poststr += URLEncode(CString("Simple Text Call ") + phoneno);

  poststr += "&phoneno=";
  poststr += phoneno;

  poststr += "&firstocc=10";

  poststr += "&selfdelete=";
  poststr += (selfDelete ? "1" : "0");

  poststr += "&txt=";
  poststr += URLEncode(text);

  // Send Call Request
  CString rcstr;
  if (! PostToGateway(m_host, m_port, urlstr, poststr, rcstr))
    return "";

  return GetReqId(rcstr);
}

CString Voicent::
CallAudio(const char* phoneno, const char* audiofile, BOOL selfDelete)
{
  // call request url
  CString urlstr = "/ocall/callreqHandler.jsp";

  // setting the http post string
  CString poststr;

  poststr += "info=";
  poststr += URLEncode(CString("Simple Audio Call ") + phoneno);

  poststr += "&phoneno=";
  poststr += phoneno;

  poststr += "&firstocc=10";

  poststr += "&selfdelete=";
  poststr += (selfDelete ? "1" : "0");

  poststr += "&audiofile=";
  poststr += URLEncode(audiofile);

  // Send Call Request
  CString rcstr;
  if (! PostToGateway(m_host, m_port, urlstr, poststr, rcstr))
    return "";

  return GetReqId(rcstr);
}

CString Voicent::
CallStatus(const char* reqId)
{
  // call status url
  CString urlstr = "/ocall/callstatusHandler.jsp";

  // setting the http post string
  CString poststr = "reqid=";
  poststr += URLEncode(reqId);

  // Send Call Request
  CString rcstr;
  if (! PostToGateway(m_host, m_port, urlstr, poststr, rcstr))
    return "";

  return GetCallStatus(rcstr);
}

void Voicent::
CallRemove(const char* reqId)
{
  // call status url
  CString urlstr = "/ocall/callremoveHandler.jsp";

  // setting the http post string
  CString poststr = "reqid=";
  poststr += URLEncode(reqId);

  // Send Call Request
  CString rcstr;
  PostToGateway(m_host, m_port, urlstr, poststr, rcstr);
}

// Call scheduler can launch program also
void Voicent::
CallTillConfirm(const char* vcastexe,
  const char* vocfile,
  const char* ccode,
  const char* wavfile)
{
  // call request url
  CString urlstr = "/ocall/callreqHandler.jsp";

  // setting the http post string
  CString poststr;

  poststr += "info=";
  poststr += URLEncode("Simple Call till Confirm");

  poststr += "&phoneno=1111111"; // any number

  poststr += "&firstocc=10";
  poststr += "&selfdelete=0";

  poststr += "&startexec=";
  poststr += URLEncode(vcastexe);

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

  // add -cleanstatus if necessary

  poststr += "&cmdline=";
  poststr += URLEncode(cmdline);

  // Send Call Request
  CString rcstr;
  PostToGateway(m_host, m_port, urlstr, poststr, rcstr);
}