Zum Hauptinhalt springen

IVR JS Example (Caller ID)

This example script demonstrates Vodia's Javascript IVR functionality, encompassing TTS output, DTMF input processing, Caller ID handling with the collected Caller ID and DTMF input being transmitted to an external application for further processing.

Scenario:

  • Play a welcome message using Vodia's TTS.
  • Collect a 4-digit input from the user via DTMF.
  • Send the collected digits to an external application in JSON format.
  • Send Caller ID and Called number (from and to headers) to an external application in JSON format.
  • The external application will attempt customer detail verification and subsequently transmit a dynamically generated message along with a destination number. This destination number may be the IVR itself in cases of failed verification or incorrect PIN entry.
'use strict';

call.say('Welcome to company ABC. Please enter your 4 digit customer PIN, we will verify against your phone number.');

var from = tables['cobjs'].get(call.callid, 'from');

var to = tables['cobjs'].get(call.callid, 'to');

console.log(to);

var digits = '';

call.dtmf(ondtmf);

function ondtmf(digit) {
digits += digit;
if (digits.length == 4) {
send(digits, from, to);
digits = '';
from = '';
to = '';
}
}

function send(digits, from, to) {
var body = JSON.stringify({
customerCode: digits,
customerNumber: from,
CalledIVR: to
});


var args = {
method: 'POST',
url: 'http://external_server/webhook',
header: [ { name: 'Content-Type', value: 'application/json' } ],
callback: callback
};

if (body) {
args.body = body;
}

system.http(args);


function callback(code, response, headers) {
console.log('IVR response:' + response.toString())
console.log('IVR response:' + code.toString())
var res = JSON.parse(response);
if (res.message) call.say(res.message)
if (res.transfer) setTimeout(function(){
call.transfer(res.destination)
}, 9000)
}

}

The external application will receive the following data. (Example data follows.)

{"customerCode": "1234", "customerNumber": "04xxxxxxxx <sip:04xxxxxxxx@vodia.pbx.com>", "CalledIVR": "Call Queue Sales <sip:61xxxxxxx@vodia.pbx.com>"}

The external application, in response to the JSON data sent by the Vodia PBX, should return a JSON object with specific fields. (Example data follows.)

{ "destination": "785","message": "We have sucessfully identified you, transfering now", "transfer": "true" }
tip

Use 'Connection': 'keep-alive' header in your response.

For more information on Vodia's JavaScript capabilities, refer to: Vodia Backend JavaScript Documentation