Skip to main content

Javascript Example - DTMF Callback

0. About

Example of calling a DTMF callback function while playing a file.

1. Example JS Code

A, B, C, D tones

Make sure to expect (and handle) the DTMF tones A , B , C , and D; these are very rare nowadays but they are still possible to send (e.g., someone has an old device with the extra ABCD column). See DTMF for more.

DTMF callback example

/* example of calling a dtmf callback function while playing a file. */
function onPlayFile(s, type, obj, arg)
{
try {
if (type == "dtmf") {
console_log("info", "DTMF digit: " + s.name + " [" + obj.digit + "] len [" + obj.duration + "]\n\n");

// below are some controls available to javascript input callback
// not all are 100% tested in this example, please check mod_spidermonkey if they do not work for you
// if you do not require playback control, the entire following block can be removed
if (obj.digit == "3") {
return "seek:+5000"; // seek forwards (testing needed)
}
else if (obj.digit == "1") {
return "seek:-5000"; // seek backwards (testing needed)
}
else if (obj.digit == "2") {
return "volume:+1"; // volume up (testing needed)
}
else if (obj.digit == "3") {
return "volume:-1"; // volume down (testing needed)
}
else if (obj.digit == "5") {
return "pause"; // pauses and unpauses
}
else if (obj.digit == "6") {
return "restart"; // seek to the beginning of the file
}
else if (obj.digit == "7") {
return "speed:+1"; // increase playback speed (testing needed)
}
else if (obj.digit == "8") {
return "speed:-1"; // decrease playback speed (testing needed)
}
else if (obj.digit == "9") {
return true; // return without interrupting playback of streamFile
}
else if (obj.digit == "*") {
return false; // stop playback and break from streamFile
}

}
} catch (e) {
console_log("err", e + "\n");
}
return true;
}

session.answer();

while(session.ready()) {
session.streamFile(argv[0], onPlayFile);
}