Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- differs from the original only in the block
- -- fallback = function(fallbackBridgeDestination, fallbackAudio)
- -- and
- -- if bridgeAfterIvr == nil then
- -- glazka uses deflect to bridge (client pbx can't get parameters without)
- dofile('/usr/share/freeswitch/scripts/lua/libs/jsonlua.lua');
- local jsonLib = jsonlua();
- session:setVariable("curl_timeout", "10");
- -- returns scenario full parameters or log error then hangup
- requestFullScenarioParameters = function(ctsHost, ctsPort, campaignId, callUuid, phone)
- local result = {
- error = false;
- json = nil;
- }
- local fullRequest = "http://"..ctsHost..":"..ctsPort.."/incomingCallTask/initForCampaignId?campaignId="..campaignId.."&callUuid="..callUuid.."&phone="..phone.." post"
- freeswitch.consoleLog("debug", "performing curl " .. fullRequest);
- session:execute("curl", fullRequest);
- local curl_response_code = session:getVariable("curl_response_code");
- if tonumber(curl_response_code) ~= 200 then
- freeswitch.consoleLog("ERR", "only 200 code expected from http api requests, but got " .. curl_response_code .. " for " .. fullRequest);
- result.error=true;
- end
- result.json = session:getVariable("curl_response_data");
- return result;
- end
- -- gets available up and running sip-servlet from broker
- requestSipServletFromBroker = function(brokerApi, campaignId, callTaskId)
- local result = {
- error = false;
- data = nil;
- }
- local fullRequest = brokerApi.."/manager/next?campaignId="..campaignId.."&callTaskId="..callTaskId;
- freeswitch.consoleLog("debug", "performing curl " .. fullRequest);
- session:execute("curl", fullRequest);
- local curl_response_code = session:getVariable("curl_response_code");
- if tonumber(curl_response_code) ~= 200 then
- freeswitch.consoleLog("ERR", "only 200 code expected from broker api, but got " .. curl_response_code .. " for " .. fullRequest);
- result.error=true;
- end
- result.data = session:getVariable("curl_response_data");
- return result;
- end
- fallback = function(fallbackBridgeDestination, fallbackAudio)
- if fallbackBridgeDestination == nil then
- freeswitch.consoleLog("INFO", "FallbackBridgeDestination is nil, will play "..fallbackAudio);
- session:execute("playback", fallbackAudio);
- session:hangup();
- return;
- end
- freeswitch.consoleLog("INFO", "bridging to " .. fallbackBridgeDestination);
- -- local fallbackBridgeSession = freeswitch.Session(fallbackBridgeDestination);
- -- freeswitch.bridge(session, fallbackBridgeSession);
- session:execute("deflect", fallbackBridgeDestination);
- end
- local campaignId=session:getVariable("sip_h_CampaignId")
- local clientId=session:getVariable("sip_h_ClientId")
- local callTaskServiceHost=session:getVariable("sip_h_CallTaskServiceHost")
- local callTaskServicePort=session:getVariable("sip_h_callTaskServicePort")
- local callerNumber=session:getVariable("sip_h_CallerIdNumber")
- local scenarioResultTopic=session:getVariable("sip_h_ResultTopic")
- local callUuid=session:getVariable("call_uuid")
- local phone=session:getVariable("caller_id_number")
- local fallbackBridgeDestination=session:getVariable("FallbackBridgeDestination");
- local fallbackAudio=session:getVariable("FallbackAudio");
- if fallbackAudio == nil then
- fallbackAudio = "http://audios.ivoice.online/common/fallback.wav";
- end
- local result = requestFullScenarioParameters(callTaskServiceHost, callTaskServicePort, campaignId, callUuid, phone);
- if result.error then
- session:hangup();
- return
- end
- local scenarioFullParameters=jsonLib.decode(result.json)
- local callTaskId = scenarioFullParameters.callTaskId
- freeswitch.consoleLog("INFO", "callTaskId: " .. callTaskId);
- local campaignState = scenarioFullParameters.campaignState
- freeswitch.consoleLog("INFO", "campaignState: " .. campaignState);
- if campaignState == "PAUSED" then
- fallback(fallbackBridgeDestination, fallbackAudio);
- return;
- end
- local SipServlet=session:getVariable("SipServlet")
- if SipServlet == nil then
- local SipServletBrokerApi=session:getVariable("SipServletBrokerApi")
- result = requestSipServletFromBroker(SipServletBrokerApi, campaignId, callTaskId);
- if result.error then
- freeswitch.consoleLog("INFO", "no available SipServlet");
- fallback(fallbackBridgeDestination, fallbackAudio);
- return;
- end
- SipServlet = result.data;
- end
- freeswitch.consoleLog("INFO", "dispatching request to: " .. SipServlet)
- freeswitch.consoleLog("INFO", "callTaskId: " .. callTaskId);
- local recordingFile = "$${recordings_dir}/"..clientId.."/"..campaignId.."/"..callTaskId..".wav"
- freeswitch.consoleLog("INFO", "start recording to " .. recordingFile);
- session:execute("set", "RECORD_STEREO=true")
- session:execute("record_session", recordingFile);
- local sipServletSession = freeswitch.Session("sofia/gateway/"..SipServlet.."/incomingSolutionV3?callTaskServiceHost="..callTaskServiceHost.."&callTaskServicePort="..callTaskServicePort.."&callTaskId="..callTaskId.."&callUuid="..callUuid.."&scenarioResultTopic="..scenarioResultTopic)
- freeswitch.consoleLog("info","starting ivr from lua")
- freeswitch.bridge(session, sipServletSession)
- -- session:execute("stop_record_session", recordingFile);
- local bridgeParams="{sip_cid_type=rpid,origination_caller_id_name="..callerNumber..",origination_caller_id_number="..callerNumber.."}"
- local bridgeAfterIvr=sipServletSession:getVariable("sip_bye_h_X-Ivoice-BridgeDestination")
- if bridgeAfterIvr == nil then
- freeswitch.consoleLog("INFO","bridgeAfterIvr is nil, hangup")
- session:hangup()
- else
- freeswitch.consoleLog("INFO","bridgeAfterIvr not nil, bridging to " .. bridgeAfterIvr)
- session:execute("deflect", bridgeAfterIvr);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement