Advertisement
BoberDiversant

Untitled

Aug 30th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. dofile('/usr/share/freeswitch/scripts/lua/libs/jsonlua.lua');
  2. dofile('/usr/share/freeswitch/scripts/lua/libs/base64.lua');
  3.  
  4. local jsonLib = jsonlua();
  5.  
  6. -- returns scenario full parameters or log error then hangup
  7. requestFullScenarioParameters = function(ctsHost, ctsPort, campaignId, callUuid, phone)
  8.     local result = {
  9.         error = false;
  10.         json = nil;
  11.     }
  12.  
  13.     local fullRequest = "http://"..ctsHost..":"..ctsPort.."/incomingCallTask/initForCampaignId?campaignId="..campaignId.."&callUuid="..callUuid.."&phone="..phone.." post"
  14.  
  15.     freeswitch.consoleLog("debug", "performing curl " .. fullRequest);
  16.  
  17.     session:execute("curl", fullRequest);
  18.     local curl_response_code = session:getVariable("curl_response_code");
  19.     if tonumber(curl_response_code) ~= 200 then
  20.         freeswitch.consoleLog("ERR", "only 200 code expected from http api requests, but got " .. curl_response_code .. " for " .. fullRequest);
  21.         result.error=true;
  22.     end
  23.     result.json = session:getVariable("curl_response_data");
  24.     return result;
  25.  end
  26.  
  27.  local campaignId=session:getVariable("sip_h_CampaignId")
  28.  local clientId=session:getVariable("sip_h_ClientId")
  29.  local callTaskServiceHost=session:getVariable("sip_h_CallTaskServiceHost")
  30.  local callTaskServicePort=session:getVariable("sip_h_callTaskServicePort")
  31.  local callerNumber=session:getVariable("sip_h_CallerIdNumber")
  32.  local scenarioResultTopic=session:getVariable("sip_h_ResultTopic")
  33.  local callUuid=session:getVariable("call_uuid")
  34.  local phone=session:getVariable("caller_id_number")
  35.  
  36.  local result = requestFullScenarioParameters(callTaskServiceHost, callTaskServicePort, campaignId, callUuid, phone);
  37.  if result.error then
  38.     session:hangup();
  39.     return
  40.  end
  41. --  freeswitch.consoleLog("INFO", "loaded fullScenarioParameters: " .. result.json);
  42.  
  43.  local scenarioFullParameters=jsonLib.decode(result.json)
  44.  
  45.  local callTaskId = scenarioFullParameters.callTaskId
  46.  freeswitch.consoleLog("INFO", "callTaskId: " .. callTaskId);
  47.  
  48.  local scenarioFullParametersBase64 = encodeBase64(result.json)
  49.  freeswitch.consoleLog("INFO", "scenarioFullParametersBase64: " .. scenarioFullParametersBase64);
  50.  
  51.  local recordingFile = "$${recordings_dir}/"..clientId.."/"..campaignId.."/"..callTaskId..".wav"
  52.  freeswitch.consoleLog("INFO", "start recording to " .. recordingFile);
  53.  session:execute("set", "RECORD_STEREO=true")
  54.  session:execute("record_session", recordingFile);
  55.  
  56.  local sipServletSession = freeswitch.Session("sofia/gateway/sipservlet/incomingSolutionV2?scenarioFullParametersBase64="..scenarioFullParametersBase64.."&scenarioResultTopic="..scenarioResultTopic)
  57.  
  58.  freeswitch.consoleLog("info","starting ivr from lua")
  59.  
  60.  freeswitch.bridge(session,  sipServletSession)
  61.  
  62.  local bridgeParams="{sip_cid_type=rpid,origination_caller_id_name="..callerNumber..",origination_caller_id_number="..callerNumber.."}"
  63.  local bridgeAfterIvr=sipServletSession:getVariable("sip_bye_h_X-Ivoice-BridgeDestination")
  64.  
  65.  if bridgeAfterIvr == nil then
  66.     freeswitch.consoleLog("INFO","bridgeAfterIvr is nil, hangup")
  67.     session:hangup()
  68.  else
  69.     freeswitch.consoleLog("INFO","bridgeAfterIvr not nil, bridging to " .. bridgeAfterIvr)
  70.     local bridgeAfterIvrSession = freeswitch.Session(bridgeParams..bridgeAfterIvr)
  71.     freeswitch.bridge(session, bridgeAfterIvrSession)
  72.  end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement