aadir.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * File: aadir.js
  3. * Purpose: Auto Attendant directory.
  4. * Machine: OS:
  5. * Author: John Wehle Date: November 6, 2008
  6. *
  7. * Copyright (c) 2008 Feith Systems and Software, Inc.
  8. * All Rights Reserved
  9. */
  10. var digitTimeOut = 3000;
  11. var interDigitTimeOut = 1000;
  12. var absoluteTimeOut = 10000;
  13. var base_dir = session.getVariable ("base_dir");
  14. var domain = session.getVariable ("domain");
  15. var voicemail_path = base_dir + "/storage/voicemail/default/" + domain + "/";
  16. var file_exts = [ ".wav", ".mp3" ];
  17. var extRE = /^1[0-9][0-9][0-9]$/g;
  18. var operator = "operator";
  19. var directory;
  20. var directory_camelcase;
  21. var translations = [ "0",
  22. "QZ", "ABC", "DEF",
  23. "GHI", "JKL", "MNO",
  24. "PQRS", "TUV", "WXYZ" ];
  25. var extension = "";
  26. var dtmf_digits = "";
  27. function load_directory ()
  28. {
  29. var i;
  30. var name;
  31. var number;
  32. var dir = apiExecute ("xml_locate", "directory domain name " + domain);
  33. var re = /\s+$/g;
  34. var length = dir.search (re);
  35. if (length == -1)
  36. length = dir.length;
  37. dir = dir.substring (0, length);
  38. var xdir = new XML (dir);
  39. directory = new Array ();
  40. i = 0;
  41. re = /[^A-Z0-9\s]/gi;
  42. for each (var variables in xdir.groups.group.users.user.variables) {
  43. name = "";
  44. number = "";
  45. for each (variable in variables.variable) {
  46. if (variable.@name.toString() == "effective_caller_id_name")
  47. name = variable.@value.toString();
  48. if (variable.@name.toString() == "effective_caller_id_number")
  49. number = variable.@value.toString();
  50. }
  51. if (name.length == 0 || number.length == 0 || number.search (extRE) == -1)
  52. continue;
  53. directory[i] = new Array (2);
  54. directory[i][0] = name.replace (re, "");
  55. directory[i][1] = number;
  56. i++;
  57. }
  58. }
  59. function build_camelcase_directory ()
  60. {
  61. var i;
  62. var fname;
  63. var lname;
  64. var fre = /^[A-Z0-9]+/gi;
  65. var lre = /[A-Z0-9]+$/gi;
  66. directory_camelcase = new Array (directory.length);
  67. for (i = 0; i < directory.length; i++) {
  68. directory_camelcase[i] = new Array (2);
  69. directory_camelcase[i][0] = "";
  70. directory_camelcase[i][1] = 0;
  71. fname = directory[i][0].match (fre);
  72. lname = directory[i][0].match (lre);
  73. if (fname.length != 1 || lname.length != 1) {
  74. console_log ("err", "Can't parse " + directory[i][0] + " for directory\n");
  75. continue;
  76. }
  77. directory_camelcase[i][0] = lname[0] + fname[0];
  78. directory_camelcase[i][1] = directory[i][1];
  79. }
  80. }
  81. function directory_lookup (digits)
  82. {
  83. var i;
  84. var match = "";
  85. var pattern = "^";
  86. var re;
  87. if (digits.length && digits[0] == 0)
  88. return 0;
  89. for (i = 0; i < digits.length; i++) {
  90. if (isNaN (parseInt (digits[i], 10)))
  91. return -1;
  92. pattern += "[" + translations[parseInt (digits[i], 10)] + "]";
  93. }
  94. re = new RegExp (pattern, "i");
  95. for (i = 0; i < directory_camelcase.length; i++)
  96. if (directory_camelcase[i][0].search (re) != -1) {
  97. if (! isNaN (parseInt (match, 10)))
  98. return "";
  99. match = directory_camelcase[i][1];
  100. }
  101. if (isNaN (parseInt (match, 10)))
  102. return -1;
  103. return match;
  104. }
  105. function on_dtmf (session, type, obj, arg)
  106. {
  107. if (type == "dtmf") {
  108. dtmf_digits += obj.digit;
  109. extension = directory_lookup (dtmf_digits)
  110. return false;
  111. }
  112. return true;
  113. }
  114. function directory_prompt ()
  115. {
  116. var choice;
  117. var index;
  118. var repeat;
  119. extension = "";
  120. choice = "";
  121. repeat = 0;
  122. while (session.ready () && repeat < 3) {
  123. /* play phrase - if digit keyed while playing callback will catch them*/
  124. session.sayPhrase ("feith_aa_directory", "#", "", on_dtmf, "");
  125. choice = dtmf_digits;
  126. while ( isNaN (parseInt (extension, 10)) ) {
  127. if (! session.ready ())
  128. return "";
  129. dtmf_digits = session.getDigits (1, '#', digitTimeOut,
  130. interDigitTimeOut, absoluteTimeOut);
  131. choice += dtmf_digits;
  132. extension = directory_lookup (choice);
  133. }
  134. if (parseInt (extension, 10) >= 0)
  135. break;
  136. session.sayPhrase ("voicemail_invalid_extension", "#", "", on_dtmf, "");
  137. dtmf_digits = "";
  138. extension = "";
  139. choice = "";
  140. repeat++;
  141. session.flushDigits ();
  142. }
  143. return extension;
  144. }
  145. var choice = "";
  146. var fd;
  147. var i;
  148. var recorded_name;
  149. session.answer ();
  150. session.execute("sleep", "1000");
  151. load_directory ();
  152. build_camelcase_directory ();
  153. dtmf_digits = "";
  154. session.flushDigits ();
  155. choice = directory_prompt ();
  156. if (! session.ready ()) {
  157. session.hangup();
  158. exit();
  159. }
  160. if ( isNaN (parseInt (choice, 10)) || parseInt (choice, 10) <= 0) {
  161. session.execute ("transfer", operator + " XML default");
  162. exit();
  163. }
  164. for (i = 0; i < file_exts.length; i++) {
  165. recorded_name = voicemail_path + choice + "/recorded_name" + file_exts[i];
  166. fd = new File (recorded_name);
  167. if (fd.exists) {
  168. session.streamFile (recorded_name);
  169. break;
  170. }
  171. }
  172. session.execute ("phrase", "spell," + choice);
  173. session.execute ("transfer", choice + " XML default");
  174. exit();