Skip to main content

IVR JS Example (Simple setup)

IVR JS Example - Simple setup

This example demonstrates a basic IVR script using Vodia's Javascript IVR capabilities, incorporating TTS, DTMF collection, and communication with an external application.

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.
  • SMS the cutomer Reference number
  • Play a dynamically generated message from the external application's response and potentially route the call to a destination number, also provided by the external application.
'use strict';
call.say('Welcome to company ABC. Please enter your 4 digit customer number.');
var digits = '';
call.dtmf(ondtmf);

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

function send(digits) {
var body = JSON.stringify({
customerCode: digits
});

var args = {
method: 'POST',
url: 'https://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);

// Play message if provided
if (res.message) call.say(res.message);

// Send SMS if provided
if (res.sms_msg) {
console.log('Sending SMS: ' + res.sms_msg);
call.sms(res.sms_msg);
}

// Transfer call if requested
if (res.transfer) {
setTimeout(function(){
call.transfer(res.destination);
}, 5000);
}
}
}

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

{
"destination": "901",
"message": "Thank you for providing your customer number. I will now transfer you to Customer Service.",
"sms_msg": "Thank you for calling Company ABC. Your reference number is 12345. We are transferring you to Customer Service.",
"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