Examples dbivrmenu js

From FreeSWITCH Wiki
Jump to: navigation, search
/** 
dbivrmenu.js - Simple IVR Menu using a database.
JavaScript Application for FreeSwitch::mod_spidermonkey.
by Paul Davis (pist @ #freeswitch)

This application is only meant to be an example of the power and ease of using FreeSwitch.
This file includes work ripped off from other JavaScript apps created by anthm @ #freeswitch.
I hope this helps you in starting to use FreeSwitch & JS.

Note: This application requires a DB.  It uses CoreDB module to access a sqlite database.
		Look for dbivrmenu_dbcreate.js, in this wiki, to have FS create the database for you.
		This requires the default dialplan to work properly.
		Sofia-SIP should be working properly before trying to use this app.
		You will have to create the sound files or REM out those statements.
		This application simply transfers the call to another extension.
		This app works on FS rev 3488.
		
Install:  copy this file to your <freeswitch dir>/scripts/
		Add the following section to your freeswitch.xml around the extension 1000 section.
		--------
		<extension name="1200">
        <condition field="destination_number" expression="^1200$">
          <action application="javascript" data="dbivrmenu.js"/>
        </condition>
      </extension> 
		--------
		Dial 1200 from your sip phone and voila!  Ofcourse you can match for inbound DID/DNIS, and run this app.
		
Disclaimer: Use at your own risk.  No implied warranties or help if/when stuff blows up.
**/

use("CoreDB");

var db = new CoreDB("dbivrmenudemo");
var sql;
var dtmf = new Object();
var replay = 1;

function mycb (session, type, data, arg) {

	if (type == "dtmf") {
		arg.digits += data.digit;
		if (arg.digits.length >= 1) {
			return false;
		}
	}
}

function get_route(digits)
{
	sql = "select * from menu_options where option = '" + digits +"';";

	var line = "====================================\n";

	console_log("notice", "Menu Option Entered: " + digits +" \n");
	console_log("notice", line);
	
	db.prepare(sql);
	while(db.next()) {
   	hash = db.fetch();
		
		if (hash["option"] == digits) {
			console_log("notice", "Found Menu Option=[" + hash["option"] + "] Route=[" + hash["route"] + "]\n");
			return hash["route"];
		} else { 
			console_log("notice", "Menu Option NOT Found!\n");
			return false; }
	}

}

/** Let's answer our call **/
session.answer();

/** Play our Main Menu prompt sound file.  You could use TTS here as well.  **/
console_log("notice", "Playing dbivrmenu Main Menu sound file . . .\n");
session.streamFile("/var/sounds/dbivrmenu_main.wav", "");	

while(session.ready()) {
	dtmf.digits = "";
    console_log("notice", "collecting dtmf digits . . .\n");
    
   if (dtmf.digits.length < 1) {
		session.collectInput(mycb, dtmf, 5000);
	} 
	
	/** when we timeout from above, we check replay value and play appropriate prompts **/
	if ((!dtmf.digits) && (replay < 4)) {
		console_log("notice", "Replay # " + replay + "\n");
		session.streamFile("/var/sounds/invalidoption.wav", "");
		session.streamFile("/var/sounds/dbivrmenu_main.wav", "");
		replay++;  // increment our replay value; we don't want to replay our prompts too many times!
		} else if (replay >= 4) { 
			/** Caller does not understand the prompts or your DB data is wrong; either way we've had enough, let's push'em off the line **/
			session.streamFile("/var/sounds/havingtrouble.wav", "");
			session.hangup();
			}
	
	
	if(dtmf.digits) {
    	console_log("notice", "Got Some Digits . . .\n");
		
		route = get_route(dtmf.digits);
		if (!route) {
   	 	console_log("notice", "Invalid Option . . .\n");
			session.streamFile("/var/sounds/invalidpin.wav", "");
			session.streamFile("/var/sounds/dbivrmenu_main.wav", "");
		} else {
	   	console_log("notice", "Found Valid Option . . .\n");
			session.streamFile("/var/sounds/plswait.wav", "");
			
			/** initiate the transfer application and send to the extension in route; again, you could extend this **/
			session.execute("transfer", route);

		}
	}
	
}

See Also