JavaScript Examples promptfordigts js

From FreeSWITCH Wiki
Jump to: navigation, search
   /** 
    * promptfordigits.js - sample application to prompt the user for n digits.
    * 
    * JavaScript Application for FreeSwitch::mod_spidermonkey.
    * by Michael Murdock 
    * 
    * This file includes work ripped off from other JavaScript apps created by pist and anthm @ #freeswitch.
    * 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="promptfordigts.js"/>
    *    </condition>
    * </extension> 
    * 
    * Dial 1200 from your sip phone and voila!  Of course you can match for inbound DID/DNIS, and run this app.
    * 
    * This application uses the phrase macro "DialByNumberMenu" and "saydigits" as below;
    * 
    * <macro name="saydigits">
    *    <input pattern="(.*)">
    *       <match>
    *          <action function="execute" data="sleep(500)"/>
    *          <action function="speak-text" data="You entered $1"/>
    *       </match>
    *    </input>
    * </macro>
    * 
    * <macro name="DialByNumberMenu">
    *    <input pattern="(.*)">
    *       <match>
    *          <action function="execute" data="sleep(500)"/>
    *          <action function="speak-text" data="Enter the $1 digit extension to dial."/>
    *       </match>
    *    </input>
    * </macro>
    * 
    *      
    * Disclaimer: Use at your own risk.  No implied warranties or help if/when stuff blows up.
    * 
    * 
   **/
   
   var promptfordigits_dtmf_digits = "";
   
   function promptfordigits_dtmf_callback(type, digits, arg)
   {
       console_log("digit: " + digits + "\n");
       promptfordigits_dtmf_digits += digits;
       /* returning true does not interrupt the digit collection*/
       return(true);
   }
   
   /* Speaks a menu and waits for a specified number of digits. If the user does not enter a selection */
   /* then the menu is repeated up to 3 times. */
   function promptfordigits(ivrsession, promptname, numdigits, timeout) {
       var repeat = 0;
   
       console_log("saymenu: menu=[" + promptname + "] numdigits=[" + numdigits + "]\n");
   
       session.flushDigits();
       promptfordigits_dtmf_digits = "";
   
       while (ivrsession.ready() && promptfordigits_dtmf_digits.length < numdigits && repeat < 3) {
           /* play phrase - if digit keyed while playing callback will catch them. 
           If less than numdigits collected we get the rest after the prompt.*/
           ivrsession.sayPhrase(promptname, numdigits, "en", promptfordigits_dtmf_callback, "");
   
           console_log("Prompt done=[" + promptname + "] Collected " + promptfordigits_dtmf_digits.length + " digits [" + promptfordigits_dtmf_digits + "]\n");
   
           /* if caller still here and has not entered any selection yet (or less than numdigits entered) - wait for the rest of the digits*/
           if (ivrsession.ready() && promptfordigits_dtmf_digits.length < numdigits ) {
               promptfordigits_dtmf_digits += ivrsession.getDigits(numdigits - promptfordigits_dtmf_digits.length, "", timeout);
               /* if still no selection or insufficient digits repeat menu */
               if (promptfordigits_dtmf_digits.length < numdigits) {
                   promptfordigits_dtmf_digits = "";
                   repeat++;
               }
           }
       }
       return(promptfordigits_dtmf_digits);
   }
   
   var menuselection = "";
   
   /** Let's answer our call **/
   session.answer();
   
   /** Play our Main Menu prompt (options 0, 1, 2, 3 and #) **/
   menuselection = promptfordigits(session, "DialByNumberMenu", 3, 6000);
   
   if (session.ready()) {
       session.execute("phrase", "saydigits," + menuselection);
   }

See Also

Javascript