index.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. --set default variables
  2. max_digits = 15;
  3. digit_timeout = 5000;
  4. --debug["sql"] = true;
  5. --general functions
  6. require "resources.functions.trim";
  7. --connect to the database
  8. local Database = require "resources.functions.database";
  9. dbh = Database.new('system');
  10. --include json library
  11. local json
  12. if (debug["sql"]) then
  13. json = require "resources.functions.lunajson"
  14. end
  15. --set the api
  16. api = freeswitch.API();
  17. --get the hostname
  18. local hostname = trim(api:execute("switchname", ""));
  19. --Get intercept logger
  20. local log = require "resources.functions.log".mobile_twinning
  21. --get the argv values
  22. call_direction = argv[2];
  23. call_uuid = "";
  24. --get the session variables
  25. if (session:ready()) then
  26. session:answer();
  27. session:execute("sleep", "1000");
  28. end
  29. --get the session variables
  30. if (session:ready()) then
  31. --general variables
  32. domain_uuid = session:getVariable("domain_uuid");
  33. domain_name = session:getVariable("domain_name");
  34. context = session:getVariable("context");
  35. uuid = session:get_uuid();
  36. caller_id_number = session:getVariable("caller_id_number");
  37. --set the sounds path for the language, dialect and voice
  38. default_language = session:getVariable("default_language");
  39. default_dialect = session:getVariable("default_dialect");
  40. default_voice = session:getVariable("default_voice");
  41. if (not default_language) then default_language = 'en'; end
  42. if (not default_dialect) then default_dialect = 'us'; end
  43. if (not default_voice) then default_voice = 'callie'; end
  44. end
  45. --define the sounds directory
  46. sounds_dir = session:getVariable("sounds_dir");
  47. sounds_dir = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice;
  48. --lookup caller-id to see if it is an associated mobile_number
  49. mobile_caller_id_number = string.sub(caller_id_number,-10);
  50. local params = {domain_uuid = domain_uuid, caller_id_number = mobile_caller_id_number }
  51. local sql = "SELECT m.mobile_twinning_number, m.extension_uuid, m.mobile_twinning_uuid, e.extension ";
  52. sql = sql .. "FROM v_mobile_twinnings as m ";
  53. sql = sql .. "LEFT OUTER JOIN v_extensions AS e ON e.extension_uuid = m.extension_uuid ";
  54. sql = sql .. "WHERE m.domain_uuid = :domain_uuid ";
  55. sql = sql .. "AND m.mobile_twinning_number = :caller_id_number ";
  56. if (debug["sql"]) then
  57. freeswitch.consoleLog("notice", "[mobile_twinning] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
  58. end
  59. dbh:query(sql, params, function(row)
  60. --set the variables
  61. mobile_twinning_number = row.mobile_twinning_number;
  62. extension_uuid = row.extension_uuid;
  63. mobile_twinning_uuid = row.mobile_twinning_uuid;
  64. extension = row.extension;
  65. end);
  66. --Call is from mobile
  67. if (mobile_twinning_uuid ~= nil and caller_id_number ~= nil) then
  68. min_digits = 1;
  69. max_digits = 1;
  70. max_tries = 1;
  71. session:streamFile("ivr/ivr-to_accept_press_one.wav");
  72. dtmf_digits = session:getDigits(max_digits, "#", 7000);
  73. if (dtmf_digits == "1") then
  74. local dbh = Database.new('switch')
  75. --check the database to the uuid of the desk call
  76. presence_id = extension.."@"..domain_name;
  77. call_hostname = "";
  78. sql = "SELECT uuid, call_uuid, direction, hostname FROM channels ";
  79. sql = sql .. "WHERE presence_id = '" .. presence_id .. "' ";
  80. sql = sql .. "AND call_uuid IS NOT NULL ";
  81. sql = sql .. "AND callstate = 'ACTIVE' ";
  82. if (debug["sql"]) then
  83. log.noticef("SQL: %s; params: %s", sql, json.encode(params));
  84. end
  85. local is_child
  86. dbh:query(sql, params, function(row)
  87. is_child = (row.uuid == row.call_uuid)
  88. call_uuid = row.call_uuid;
  89. call_hostname = row.hostname;
  90. direction = row.direction;
  91. end);
  92. log.notice("call_uuid: "..uuid .. " direction: " .. direction);
  93. if (direction == 'inbound') then
  94. leg = "bleg"
  95. end
  96. end
  97. end
  98. --if the call is from an extension, lookup the mobile number and try to pull the call off the mobile phone
  99. if (mobile_twinning_uuid == nil and caller_id_number ~= nil) then
  100. --lookup the extension_uuid
  101. local params = {domain_uuid = domain_uuid, caller_id_number = caller_id_number }
  102. local sql = "SELECT extension_uuid FROM v_extensions ";
  103. sql = sql .. "WHERE domain_uuid = :domain_uuid ";
  104. sql = sql .. "AND extension = :caller_id_number ";
  105. if (debug["sql"]) then
  106. freeswitch.consoleLog("notice", "[mobile_twinning] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
  107. end
  108. dbh:query(sql, params, function(row)
  109. --set the variables
  110. extension_uuid = row.extension_uuid;
  111. end);
  112. --lookup the mobile number associated to the extension
  113. local params = {domain_uuid = domain_uuid, extension_uuid = extension_uuid }
  114. local sql = "SELECT m.mobile_twinning_number, e.extension ";
  115. sql = sql .. "FROM v_mobile_twinnings as m ";
  116. sql = sql .. "LEFT OUTER JOIN v_extensions AS e ON e.extension_uuid = m.extension_uuid ";
  117. sql = sql .. "WHERE m.domain_uuid = :domain_uuid ";
  118. sql = sql .. "AND m.extension_uuid = :extension_uuid ";
  119. if (debug["sql"]) then
  120. freeswitch.consoleLog("notice", "[mobile_twinning] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
  121. end
  122. dbh:query(sql, params, function(row)
  123. --set the variables
  124. mobile_twinning_number = row.mobile_twinning_number;
  125. extension = row.extension;
  126. end);
  127. if (mobile_twinning_number ~= nil) then
  128. --lookup active calls to the mobile (for follow-me)
  129. --connect to FS database
  130. local dbh = Database.new('switch')
  131. --check the database for the uuid of the mobile call
  132. presence_id = caller_id_number.."@"..domain_name;
  133. call_hostname = "";
  134. sql = "SELECT uuid, call_uuid, hostname FROM channels ";
  135. sql = sql .. "WHERE callstate = 'ACTIVE' ";
  136. sql = sql .. "AND direction = 'outbound' ";
  137. sql = sql .. "AND dest like '%" .. mobile_twinning_number .. "' ";
  138. sql = sql .. "AND context = '" .. domain_name .. "' ";
  139. sql = sql .. "AND call_uuid IS NOT NULL ";
  140. if (debug["sql"]) then
  141. log.noticef("SQL: %s; params: %s", sql, json.encode(params));
  142. end
  143. local is_child
  144. dbh:query(sql, params, function(row)
  145. is_child = (row.uuid == row.call_uuid)
  146. call_uuid = row.call_uuid;
  147. call_hostname = row.hostname;
  148. end);
  149. log.notice("call_uuid from follow-me q1: "..call_uuid);
  150. --lookup active calls to the mobile (for instances where the mobile switched the call from the extension)
  151. if (call_uuid == nil or call_uuid == '') then
  152. --connect to FS database
  153. local dbh = Database.new('switch')
  154. --check the database for the uuid of the mobile call
  155. call_hostname = "";
  156. sql = "SELECT uuid, call_uuid, hostname FROM channels ";
  157. sql = sql .. "WHERE callstate = 'ACTIVE' ";
  158. sql = sql .. "AND direction = 'inbound' ";
  159. sql = sql .. "AND dest = '" .. extension .. "' ";
  160. sql = sql .. "AND call_uuid IS NOT NULL ";
  161. if (debug["sql"]) then
  162. log.noticef("SQL: %s; params: %s", sql, json.encode(params));
  163. end
  164. local is_child
  165. dbh:query(sql, params, function(row)
  166. is_child = (row.uuid == row.call_uuid)
  167. call_uuid = row.call_uuid;
  168. call_hostname = row.hostname;
  169. end);
  170. log.notice("call_uuid from follow-me q2: "..call_uuid);
  171. leg = "bleg"
  172. end
  173. --lookup active calls on the mobile (for instances where the mobile switched the call from the extension. Original call was outbound)
  174. if (call_uuid == nil or call_uuid == '') then
  175. --connect to FS database
  176. local dbh = Database.new('switch')
  177. --check the database for the uuid of the mobile call
  178. call_hostname = "";
  179. sql = "SELECT uuid, call_uuid, hostname FROM channels ";
  180. sql = sql .. "WHERE callstate = 'ACTIVE' ";
  181. sql = sql .. "AND direction = 'outbound' ";
  182. sql = sql .. "AND cid_num like '%" .. mobile_twinning_number .. "' ";
  183. sql = sql .. "AND call_uuid IS NOT NULL ";
  184. if (debug["sql"]) then
  185. log.noticef("SQL: %s; params: %s", sql, json.encode(params));
  186. end
  187. local is_child
  188. dbh:query(sql, params, function(row)
  189. is_child = (row.uuid == row.call_uuid)
  190. call_uuid = row.call_uuid;
  191. call_hostname = row.hostname;
  192. end);
  193. log.notice("call_uuid from follow-me q3: "..call_uuid);
  194. leg = "bleg"
  195. end
  196. end
  197. end
  198. --intercept a call that is on your mobile
  199. if (call_uuid ~= nil) then
  200. if (hostname == call_hostname) then
  201. if (leg == 'bleg') then
  202. session:execute("intercept", "-bleg " .. call_uuid);
  203. else
  204. session:execute("intercept", call_uuid);
  205. end
  206. -- else
  207. -- session:execute("export", "sip_h_X-intercept_uuid="..uuid);
  208. -- make_proxy_call(pickup_number, call_hostname)
  209. end
  210. else
  211. log.notice("No active call to transfer.");
  212. end