Step By Step instructions to write your first IVR Application – II

by Uttam Pegu on May 14, 2009

In my previous post about Step By Step instructions to write your first IVR Application , I wrote about installing the Dialogic D4 PCI card on PCI slot and then installing Dialogic System Release on a Windows XP machine. In this part, I intend to provide the C++ code to initialise the card, make it ready to accept a call and then when it rings, pick the call and play an WAV file!

1. Setup Visual Studio 6.0. Please do not forget to select install the Visual C++ 6.0.

2. It is assumed that you know how to use Visual Studio C++ 6.0. Start a new project IVRS. You will have to include Dialogic INC directories as well as Lib in its option. Else you will see “Compile Error”.

3. Every Channel in a D4 PCI card is denoted by a string dxxxBnCk where n denotes the board ID and k denotes the channel number 1 through 4. For a D4 PCI Card, the list of channels are
a. dxxxB1C1
b. dxxxB1C2
c. dxxxB1C3
d. dxxxB1C4
There are other ways to find these strings to denote channels. There are also Dialogic APIs to find these. You can also check these strings attached Sample Voice application that comes with Dialogic System Release.

4. Now here is the C++ code.

To open the first channel
int hChanell;
hChannel = dx_open(dxxxB1C1,0)

If ( hChannel != -1 )
{
AfxBeginThread(threadFunc,chidchar);
}

// Initialise same way for other three channels, ie. dxxxB1C2, dxxxB1C3 and dxxxB1C4

Now threadFunc is a Windows Thread that performs the Call Flow.

DV_TPT def_rp_tpt[] = {
{IO_CONT,DX_LCOFF, 4, TF_LCOFF, 0, 0, NULL},
//{IO_CONT,DX_TONE,POTS_DISCTONE,TF_TONE,DX_TONEON,0,NULL},
{IO_EOT,DX_MAXDTMF,1,TF_MAXDTMF,0,0,NULL}

UINT threadFunc(LPVOID pParam)
{
char *ch = (char *)pParam;
int channel = atoi(ch);
unsigned char callid[81];
memset((void *)callid, 0, 81*sizeof(char));
char UniqueId[300];

char mobid[20];
char temp[100];
char channelname[20];
char callername[50];
time_t starttime,endtime;

char tm[100],tm1[100];
int retval=1;

FILE *file;
char szBuffer[200];

int NumRingAnswer;

// Ensure the phone is kept ON HOOK
if (dx_sethook(channel,DX_ONHOOK,EV_SYNC) == -1)
{
//WinPrintf(“dx_sethook failed”);
//return(0);
}

NumRingAnswer = 3; // Answer after 3 rings
while(true)
{

if (dx_wtring(channel,NumRingAnswer,DX_OFFHOOK,-1) == -1)
{
print(“ERROR: dx_wtring failed. errno = %d\n,Please make sure your drivers are properly installed”, errno);
exit(0);
}

// PostMessage(hWnd,WM_OFFHOOK,channel,0);

if(dx_gtcallid(channel,callid)==-1)
{
//Get the caller id
strcpy((char *)callid, “0000000000″);
strcpy((char *)mobid, “0000000000″);

if (ATDV_LASTERR(channel) == EDX_CLIDBLK)
{
//printf(“Caller ID information blocked \n”);
}
else if (ATDV_LASTERR(channel) == EDX_CLIDOOA)
{
//printf(“Caller out of area \n”);
}
else {
/* Or print the pre-formatted error message */
//printf(“Error: %s \n”, ATDV_ERRMSGP(channel));
}

}
switch (ATDX_TERMMSK(channel))
{
case TM_LCOFF:
//delete cml;
return false;
case TM_DIGIT:
case TM_EOD:
dx_clrdigbuf(channel);
}

strcpy(callername,(char *)callid);

sprintf(channelname,”%d”,channel);

sprintf(UniqueId,”%d”,::GetTickCount()); // Just to generate an unique ID

starttime=time(NULL);

dx_clrdigbuf(channel);

strcpy(szBuffer,”Welcome.wav”); // welcome.wav is the file played, it has to be 11025 KHz, PCM, 8Bit, Mono.

if(dx_playwav(channel,szBuffer,def_rp_tpt,EV_SYNC)<0)
{
printf(ATDV_ERRMSGP(channel));
}

tch (ATDX_TERMMSK(channel))
{
case TM_LCOFF:
return false;
case TM_DIGIT:
case TM_EOD:
break;
}

endtime=time(NULL);
sprintf(tm,”%s”,asctime(localtime(&starttime)));
sprintf(tm1,”%s”,asctime(localtime(&endtime)));
// Just to record time of call

if (dx_sethook(channel,DX_ONHOOK,EV_SYNC) == -1)
{
//WinPrintf(“dx_sethook failed”);
//return(0);
}

PostMessage(hWnd,WM_ONHOOK,channel,0);

// if it discovers a file civrs.dat in its directory, then it closes itself.
strcpy(szDirectory[channel],defLanguage);

if ( (file=fopen(“civrs.dat”,”r”))==NULL )
{
}
else
{
exit(0);
}
}
return 1;

}

6. The variable names etc. may produce some errors! This code may not work as it is if pasted! But this code is intended for use a reference and starting point

I will update the code with fully working code soon.

Popularity: 14% [?]


{ 2 comments… read them below or add one }

1 Jeeva February 5, 2010 at 10:30 am

hi,
I am trying to do an IVR. you guys adviced to use dialogic card.Since, it is very costlier to me I am trying to do IVR with the help of voice modem. can any one help me out plz!!!!!!!!!!!???

Regards,
Jeeva

2 Uttam Pegu February 5, 2010 at 10:38 am

Hi Jeeva,
You may try Donjin o Synway board. These boards are much cheaper ( 1/3rd of Dialogic cost). Donjin uses same library as Dialogic.

For using Voice modem, you may try with TAPI which is inbuilt library in any Microsoft Tools like VB, VC++ etc.

Hope this helps.

Leave a Comment

Previous post:

Next post: