Guest User

Untitled

a guest
Sep 1st, 2021
222
42 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Lua code regarding creating sessions and bridging:
  2. ************ Functions that are referenced in the following 4 sections of code************************
  3. function FS_exec_Bridge(self, value)
  4. self:execute("bridge", value)
  5. end
  6. function FS_getHangupCause(self)
  7. return self:hangupCause();
  8. end
  9. function FS_IsReady(self)
  10. if (self:ready()) then return true end
  11. return false;
  12. end
  13. function getStdRingback()
  14. return "ringback=%(2000,4000,440.0,480.0)"
  15. end
  16. function FS_setHangup_after_bridge(self)
  17. self:setVariable('hangup_after_bridge','false')
  18. end
  19. function FS_setInstantRingback(self)
  20. self:setVariable('instant_ringback', 'true')
  21. end
  22. function FS_setRingback(self, value)
  23. if(not IsNull(value)) then
  24. self:setVariable("ringback", value)
  25. end
  26. end
  27. function sendDigits(call_sid, dial_send_Digit)
  28. if not IsNull(dial_send_Digit) then
  29. send_digits="uuid_send_dtmf " .. call_sid .. " " .. dial_send_Digit
  30. local api = getFreeswitchAPI()
  31. api:executeString(send_digits)
  32. end
  33. end
  34. function FS_streamFile(self, value)
  35. return self:streamFile(value)
  36. end
  37. ***** There are 4 lua scripts that create a session and bridge the new session to an existing call
  38. The variable name "session" refers to the original call.
  39.  
  40. 1. ***************************************** Bridge inbound call - does not answer first
  41. local origBase = string.format("<absolute_codec_string=pcmu,suppress_cng=true,ignore_early_media=false,originate_timeout=%d,effective_caller_id_number=%s,origination_caller_id_number=%s>", TIMEOUT.originate_timeout, caller_id, caller_id)
  42. local originate = string.format("%s{%s}sofia/external/%s@%s ",origBase, concat_table(common_params,","), calledNumber, FS_getCustom_gateway(session))
  43.  
  44. -- Attempt to bridge the two calls
  45. FS_setRingback(session, getStdRingback())
  46. FS_setInstantRingback(session)
  47.  
  48. if FS_IsReady(session) then
  49. FS_setHangup_after_bridge(session)
  50. FS_exec_Bridge(session, originate)
  51. end
  52.  
  53. 2. ***************************************** Bridge sip call
  54. local common_params = {
  55. ["group_confirm_key"]=dial_params.groupconfirmkey,
  56. ["group_confirm_file"]=dial_params.groupconfirmfile,
  57. ["execute_on_answer"] = "playback::silence_stream://150",
  58. ["execute_on_answer01"] = "sched_hangup +" .. dial_params.timelimit,
  59. ["originate_timeout"] = dial_params.timeout,
  60. ["ignore_early_media"] = "true",
  61. ["origination_caller_id_number"] = caller_id,
  62. ["effective_caller_id_number"] = caller_id,
  63. ["origination_caller_id_name"] = dial_params.callername,
  64. ["call_timeout"]=dial_params.timeout,
  65. ... other company specific parameters
  66. ["suppress_cng"]="true",
  67. ["zrtp_secure_media"]="false"
  68. }
  69.  
  70. local bridge_params =
  71. { "[instant_ringback=true,origination_uuid=",
  72. childLog.call_sid,
  73. ",fsrouter_uuid=",
  74. childLog.call_sid,
  75. ",caller_id=",
  76. dial_params.callerid,
  77. ",called_num=",
  78. childLog.called_number,
  79. "]sofia/external/",
  80. childLog.called_number,
  81. ";fs_path=",
  82. SIP_FS_PATH,
  83. ","
  84. }
  85.  
  86. local BridgeCall = "{"..concat_table(common_params,",") .. "}".. concat_list(bridge_params)
  87.  
  88. FS_setHangup_after_bridge(session)
  89. local session1 = freeswitch.Session(BridgeCall, session)
  90.  
  91. if FS_IsReady(session) and FS_IsReady(session1) and FS_getHangupCause(session1) == "SUCCESS" then
  92. FS_setStatus(session1,childLog.status)
  93. if IsTrue(dial_params.confirmsound) then
  94. FS_streamFile(session,BEEP_WAV)
  95. end
  96.  
  97. freeswitch.bridge(session, session1);
  98. end
  99.  
  100. 3.*****************************************
  101. local originate_params = {
  102. ["execute_on_answer"] = "playback::silence_stream://150",
  103. ["originate_timeout"] = tag.attr.timeout,
  104. ["ignore_early_media"] = "true",
  105. ["instant_ringback"] = "true",
  106. ["effective_caller_id_number"] = origCallerId,
  107. ["origination_caller_id_number"] = origCallerId,
  108. ["origination_caller_id_name"] = dial_params.callername,
  109. ["call_timeout"] = dial_params.timeout,
  110. ... other company specific parameters
  111. ["suppress_cng"]="true"
  112. }
  113.  
  114. local bridge_params =
  115. { "[instant_ringback=true,origination_uuid=",
  116. childLog.call_sid,
  117. ",fsrouter_uuid=",
  118. childLog.call_sid,
  119. ",called_num=",
  120. childLog.called_number,
  121. "]sofia/external/",
  122. childLog.called_number,
  123. "@",
  124. FS_getCustom_gateway(session)
  125. }
  126. table.insert(bridgearray, concat_list(bridge_params))
  127. -- there can be multiple originates
  128.  
  129. local BridgeCall = "{"..concat_table(originate_params,",") .. "}".. table.concat(bridgearray,",")
  130. if not FS_IsReady(session) then
  131. return
  132. end
  133.  
  134. local session1 = freeswitch.Session(BridgeCall,session)
  135. FS_setHangup_after_bridge(session)
  136.  
  137. if (FS_IsReady(session1) and FS_IsReady(session)) and FS_getHangupCause(session1) == "SUCCESS" then
  138. FS_setStatus(session1, childLog.status)
  139. sendDigits(FS_getCallSid(session1), dial_params.senddigits)
  140. freeswitch.bridge(session, session1);
  141. logChildCallConfirmed(session1, callSid) - sends custom event
  142. session1:hangup()
  143. end
  144.  
  145. 4. ************************************************* can be for Group call
  146. local originate_params = {
  147. ["execute_on_answer"] = "playback::silence_stream://150",
  148. ["originate_timeout"] = tag.attr.timeout,
  149. ["ignore_early_media"] = "true",
  150. ["instant_ringback"] = "true",
  151. ["effective_caller_id_number"] = origCallerId,
  152. ["origination_caller_id_number"] = origCallerId,
  153. ["origination_caller_id_name"] = dial_params.callername,
  154. ["call_timeout"] = dial_params.timeout,
  155. ... other company specific parameters
  156. ["suppress_cng"]="true"
  157. }
  158.  
  159. local bridge_params =
  160. { "[instant_ringback=true,origination_uuid=",
  161. childLog.call_sid,
  162. ",fsrouter_uuid=",
  163. childLog.call_sid,
  164. ",called_num=",
  165. childLog.called_number,
  166. "]sofia/external/",
  167. childLog.called_number,
  168. "@",
  169. FS_getCustom_gateway(session)
  170. }
  171. table.insert(bridgearray,func_concat_list(bridge_params))
  172. - can have multiple originates
  173.  
  174. local BridgeCall = "{"..func_concat_table(common_params,",") .. "}".. table.concat(bridgearray,",")
  175.  
  176. if not FS_IsReady(session) then
  177. return (concat_list({"DialCallSid=",tostring(childLog.call_sid),"&DialCallStatus=",convertHangupCause(FS_getHangupCause(session))}))
  178. end
  179.  
  180. FS_setHangup_after_bridge(session)
  181. local session1 = freeswitch.Session(BridgeCall, session)
  182.  
  183. if FS_IsReady(session) and FS_IsReady(session1) and FS_getHangupCause(session1) == "SUCCESS" then
  184.  
  185. -- had to do the dtmf and onanswerplay this way because
  186. -- the execute on answer was not working as expected
  187. local hasAudio = not IsNull(dial_params.onanswerplay)
  188. --bleg dtmf
  189. if not IsNull(dial_params.playdtmf) then
  190. FS_SendDTMF(session1, dial_params.playdtmf, dial_params.playdtmfdelay, hasAudio)
  191. end
  192. -- bleg audio
  193. if FS_IsReady(session) and hasAudio then
  194. FS_streamFile(session1, dial_params.onanswerplay)
  195. end
  196.  
  197. -- Send answer callback for only the winner of call in custom event
  198. if groupCall == "true" then
  199. sendChildStatusCallback(childLog, session1)
  200. end
  201. if IsTrue(dial_params.confirmsound) then
  202. FS_streamFile(session,BEEP_WAV)
  203. end
  204. freeswitch.bridge(session, session1)
  205. logChildCallConfirmed(session1, callSid) -- custom event
  206. end
RAW Paste Data