IVR JS Example (Smart Address Book Transfer)
IVR JS Example - Smart Address Book Transfer
This example script demonstrates Vodia's Javascript IVR functionality for automatic call routing based on address book lookups. The script extracts caller information, searches the internal address book, and transfers calls to designated numbers stored in the fax field.
Scenario:
- Extract caller ID from SIP URI format (
from
andto
headers). - Search the internal address book table for matching caller numbers.
- Attempt lookup by
number
,mobile
, anddisplay_number
fields. - Transfer the call to the number stored in the
fax
field if a match is found. - Provide appropriate voice prompts and fallback handling to 999 for unrecognized callers(change 999 to your fallback account number)
- Log detailed information for debugging and call tracking.
'use strict';
// Get the from number of the incoming call
var fromRaw = tables['cobjs'].get(call.callid, 'from');
var toRaw = tables['cobjs'].get(call.callid, 'to');
console.log('Incoming call from: ' + fromRaw);
console.log('Called number: ' + toRaw);
// Extract phone number from SIP URI format: "Display Name" <sip:+phonenumber@domain>
function extractPhoneNumber(sipUri) {
if (!sipUri) return '';
// Look for the pattern sip:+number@ or sip:number@
var match = sipUri.match(/sip:(\+?\d+)@/);
if (match && match[1]) {
return match[1];
}
// If no match, return the original (fallback)
return sipUri;
}
var from = extractPhoneNumber(fromRaw);
var to = extractPhoneNumber(toRaw);
console.log('Extracted from number: ' + from);
console.log('Extracted to number: ' + to);
// Search the adrbook table directly
console.log('Searching adrbook table for number: ' + from);
// Search by number field first
var numberResults = tables['adrbook'].search('number', from);
console.log('Search by number field results: ' + JSON.stringify(numberResults));
if (numberResults && numberResults.length > 0) {
var entryId = numberResults[0];
console.log('Found entry with ID: ' + entryId);
// Get full entry details for debugging
var fullEntry = tables['adrbook'].get(entryId, '*');
console.log('Full entry details: ' + JSON.stringify(fullEntry, null, 2));
// Get the fax field
var fax = tables['adrbook'].get(entryId, 'fax');
console.log('Fax field value: ' + fax);
if (fax && fax.trim() !== '') {
console.log('Found fax number: ' + fax);
call.say('Transferring your call to your designated number.');
// Transfer the call to the fax number
setTimeout(function(){
call.transfer(fax);
}, 2000);
} else {
console.log('No fax number found for this contact');
call.say('No transfer number found for your account. Please hold while we connect you to an operator.');
}
} else {
// Try searching by mobile field
console.log('Not found by number, trying mobile field...');
var mobileResults = tables['adrbook'].search('mobile', from);
console.log('Search by mobile field results: ' + JSON.stringify(mobileResults));
if (mobileResults && mobileResults.length > 0) {
var mobileEntryId = mobileResults[0];
console.log('Found entry by mobile with ID: ' + mobileEntryId);
// Get full entry details
var fullMobileEntry = tables['adrbook'].get(mobileEntryId, '*');
console.log('Full mobile entry details: ' + JSON.stringify(fullMobileEntry, null, 2));
var fax = tables['adrbook'].get(mobileEntryId, 'fax');
console.log('Fax field value from mobile entry: ' + fax);
if (fax && fax.trim() !== '') {
console.log('Found fax number: ' + fax);
call.say('Transferring your call to your designated number.');
setTimeout(function(){
call.transfer(fax);
}, 2000);
} else {
console.log('No fax number found for this mobile contact');
call.say('No transfer number found for your account. Please hold while we connect you to an operator.');
}
} else {
// Try searching by display_number field
console.log('Not found by mobile, trying display_number field...');
var displayResults = tables['adrbook'].search('display_number', from);
console.log('Search by display_number field results: ' + JSON.stringify(displayResults));
if (displayResults && displayResults.length > 0) {
var displayEntryId = displayResults[0];
console.log('Found entry by display_number with ID: ' + displayEntryId);
var fullDisplayEntry = tables['adrbook'].get(displayEntryId, '*');
console.log('Full display_number entry details: ' + JSON.stringify(fullDisplayEntry, null, 2));
var fax = tables['adrbook'].get(displayEntryId, 'fax');
console.log('Fax field value from display_number entry: ' + fax);
if (fax && fax.trim() !== '') {
call.say('Transferring your call to your designated number.');
setTimeout(function(){
call.transfer(fax);
}, 2000);
} else {
console.log('No fax number found for this display_number contact');
call.say('No transfer number found for your account. Please hold while we connect you to an operator.');
}
} else {
console.log('Caller not found in address book by number, mobile, or display_number fields');
console.log('Searched for: ' + from);
// Let's also check how many total entries are in the address book
var totalCount = tables['adrbook'].count();
console.log('Total entries in address book: ' + totalCount);
call.say('Your number is not recognized in our system. Please hold while we connect you to an operator.');
// Transfer to 999 if caller not found
setTimeout(function(){
call.transfer('999');
}, 8000);
}
}
}
Console Log Output Example
The script provides detailed logging for troubleshooting:
Incoming call from: "Christo Da Silva" <sip:+61433997299@sbc.vodia-teams.com>
Called number: "02 7201 0747" <sip:+61272010747@sbc.vodia-teams.com>
Extracted from number: +61433997299
Extracted to number: +61272010747
Searching adrbook table for number: +61433997299
Search by number field results: [243]
Found entry with ID: 243
Fax field value: +6122223333
Found fax number: +6122223333
Search Priority
The script searches the address book in the following order:
- number field - Primary landline number
- mobile field - Mobile phone number
- display_number field - Number as originally entered
Ensure the fax
field contains the destination number for transfer. The script uses a 2-second delay before transfer to allow the TTS message to complete.
The script extracts phone numbers from SIP URI format automatically. Ensure the from header number matches the adressbook entry.
For more information on Vodia's JavaScript capabilities, refer to: Vodia Backend JavaScript Documentation