Skip to main content

IVR JS Example (EMAIL)

This example demonstrates a IVR script using Vodia's Javascript IVR capabilities, incorporating TTS, DTMF collection, communication with an external application and automatically sends emails to customers based on their inputs.

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.
  • If the JSON response includes an email address, send the details to that address and transfer to an Auto attendant '10'
  • If no email address is found, play the provided message and transfer the call to the destination specified in the JSON response.
'use strict';

call.say('Welcome to company ABC. Please enter your 4 digit customer number and we will email your details to the nominated email address.');

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://extenal_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 there's an email address in the response, send details
if (res.email) {
var emailArgs = {
to: system.parseEmailAdr(res.email),
from: { name: "Company ABC Support", adr: "support@companyabc.com" },
subject: "Your Details requested",
body: res.message
};

system.email(emailArgs);
call.say("We have emailed your details requested.");
}

// If there's a transfer request, process it after a delay
if (res.transfer) {
call.say(res.message);
setTimeout(function() {
call.transfer(res.destination);
}, 10000);
}
if (res.transfer == null) {
setTimeout(function() {
call.transfer('10');
}, 5000);
}

}
}

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

{ "destination": "785","message": "Thanks Mr. Jones we could not find your email transfering you now", "transfer": "true" }

OR

{ "email": "pbx@vodia.com","message": "Thanks Mr. Jones , your access code is 1234" }
tip

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

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