Javascript Example - HelloWorld
About
This quick start aims to help you with creating your first JavaScript application.
Paths
If you place the script in the "[FS root dir]/scripts" you may access them by their basename without any path information; if you place them elsewhere you must include the full path to the script eg:
If located in [FS root dir]/scripts
<action application="javascript" data="HelloWorld.js" />
Full path example:
<action application="javascript" data="/var/scripts/HelloWorld.js" />
First application
Let's create a new application called HelloWorld.js and place it in the correct directory.
Your first JS app
var languageCode = "en";
var soundDir = "sound/";
function playFile(fileName, callBack, callBackArgs)
{
session.streamFile(soundDir + languageCode + "/" + fileName, callBack, callBackArgs);
}
session.answer();
playFile("HelloWorld.wav");
exit();
Explanation
That wasn't so hard, was it? The first line specifies which Human language we are using. We can switch language anytime during the session thanks to this variable.
The second variable specifies the root directory for the sound files.
We have created our own method called playFile which is used to play the correct language prompt. It also allows us not to care about where the sound files are located, which gives us the advantage of D.R.Y. (don't repeat yourself).
Session.answer() is the most important function in this script, since it answers the telephone call.
Finally we have exit (not really necessary in this example, but I feel obliged to discuss it). exit is a magic method which will hangup on the user AND exit the script. There is another function called session.hangup() which will allow you to hangup on the user but not quit the JavaScript. This method is excellent if you need to write stuff to a database or something like that.
Sound file
We have a complete JavaScript app, but we will also need a sound file. Record or copy another .wav name HelloWorld.wav and place it in the "sounds/en/us/callie/sounds/en/" folder.
Dialplan
Last thing to do is to add the JavaScript to our dialplan. Open "conf/default.xml" and add the following:
<extension name="9910">
<condition field="destination_number" expression="^9910$">
<action application="javascript" data="HelloWorld.js" />
</condition>
</extension>
Test
Now dial 9910 and enjoy your first JavaScript application.
Comments:
I had to modify: var soundDir = "ivr/8000";in order to play a wav file from the ivr group in order to get one of the IVR announcements to play. Also, there is no HelloWorld.wav that I can find. I'm certain it was meant as an example.Thanks for this example, John. Posted by tom@tomlynn.com at Oct 27, 2017 10:17 |
---|