package.cpath ="/usr/lib/x86_64-linux-gnu/lua/5.2/?.so;" .. package.cpath package.path = "/usr/share/lua/5.2/?.lua;" .. package.path local https = require("ssl.https") local http = require("socket.http") local ltn12 = require "ltn12" local json = require "json" session:answer() local call_started_timestamp = os.time() * 1000; freeswitch.consoleLog("INFO", "IVR CALL STARTED AT : " .. call_started_timestamp) local call_uuid = session:getVariable("uuid"); freeswitch.consoleLog("INFO", "CALL UUID : " .. call_uuid) local caller_id_number = session:getVariable("caller_id_number"); freeswitch.consoleLog("INFO", "CALLER ID NUMBER : " .. caller_id_number) local call_id = session:getVariable("sip_call_id") freeswitch.consoleLog("INFO", "SIP CALL ID : " .. call_id) local destination_number = session:getVariable("destination_number"); freeswitch.consoleLog("INFO", "SERVICE IDENTIFIER : " .. destination_number) local env_file_name = "cx_env" .. destination_number local cx_env = require(env_file_name) session:setAutoHangup(false) -- Play welcome prompt --session:streamFile(cx_env.ivr_prompts_folder .. "welcome.wav"); local activities = {} -- Avoids call hangup when making agent request function session_transfer() freeswitch.consoleLog("INFO", "SKIPPING SESSION HANGUP ON AGENT TRANSFER\n") end -- Get AUTH token from CX local function get_token() if (not cx_env.auth_enabled) then return "none" end freeswitch.consoleLog("INFO", "-------------------GET TOKEN FUNCTION START-------------------\n") local customer_left_timestamp = os.time() * 1000; local req_body1 = "grant_type=password&client_id=" .. cx_env.client_id .. "&client_secret=" .. cx_env.client_secret .. "&username=" .. cx_env.username .. "&password=" .. cx_env.password freeswitch.consoleLog("INFO", "TOKEN REQUEST PAYLOAD :" .. req_body1 .. "\n") local response_body = {} local api_url = cx_env.cxFqdn .. "/auth/realms/" .. cx_env.auth_realm .. "/protocol/openid-connect/token" freeswitch.consoleLog("INFO", "POSTING TOKEN REQUEST :" .. api_url .. "\n") local res, code, headers, status = https.request { method = "POST", url = api_url, source = ltn12.source.string(req_body1), headers = { ["content-type"] = "application/x-www-form-urlencoded", ["Content-Length"] = string.len(req_body1) }, sink = ltn12.sink.table(response_body) } freeswitch.consoleLog("INFO", "RESPONSE BODY = " .. tostring(res) .. "\n STATUS CODE = " .. tostring(code) .. "\n STATUS BODY = " .. tostring(status) .."\n") if (code == 200) then local fixedResponse = "" for key,value in pairs(response_body) do fixedResponse = fixedResponse .. value end token = json.decode(fixedResponse)["access_token"] freeswitch.consoleLog("INFO", " AUTH TOKEN: " .. token) return token end return "none" end -- This function calls when the call hanup on IVR before Agent reguest function session_hangup_hook() freeswitch.consoleLog("INFO", "--------------------IVR ACTIVITY DIALOG ENDED FUNCTION START--------------------\n") local t_activities = json.encode(activities) freeswitch.consoleLog("INFO", "--------- " .. t_activities ) local call_hangup_timestamp = os.time() * 1000; local jsonstring = '{ "id": "15242","header": ' jsonstring = jsonstring .. '{"channelData": {"channelCustomerIdentifier": "' .. tostring(caller_id_number) ..'", "serviceIdentifier": "' .. destination_number .. '" },' jsonstring = jsonstring .. '"intent": "IVR_AGGREGATED_ACTIVITY", "sender": { "id": "f1370ff7-43fa-496e-9966-e64061d35f5c","senderName": "MY-IVR","type": "IVR"}},' jsonstring = jsonstring .. '"body": {"type": "IVR_AGGREGATED_ACTIVITY","callId": "'..call_id..'", "startTime": '.. call_started_timestamp ..', "endTime": '..call_hangup_timestamp..', "startDirection": "INBOUND","endDirection": "DIALOG_ENDED",' jsonstring = jsonstring .. '"activities": '.. t_activities .. '}}' freeswitch.consoleLog("INFO", "IVR ACTIVITY DIALOG ENDED PAYLOAD : " .. jsonstring .. "\n") local req_body2 = json.decode(jsonstring) local req_body1 = json.encode(req_body2) local response_body = {} freeswitch.consoleLog("INFO", "POSTING IVR ACTIVITY DIALOG ENDED TO CCM API :" .. cx_env.cxFqdn .. "/ccm/message/receive" .. "\n") local res, code, headers, status = https.request { method = "POST", url = cx_env.cxFqdn .. "/ccm/message/receive", source = ltn12.source.string(req_body1), headers = { ["content-type"] = "application/json", ["Content-Length"] = string.len(req_body1), ["Authorization"] = "Bearer " .. get_token() }, sink = ltn12.sink.table(response_body) } freeswitch.consoleLog("INFO", "RESPONSE BODY = " .. tostring(res) .. "\n STATUS CODE = " .. tostring(code) .. "\n STATUS BODY = " .. tostring(status) .."\n") freeswitch.consoleLog("INFO", "--------------------IVR ACTIVITY DIALOG ENDED FUNCTION END--------------------\n") end -- This function calls when we initiate the transfer request for the agent function session_transfer_hook(activities) freeswitch.consoleLog("INFO", "--------------------IVR ACTIVITY TRANSFER FUNCTION START--------------------\n") local t_activities = json.encode(activities) freeswitch.consoleLog("INFO", "--------- " ..t_activities ) local call_transfer_timestamp = os.time() * 1000; local jsonstring = '{ "id": "15242","header": ' jsonstring = jsonstring .. '{"channelData": {"channelCustomerIdentifier": "' .. tostring(caller_id_number) ..'", "serviceIdentifier": "' .. destination_number .. '" },' jsonstring = jsonstring .. '"intent": "IVR_AGGREGATED_ACTIVITY", "sender": { "id": "f1370ff7-43fa-496e-9966-e64061d35f5c","senderName": "MY-IVR","type": "IVR"}},' jsonstring = jsonstring .. '"body": {"type": "IVR_AGGREGATED_ACTIVITY","callId": "'..call_id..'", "startTime": '.. call_started_timestamp ..', "endTime": '..call_transfer_timestamp..', "startDirection": "INBOUND","endDirection": "TRANSFER",' jsonstring = jsonstring .. '"activities": '.. t_activities .. '}}' freeswitch.consoleLog("INFO", "IVR ACTIVITY TRANSFER PAYLOAD : " .. jsonstring .. "\n") local req_body2 = json.decode(jsonstring) local req_body1 = json.encode(req_body2) local response_body = {} freeswitch.consoleLog("INFO", "POSTING IVR ACTIVITY TRANSFER TO CCM API :" .. cx_env.cxFqdn .. "/ccm/message/receive" .. "\n") local res, code, headers, status = https.request { method = "POST", url = cx_env.cxFqdn .. "/ccm/message/receive", source = ltn12.source.string(req_body1), headers = { ["content-type"] = "application/json", ["Content-Length"] = string.len(req_body1), ["Authorization"] = "Bearer " .. get_token() }, sink = ltn12.sink.table(response_body) } freeswitch.consoleLog("INFO", "RESPONSE BODY = " .. tostring(res) .. "\n STATUS CODE = " .. tostring(code) .. "\n STATUS BODY = " .. tostring(status) .."\n") freeswitch.consoleLog("INFO", "--------------------IVR ACTIVITY TRANSFER FUNCTION END--------------------\n") end session:setVariable("session_in_hangup_hook", "true"); session:setHangupHook("session_hangup_hook"); -- Main menu of IVR function main_menu() freeswitch.consoleLog("INFO", "--------------------MAIN MENU FUNCTION START--------------------\n") local digit = '' for i = 1,5,1 do -- Play prompt and get pressed digits --local digits = session:playAndGetDigits(1, 2, 3, 3000, "#", cx_env.ivr_prompts_folder .. "main_menu_sale.wav", cx_env.ivr_prompts_folder .. "Wrongentry.wav", "\\d+") local digits = "0" session:consoleLog("info", "Got DTMF digits: ".. digits .."\n") if (digits == "1") then table.insert(activities, { menu = "Main Menu", selection = "1", timestamp = os.time() * 1000, metadata = {} }); sales_sub_menu() break; elseif (digits == "2") then table.insert(activities, { menu = "Main Menu", selection = "2", timestamp = os.time() * 1000, metadata = {} }); -- Adding priority -- session:setVariable("sip_h_X-queuePriority", 5) transfer_to_agent(cx_env.queue, cx_env.queueType) break; elseif (digits == "0") then table.insert(activities, { menu = "Main Menu", selection = "0", timestamp = os.time() * 1000, metadata = {} }); transfer_to_agent(cx_env.queue, cx_env.queueType) break; else -- Play wrong entry message session:streamFile(cx_env.ivr_prompts_folder .. "Wrongentry.wav"); end session:sleep(1000); end freeswitch.consoleLog("INFO", "--------------------MAIN MENU FUNCTION END--------------------\n") end -- Sub menu of IVR function sales_sub_menu() freeswitch.consoleLog("INFO", "--------------------SALES SUB MENU FUNCTION START--------------------\n") local digit = '' for i = 1,5,1 do local digits = session:playAndGetDigits(1, 2, 3, 4000, "#", cx_env.ivr_prompts_folder .. "sales_submenu.wav", cx_env.ivr_prompts_folder .. "Wrongentry.wav", "\\d+") session:consoleLog("info", "Got DTMF digits: ".. digits .."\n") if (digits == "1") then table.insert(activities, { menu = "Sub Meun Sales", selection = "1", timestamp = os.time() * 1000, metadata = {} }); transfer_to_agent(cx_env.queue, cx_env.queueType) break; elseif (digits == "2") then table.insert(activities, { menu = "Sub Meun Sales", selection = "2", timestamp = os.time() * 1000, metadata = {} }); transfer_to_agent(cx_env.queue, cx_env.queueType) break; elseif (digits == "3") then table.insert(activities, { menu = "Sub Meun Sales", selection = "3", timestamp = os.time() * 1000, metadata = {} }); transfer_to_agent(cx_env.queue, cx_env.queueType) break; else session:streamFile(cx_env.ivr_prompts_folder .. "Wrongentry.wav"); end session:sleep(1000); end freeswitch.consoleLog("INFO", "--------------------SALES SUB MENU FUNCTION END--------------------\n") end -- This function is called when the customer chooses to talk with an agent function transfer_to_agent(queue, queueType) freeswitch.consoleLog("INFO", "REQUEST AGENT OPTION SELECTED\n") -- Set queue info in SIP headers session:setVariable("sip_h_X-queueType", queueType); session:setVariable("sip_h_X-queue", queue); session:setVariable("sip_rh_X-queueType", queueType); session:setVariable("sip_rh_X-queue", queue); session:setVariable("session_in_hangup_hook", "false"); -- Play prompt before sending agent request session:streamFile(cx_env.ivr_prompts_folder .. "connect_to_agent.wav"); session:sleep(1000); session:setVariable("session_in_hangup_hook", "true"); session:setHangupHook("session_transfer"); local json_activites = json.encode(activities) if (not session:ready()) then return end freeswitch.consoleLog("INFO", "IVR JOURNEY : " ..json_activites ) session_transfer_hook(activities) -- Call voice connector communication script dofile("/usr/share/freeswitch/scripts/vcApi.lua") end main_menu()