/////////////////////////////////////////////////////////////////////////// // ivr.js // // Plays a prompt and gets some input. Forwards on to the proper // destination or plays the error prompt if the selection is invalid. // // Note: This implies the existance of a larger surrounding system that // among other things sets numberId. This code is released for example // purposes only and is not intended to be a complete working system on // its own. // // 2008-05-28 Anders Brownworth - anders-ivr-js at evantide.com // this code: http://www.anders.com/1offs/ivr.js.txt // db schema: http://www.anders.com/1offs/ivr.sql.txt // /////////////////////////////////////////////////////////////////////////// var ivrPromptDir = "sounds/ivr"; /////////////////////////////////////////////////////////////////////////// // DB setup /////////////////////////////////////////////////////////////////////////// use( "ODBC" ); var db = new ODBC( "freeswitch-db", "freeswitch-user", "freeswitch-password" ); /////////////////////////////////////////////////////////////////////////// // IVR Helper Functions /////////////////////////////////////////////////////////////////////////// function ivrOnInput( session, type, data, arg ) { if ( type == "dtmf" ) { console_log( "info", "Got " + data.digit + "\n" ); // should probably return true for all but VALID options instead... option = data.digit; return( false ); } return( true ); } function ivrGetDigit( soundFile, hangUpAfter ) { var loopCount = 0; while ( session.ready( ) && loopCount < hangUpAfter && option == "" ) { session.streamFile( ivrPromptDir + "/" + soundFile, ivrOnInput ); if ( option == "" && session.ready( ) ) { session.collectInput( ivrOnInput, '', 5000 ); } } return( option ); } function ivrSetVars( destinationType, destinationData ) { if ( destinationType == "number" ) { numberId = getNumberId( destinationData ); if ( numberId > -1 ) { tiedTo = "user"; } to = destinationData; // this is really horrible to be setting this all the way in here but JavaScript has no variable scoping! exit = true; } else if ( destinationType == "queueId" ) { queueId = destinationData; tiedTo = "queue"; exit = true; } else if ( destinationType == "huntGroupId" ) { huntGroupId = destinationData; tiedTo = "huntgroup"; exit = true; } else if ( destinationType == "ivrNodeId" ) { nodeId = destinationData; console_log( "info", "Continuing with ivr node id " + nodeId + "...\n" ); } else { console_log( "err", "I don't understand " + destinationType + " as a destination type!\n" ); exit = true; } } /////////////////////////////////////////////////////////////////////////// // Main Application /////////////////////////////////////////////////////////////////////////// console_log( "info", "ivr.js Started...\n" ); if ( session.ready( ) ) { session.answer( ); try { db.connect( ); db.query( "select i.ivrnodeid, i.welcomefile " + "from ivrs i " + " left join numbers n on i.numberid = n.numberid " + "where n.numberid = " + numberId ); if ( db.nextRow( ) ) { row = db.getData( ); nodeId = row[ "ivrnodeid" ]; welcomeFile = row[ "welcomefile" ]; session.answer( ); if ( welcomeFile != "" ) { session.streamFile( ivrPromptDir + "/" + welcomeFile ); } exit = false; while ( session.ready( ) && ! exit ) { db.query( "select promptfile, invalidselectionfile, hangupafter " + "from ivrnodes " + "where ivrnodeid = " + nodeId ); if ( db.nextRow( ) ) { row = db.getData( ); ivrPromptFile = row[ "promptfile" ]; invalidSelectionFile = row[ "invalidselectionfile" ]; hangUpAfter = row[ "hangupafter" ]; console_log( "info", "ivrPromptFile=" + ivrPromptFile + " invalidSelectionFile=" + invalidSelectionFile + " hangUpAfter=" + hangUpAfter + "\n" ); ivrGetDigit( ivrPromptFile, hangUpAfter ); console_log( "info", "user selected option [" + option + "]\n" ); if ( option != "" ) { db.query( "select destinationtype, destinationdata " + "from ivroptions " + "where ivrnodeid = " + nodeId + " and option='" + option + "'" ); if ( db.nextRow( ) ) { console_log( "info", "option found!\n" ); row = db.getData( ); destinationType = row[ "destinationtype" ]; destinationData = row[ "destinationdata" ]; console_log( "info", "destination type: " + destinationType + " destination data: " + destinationData + "\n" ); ivrSetVars( destinationType, destinationData ); } else { console_log( "info", "option NOT found!\n" ); session.streamFile( ivrPromptDir + "/" + invalidSelectionFile ); } } // end if ( option != "" ) option = ""; } else { console_log( "err", "Whoops! No ivr with the id of " + ivrId + " in the database!\n" ); exit = true; } } // end of while ( session.ready( ) && ! exit ) } // end if ( db.nextRow( ) ) else { console_log( "err", "Whoops! No ivr for numberId " + numberId + "\n" ); } } catch( e ) { console_log( "err", "Whoops! " + e + "\n" ); } } // end if ( session.ready( ) ); console_log( "info", "ivr.js Finished.\n" );