Simple IVR in JavaScript
About
This is more or less based on the other examples but was slightly modified to "dial a users extension at any time".
Click here to expand Table of Contents
- 1 Example
Example
simpleivr.js
function on_dtmf(session, type, digits, arg)
{
if ( saved_digits = session.getVariable("digits") ) {
session.setVariable("digits", saved_digits + digits.digit);
} else {
session.setVariable("digits",digits.digit);
dtmf = session.getDigits(10, "#", 4000);
session.setVariable("digits", session.getVariable("digits") + dtmf);
}
console_log("in callback got digits: " + session.getVariable("digits") + "\n");
return(false);
}
function startivr(session) {
var dtmf_digits = "";
var repeat = 0;
while (session.ready() && dtmf_digits == "" && repeat < 3) {
session.streamFile("/usr/local/freeswitch/sounds/ivr/default/mainivr.wav", on_dtmf);
dtmf_digits = session.getVariable("digits");
if (session.ready() && dtmf_digits == "") {
dtmf_digits = session.getVariable("digits");
if (dtmf_digits == "") {
repeat++;
}
}
}
return(dtmf_digits);
}
var dtmf = "";
session.answer();
session.setVariable("digits","");
dtmf = startivr(session);
console_log("got total digits: " + dtmf + "\n");
if ( dtmf == "" ) {
session.hangup();
} else if ( dtmf.length == 1 ) {
session.execute("execute_extension", dtmf+" XML ivr")
} else {
session.execute("execute_extension", dtmf+" XML proxy");
}