Advertisement
Bobrovozka

Untitled

Aug 18th, 2021
1,559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.76 KB | None | 0 0
  1. -- differs from the original only in the block
  2. -- fallback = function(fallbackBridgeDestination, fallbackAudio)
  3. -- and
  4. -- if bridgeAfterIvr == nil then
  5. -- glazka uses deflect to bridge (client pbx can't get parameters without)
  6.  
  7. dofile('/usr/share/freeswitch/scripts/lua/libs/jsonlua.lua');
  8.  
  9. local jsonLib = jsonlua();
  10.  
  11. session:setVariable("curl_timeout", "10");
  12.  
  13. -- returns scenario full parameters or log error then hangup
  14. requestFullScenarioParameters = function(ctsHost, ctsPort, campaignId, callUuid, phone)
  15.     local result = {
  16.         error = false;
  17.         json = nil;
  18.     }
  19.  
  20.     local fullRequest = "http://"..ctsHost..":"..ctsPort.."/incomingCallTask/initForCampaignId?campaignId="..campaignId.."&callUuid="..callUuid.."&phone="..phone.." post"
  21.  
  22.     freeswitch.consoleLog("debug", "performing curl " .. fullRequest);
  23.  
  24.     session:execute("curl", fullRequest);
  25.     local curl_response_code = session:getVariable("curl_response_code");
  26.     if tonumber(curl_response_code) ~= 200 then
  27.         freeswitch.consoleLog("ERR", "only 200 code expected from http api requests, but got " .. curl_response_code .. " for " .. fullRequest);
  28.         result.error=true;
  29.     end
  30.     result.json = session:getVariable("curl_response_data");
  31.     return result;
  32. end
  33.  
  34. -- gets available up and running sip-servlet from broker
  35. requestSipServletFromBroker = function(brokerApi, campaignId, callTaskId)
  36.     local result = {
  37.         error = false;
  38.         data = nil;
  39.     }
  40.  
  41.     local fullRequest = brokerApi.."/manager/next?campaignId="..campaignId.."&callTaskId="..callTaskId;
  42.  
  43.     freeswitch.consoleLog("debug", "performing curl " .. fullRequest);
  44.  
  45.     session:execute("curl", fullRequest);
  46.     local curl_response_code = session:getVariable("curl_response_code");
  47.     if tonumber(curl_response_code) ~= 200 then
  48.         freeswitch.consoleLog("ERR", "only 200 code expected from broker api, but got " .. curl_response_code .. " for " .. fullRequest);
  49.         result.error=true;
  50.     end
  51.     result.data = session:getVariable("curl_response_data");
  52.     return result;
  53. end
  54.  
  55. fallback = function(fallbackBridgeDestination, fallbackAudio)
  56.     if fallbackBridgeDestination == nil then
  57.         freeswitch.consoleLog("INFO", "FallbackBridgeDestination is nil, will play "..fallbackAudio);
  58.         session:execute("playback", fallbackAudio);
  59.         session:hangup();
  60.         return;
  61.     end
  62.     freeswitch.consoleLog("INFO", "bridging to " .. fallbackBridgeDestination);
  63.     -- local fallbackBridgeSession = freeswitch.Session(fallbackBridgeDestination);
  64.     -- freeswitch.bridge(session, fallbackBridgeSession);
  65.     session:execute("deflect", fallbackBridgeDestination);
  66. end
  67.  
  68. local campaignId=session:getVariable("sip_h_CampaignId")
  69. local clientId=session:getVariable("sip_h_ClientId")
  70. local callTaskServiceHost=session:getVariable("sip_h_CallTaskServiceHost")
  71. local callTaskServicePort=session:getVariable("sip_h_callTaskServicePort")
  72. local callerNumber=session:getVariable("sip_h_CallerIdNumber")
  73. local scenarioResultTopic=session:getVariable("sip_h_ResultTopic")
  74. local callUuid=session:getVariable("call_uuid")
  75. local phone=session:getVariable("caller_id_number")
  76. local fallbackBridgeDestination=session:getVariable("FallbackBridgeDestination");
  77. local fallbackAudio=session:getVariable("FallbackAudio");
  78.  
  79. if fallbackAudio == nil then
  80.     fallbackAudio = "http://audios.ivoice.online/common/fallback.wav";
  81. end
  82.  
  83. local result = requestFullScenarioParameters(callTaskServiceHost, callTaskServicePort, campaignId, callUuid, phone);
  84. if result.error then
  85.     session:hangup();
  86.     return
  87. end
  88.  
  89. local scenarioFullParameters=jsonLib.decode(result.json)
  90.  
  91. local callTaskId = scenarioFullParameters.callTaskId
  92. freeswitch.consoleLog("INFO", "callTaskId: " .. callTaskId);
  93.  
  94. local campaignState = scenarioFullParameters.campaignState
  95. freeswitch.consoleLog("INFO", "campaignState: " .. campaignState);
  96.  
  97. if campaignState == "PAUSED" then
  98.     fallback(fallbackBridgeDestination, fallbackAudio);
  99.     return;
  100. end
  101.  
  102. local SipServlet=session:getVariable("SipServlet")
  103. if SipServlet == nil then
  104.     local SipServletBrokerApi=session:getVariable("SipServletBrokerApi")
  105.     result = requestSipServletFromBroker(SipServletBrokerApi, campaignId, callTaskId);
  106.     if result.error then
  107.         freeswitch.consoleLog("INFO", "no available SipServlet");
  108.         fallback(fallbackBridgeDestination, fallbackAudio);
  109.         return;
  110.     end
  111.     SipServlet = result.data;
  112. end
  113. freeswitch.consoleLog("INFO", "dispatching request to: " .. SipServlet)
  114.  
  115. freeswitch.consoleLog("INFO", "callTaskId: " .. callTaskId);
  116.  
  117. local recordingFile = "$${recordings_dir}/"..clientId.."/"..campaignId.."/"..callTaskId..".wav"
  118. freeswitch.consoleLog("INFO", "start recording to " .. recordingFile);
  119. session:execute("set", "RECORD_STEREO=true")
  120. session:execute("record_session", recordingFile);
  121.  
  122. local sipServletSession = freeswitch.Session("sofia/gateway/"..SipServlet.."/incomingSolutionV3?callTaskServiceHost="..callTaskServiceHost.."&callTaskServicePort="..callTaskServicePort.."&callTaskId="..callTaskId.."&callUuid="..callUuid.."&scenarioResultTopic="..scenarioResultTopic)
  123.  
  124. freeswitch.consoleLog("info","starting ivr from lua")
  125.  
  126. freeswitch.bridge(session,  sipServletSession)
  127.  
  128. --  session:execute("stop_record_session", recordingFile);
  129.  
  130. local bridgeParams="{sip_cid_type=rpid,origination_caller_id_name="..callerNumber..",origination_caller_id_number="..callerNumber.."}"
  131. local bridgeAfterIvr=sipServletSession:getVariable("sip_bye_h_X-Ivoice-BridgeDestination")
  132.  
  133. if bridgeAfterIvr == nil then
  134.     freeswitch.consoleLog("INFO","bridgeAfterIvr is nil, hangup")
  135.     session:hangup()
  136. else
  137.     freeswitch.consoleLog("INFO","bridgeAfterIvr not nil, bridging to " .. bridgeAfterIvr)
  138.     session:execute("deflect", bridgeAfterIvr);
  139. end
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement