Update call_flow.lua
This commit is contained in:
@@ -36,6 +36,10 @@
|
|||||||
local Settings = require "resources.functions.lazy_settings"
|
local Settings = require "resources.functions.lazy_settings"
|
||||||
local file = require "resources.functions.file"
|
local file = require "resources.functions.file"
|
||||||
local log = require "resources.functions.log".call_flow
|
local log = require "resources.functions.log".call_flow
|
||||||
|
local basename = require "resources.functions.basename"
|
||||||
|
local is_absolute_path = require "resources.functions.is_absolute_path"
|
||||||
|
local find_file = require "resources.functions.find_file"
|
||||||
|
local play_file = require "resources.functions.play_file"
|
||||||
|
|
||||||
--include json library
|
--include json library
|
||||||
local json
|
local json
|
||||||
@@ -46,156 +50,65 @@
|
|||||||
--connect to the database
|
--connect to the database
|
||||||
local dbh = Database.new('system');
|
local dbh = Database.new('system');
|
||||||
|
|
||||||
--! @todo move to library
|
--get the variables
|
||||||
local function basename(file_name)
|
if (session:ready()) then
|
||||||
return (string.match(file_name, "([^/]+)$"))
|
|
||||||
end
|
|
||||||
|
|
||||||
--! @todo move to library
|
|
||||||
local function isabspath(file_name)
|
|
||||||
return string.sub(file_name, 1, 1) == '/' or string.sub(file_name, 2, 1) == ':'
|
|
||||||
end
|
|
||||||
|
|
||||||
--! @todo move to library
|
|
||||||
local function find_file(dbh, domain_name, domain_uuid, file_name)
|
|
||||||
-- if we specify e.g. full path
|
|
||||||
if isabspath(file_name) and file.exists(file_name) then
|
|
||||||
log.debugf('found file `%s` in file system', file_name)
|
|
||||||
return file_name
|
|
||||||
end
|
|
||||||
|
|
||||||
local file_name_only = basename(file_name)
|
|
||||||
|
|
||||||
local is_base64, found
|
|
||||||
|
|
||||||
if file_name_only == file_name then -- this can be recordings
|
|
||||||
local full_path = recordings_dir .. "/" .. domain_name .. "/" .. file_name
|
|
||||||
if file.exists(full_path) then
|
|
||||||
log.debugf('resolve `%s` as recording `%s`', file_name, full_path)
|
|
||||||
file_name, found = full_path, true
|
|
||||||
else -- recordings may be in database
|
|
||||||
local settings = Settings.new(dbh, domain_name, domain_uuid)
|
|
||||||
local storage_type = settings:get('recordings', 'storage_type', 'text') or ''
|
|
||||||
if storage_type == 'base64' then
|
|
||||||
local sql = "SELECT recording_base64 FROM v_recordings "
|
|
||||||
.. "WHERE domain_uuid = :domain_uuid"
|
|
||||||
.. "AND recording_filename = :file_name "
|
|
||||||
local params = {domain_uuid = domain_uuid, file_name = file_name};
|
|
||||||
if (debug["sql"]) then
|
|
||||||
log.notice("SQL: " .. sql .. "; params: " .. json.encode(params))
|
|
||||||
end
|
|
||||||
|
|
||||||
local dbh = Database.new('system', 'base64/read')
|
|
||||||
local recording_base64 = dbh:first_value(sql, params);
|
|
||||||
dbh:release();
|
|
||||||
|
|
||||||
if recording_base64 and #recording_base64 > 32 then
|
|
||||||
log.debugf('resolve `%s` as recording `%s`(base64)', file_name, full_path)
|
|
||||||
file_name, found, is_base64 = full_path, true, true
|
|
||||||
file.write_base64(file_name, recording_base64)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if not found then
|
|
||||||
local sounds_dir
|
|
||||||
if session then
|
|
||||||
-- Implemented based on stream.lua. But seems it never works.
|
|
||||||
-- because if we have file like `digits/1.wav` but full_path is `sounds_dir/digits/XXXX/1.wav`
|
|
||||||
sounds_dir = session:getVariable("sounds_dir")
|
|
||||||
local default_language = session:getVariable("default_language") or 'en'
|
|
||||||
local default_dialect = session:getVariable("default_dialect") or 'us'
|
|
||||||
local default_voice = session:getVariable("default_voice") or 'callie'
|
|
||||||
|
|
||||||
sounds_dir = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice
|
|
||||||
else
|
|
||||||
--! @todo implement for not sessoin call.
|
|
||||||
end
|
|
||||||
|
|
||||||
if sounds_dir then
|
|
||||||
found = file.exists(sounds_dir.. "/" ..file_name_only)
|
|
||||||
if found then
|
|
||||||
log.debugf('resolve `%s` as sound `%s`', file_name, found)
|
|
||||||
file_name, found = found, true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if not found then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
return file_name, is_base64
|
|
||||||
end
|
|
||||||
|
|
||||||
--! @todo move to library
|
|
||||||
local function play_file(dbh, domain_name, domain_uuid, file_name)
|
|
||||||
local full_path, is_base64 = find_file(dbh, domain_name, domain_uuid, file_name)
|
|
||||||
if not full_path then
|
|
||||||
log.warningf('Can not find audio file: %s. Try using it in raw mode.', file_name)
|
|
||||||
full_path = file_name
|
|
||||||
else
|
|
||||||
log.noticef('Found `%s` as `%s`%s', file_name, full_path, is_base64 and '(BASE64)' or '')
|
|
||||||
end
|
|
||||||
|
|
||||||
session:execute("playback", full_path);
|
|
||||||
end
|
|
||||||
|
|
||||||
if (session:ready()) then
|
|
||||||
--get the variables
|
|
||||||
local domain_name = session:getVariable("domain_name");
|
local domain_name = session:getVariable("domain_name");
|
||||||
local domain_uuid = session:getVariable("domain_uuid");
|
local domain_uuid = session:getVariable("domain_uuid");
|
||||||
local call_flow_uuid = session:getVariable("call_flow_uuid");
|
local call_flow_uuid = session:getVariable("call_flow_uuid");
|
||||||
local sounds_dir = session:getVariable("sounds_dir");
|
local sounds_dir = session:getVariable("sounds_dir");
|
||||||
local feature_code = session:getVariable("feature_code");
|
local feature_code = session:getVariable("feature_code");
|
||||||
|
end
|
||||||
|
|
||||||
--set the sounds path for the language, dialect and voice
|
--set the sounds path for the language, dialect and voice
|
||||||
|
if (session:ready()) then
|
||||||
local default_language = session:getVariable("default_language") or 'en';
|
local default_language = session:getVariable("default_language") or 'en';
|
||||||
local default_dialect = session:getVariable("default_dialect") or 'us';
|
local default_dialect = session:getVariable("default_dialect") or 'us';
|
||||||
local default_voice = session:getVariable("default_voice") or 'callie';
|
local default_voice = session:getVariable("default_voice") or 'callie';
|
||||||
|
end
|
||||||
|
|
||||||
--get the extension list
|
--get the call flow details
|
||||||
local sql = "SELECT * FROM v_call_flows where call_flow_uuid = :call_flow_uuid"
|
local sql = "SELECT * FROM v_call_flows where call_flow_uuid = :call_flow_uuid"
|
||||||
-- .. "and call_flow_enabled = 'true'"
|
-- .. "and call_flow_enabled = 'true'"
|
||||||
local params = {call_flow_uuid = call_flow_uuid};
|
local params = {call_flow_uuid = call_flow_uuid};
|
||||||
--log.notice("SQL: %s", sql);
|
--log.notice("SQL: %s", sql);
|
||||||
|
dbh:query(sql, params, function(row)
|
||||||
|
call_flow_name = row.call_flow_name;
|
||||||
|
call_flow_extension = row.call_flow_extension;
|
||||||
|
call_flow_feature_code = row.call_flow_feature_code;
|
||||||
|
--call_flow_context = row.call_flow_context;
|
||||||
|
call_flow_status = row.call_flow_status;
|
||||||
|
pin_number = row.call_flow_pin_number;
|
||||||
|
call_flow_label = row.call_flow_label;
|
||||||
|
call_flow_alternate_label = row.call_flow_alternate_label;
|
||||||
|
call_flow_sound = row.call_flow_sound or '';
|
||||||
|
call_flow_alternate_sound = row.call_flow_alternate_sound or '';
|
||||||
|
|
||||||
dbh:query(sql, params, function(row)
|
if #call_flow_status == 0 then
|
||||||
call_flow_name = row.call_flow_name;
|
call_flow_status = "true";
|
||||||
call_flow_extension = row.call_flow_extension;
|
end
|
||||||
call_flow_feature_code = row.call_flow_feature_code;
|
if call_flow_status == "true" then
|
||||||
--call_flow_context = row.call_flow_context;
|
app = row.call_flow_app;
|
||||||
call_flow_status = row.call_flow_status;
|
data = row.call_flow_data
|
||||||
pin_number = row.call_flow_pin_number;
|
else
|
||||||
call_flow_label = row.call_flow_label;
|
app = row.call_flow_alternate_app;
|
||||||
call_flow_alternate_label = row.call_flow_alternate_label;
|
data = row.call_flow_alternate_data
|
||||||
call_flow_sound = row.call_flow_sound or '';
|
end
|
||||||
call_flow_alternate_sound = row.call_flow_alternate_sound or '';
|
end);
|
||||||
|
|
||||||
if #call_flow_status == 0 then
|
|
||||||
call_flow_status = "true";
|
|
||||||
end
|
|
||||||
if call_flow_status == "true" then
|
|
||||||
app = row.call_flow_app;
|
|
||||||
data = row.call_flow_data
|
|
||||||
else
|
|
||||||
app = row.call_flow_alternate_app;
|
|
||||||
data = row.call_flow_alternate_data
|
|
||||||
end
|
|
||||||
end);
|
|
||||||
|
|
||||||
|
--if feature code toggle the status or send to the destination
|
||||||
if (feature_code == "true") then
|
if (feature_code == "true") then
|
||||||
--if the pin number is provided then require it
|
--if the pin number is provided then require it
|
||||||
if #pin_number > 0 then
|
if (session:ready()) then
|
||||||
local min_digits = #pin_number;
|
if #pin_number > 0 then
|
||||||
local max_digits = #pin_number+1;
|
local min_digits = #pin_number;
|
||||||
session:answer();
|
local max_digits = #pin_number+1;
|
||||||
local digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_pass:#", "", "\\d+");
|
session:answer();
|
||||||
if digits ~= pin_number then
|
local digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_pass:#", "", "\\d+");
|
||||||
session:streamFile("phrase:voicemail_fail_auth:#");
|
if digits ~= pin_number then
|
||||||
session:hangup("NORMAL_CLEARING");
|
session:streamFile("phrase:voicemail_fail_auth:#");
|
||||||
return;
|
session:hangup("NORMAL_CLEARING");
|
||||||
|
return;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -229,33 +142,44 @@ if (session:ready()) then
|
|||||||
});
|
});
|
||||||
|
|
||||||
--answer
|
--answer
|
||||||
session:answer();
|
if (session:ready()) then
|
||||||
|
session:answer();
|
||||||
--display label on Phone (if support)
|
|
||||||
if #active_flow_label > 0 then
|
|
||||||
local api = freeswitch.API();
|
|
||||||
local reply = api:executeString("uuid_display "..session:get_uuid().." "..active_flow_label);
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if #audio_file > 0 then
|
--display label on Phone (if support)
|
||||||
session:sleep(1000);
|
if (session:ready()) then
|
||||||
play_file(dbh, domain_name, domain_uuid, audio_file)
|
if #active_flow_label > 0 then
|
||||||
session:sleep(1000);
|
local api = freeswitch.API();
|
||||||
else
|
local reply = api:executeString("uuid_display "..session:get_uuid().." "..active_flow_label);
|
||||||
session:sleep(2000);
|
end
|
||||||
audio_file = "tone_stream://%(200,0,500,600,700)"
|
end
|
||||||
|
|
||||||
|
--play the audio fil or tone
|
||||||
|
if (session:ready()) then
|
||||||
|
if #audio_file > 0 then
|
||||||
|
session:sleep(1000);
|
||||||
|
play_file(dbh, domain_name, domain_uuid, audio_file)
|
||||||
|
session:sleep(1000);
|
||||||
|
else
|
||||||
|
session:sleep(2000);
|
||||||
|
audio_file = "tone_stream://%(200,0,500,600,700)"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--hangup the call
|
--hangup the call
|
||||||
session:hangup();
|
if (session:ready()) then
|
||||||
|
session:hangup();
|
||||||
|
end
|
||||||
else
|
else
|
||||||
log.notice("execute " .. app .. " " .. data);
|
--send to the log
|
||||||
|
log.notice("execute " .. app .. " " .. data);
|
||||||
|
|
||||||
--exucute the application
|
--execute the application
|
||||||
session:execute(app, data);
|
if (session:ready()) then
|
||||||
|
session:execute(app, data);
|
||||||
|
end
|
||||||
--timeout application
|
--timeout application
|
||||||
--if (not session:answered()) then
|
--if (not session:answered()) then
|
||||||
-- session:execute(ring_group_timeout_app, ring_group_timeout_data);
|
-- session:execute(ring_group_timeout_app, ring_group_timeout_data);
|
||||||
--end
|
--end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|||||||
Reference in New Issue
Block a user