Session sayPhrase

From FreeSWITCH Wiki

Jump to: navigation, search

Speaks a prompt macro using the FreeSwitch say macro API.

Macros should be placed in conf/lang_XX.xml.

Synopsis

session.sayPhrase(macro_name, macro_data, language, dtmf_callback, dtmf_callback_args);
  • macro_name - The name of the say macro to speak
  • macro_data - Data to pass to the say macro
  • language - language to speak macro in (ie. "en" or "fr")
  • dtmf_callback - dtmf callback function name.
  • dtmf_callback_args - dtmf callback args to pass to the callback function when it is called.

Callback return values and meanings:

  • "true" - causes prompts to continue speaking
  • any other value interrupts the prompt and returns the string from the sayPhrase function

Example

  var dtmf_digits = "";
  
  function on_dtmf(session, type, obj, arg)
  {
     if (type == "dtmf") {
       console_log("on_dtmf got digit: " + obj.digit + "\n");
       dtmf_digits += obj.digit;
     }
     return(true);
  }
  
  /* Speaks a menu and waits for a single digit press. If the user does not enter a selection */
  /* then the menu is repeated up to 3 times. */
  function sayivrmenu(ivrsession, menuname, validdigits, timeout) {
      var repeat = 0;
  
      console_log("sayivrmenu: menu=[" + menuname + "] validdigits=[" + validdigits + "]\n");
  
      session.flushDigits();
      dtmf_digits = "";
  
      while (ivrsession.ready() && dtmf_digits == "" && repeat < 3) {
          /* play phrase - if digit keyed while playing callback will catch them*/
          ivrsession.sayPhrase(menuname, validdigits, "en", on_dtmf, "");
  
          /* if caller still here and has not entered any selection yet - wait for a selection*/
          if (ivrsession.ready() && dtmf_digits == "") {
              dtmf_digits = ivrsession.getDigits(1, "", timeout);
              /* if still no selection repeat menu */
              if (dtmf_digits == "") {
                  repeat++;
              }
          }
      }
      return(dtmf_digits);
  }
  
  
  var menuselection = "";
  
  /** Let's answer our call **/
  session.answer();
  
  /** Play our Main Menu prompt (options 0, 1, 2, 3 and #) **/
  menuselection = sayivrmenu(session, "mainmenu", "0123#", 6000);
  
  if (session.ready()) {
      session.execute("phrase", "saydigits," + menuselection);
  }

See Also

Personal tools
Community