Advertisement
BoberDiversant

Untitled

Aug 30th, 2019
219
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.  
  50.  freeswitch.consoleLog("INFO", "scenarioFullParametersBase64: " .. scenarioFullParametersBase64);
  51.  
  52.  local recordingFile = "$${recordings_dir}/"..clientId.."/"..campaignId.."/"..callTaskId..".wav"
  53.  freeswitch.consoleLog("NOTICE", "start recording to " .. recordingFile);
  54.  session:execute("set", "RECORD_STEREO=true")
  55.  session:execute("record_session", recordingFile);
  56.  
  57.  local sipServletSession = freeswitch.Session("sofia/gateway/sipservlet/incomingSolutionV2?scenarioFullParametersBase64="..scenarioFullParametersBase64.."&scenarioResultTopic="..scenarioResultTopic)
  58.  
  59.  freeswitch.consoleLog("info","starting ivr from lua")
  60.  
  61.  freeswitch.bridge(session,  sipServletSession)
  62.  
  63.  local bridgeParams="{sip_cid_type=rpid,origination_caller_id_name="..callerNumber..",origination_caller_id_number="..callerNumber.."}"
  64.  local bridgeAfterIvr=sipServletSession:getVariable("sip_bye_h_X-Ivoice-BridgeDestination")
  65.  
  66.  if bridgeAfterIvr == nil then
  67.     freeswitch.consoleLog("INFO","bridgeAfterIvr is nil, hangup")
  68.     session:hangup()
  69.  else
  70.     freeswitch.consoleLog("INFO","bridgeAfterIvr not nil, bridging to " .. bridgeAfterIvr)
  71.     local bridgeAfterIvrSession = freeswitch.Session(bridgeParams..bridgeAfterIvr)
  72.     freeswitch.bridge(session, bridgeAfterIvrSession)
  73.  end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement