IVR: Better support for the use of base64 Recordings, added option to use Phrases in a couple places, fixed the Exit Sound. (Note: A selected Sound with the same file name as a Recording will get trumped.)

This commit is contained in:
Nate Jones
2015-04-24 02:47:10 +00:00
parent 7cf0150765
commit 998b6e6197
5 changed files with 450 additions and 138 deletions

View File

@@ -182,85 +182,167 @@
if (string.sub(ivr_menu_greet_short,0,71) == "$${sounds_dir}/${default_language}/${default_dialect}/${default_voice}/") then
ivr_menu_greet_short = string.sub(ivr_menu_greet_short,72);
end
if (string.sub(ivr_menu_invalid_sound,0,71) == "$${sounds_dir}/${default_language}/${default_dialect}/${default_voice}/") then
ivr_menu_invalid_sound = string.sub(ivr_menu_invalid_sound,72);
end
if (string.sub(ivr_menu_exit_sound,0,71) == "$${sounds_dir}/${default_language}/${default_dialect}/${default_voice}/") then
ivr_menu_exit_sound = string.sub(ivr_menu_exit_sound,72);
end
--adjust the file path
if (ivr_menu_greet_short) then
--do nothing
else
ivr_menu_greet_short = ivr_menu_greet_long;
end
if (not file_exists(ivr_menu_greet_long)) then
if (file_exists(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..ivr_menu_greet_long)) then
ivr_menu_greet_long = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..ivr_menu_greet_long;
elseif (file_exists(recordings_dir.."/"..ivr_menu_greet_long)) then
ivr_menu_greet_long = recordings_dir.."/"..ivr_menu_greet_long;
end
end
if (string.len(ivr_menu_greet_short) > 1) then
if (not file_exists(ivr_menu_greet_short)) then
if (file_exists(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..ivr_menu_greet_short)) then
ivr_menu_greet_short = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..ivr_menu_greet_short;
elseif (file_exists(recordings_dir.."/"..ivr_menu_greet_short)) then
ivr_menu_greet_short = recordings_dir.."/"..ivr_menu_greet_short;
end
end
else
ivr_menu_greet_short = ivr_menu_greet_long;
end
ivr_menu_invalid_entry = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..ivr_menu_invalid_sound;
--parse file names
greet_long_file_name_only = ivr_menu_greet_long:match("([^/]+)$");
greet_short_file_name_only = ivr_menu_greet_short:match("([^/]+)$");
invalid_sound_file_name_only = ivr_menu_invalid_sound:match("([^/]+)$");
exit_sound_file_name_only = ivr_menu_exit_sound:match("([^/]+)$");
--get the recordings from the database
ivr_menu_greet_long_is_base64 = false;
ivr_menu_greet_short_is_base64 = false;
ivr_menu_invalid_sound_is_base64 = false;
ivr_menu_exit_sound_is_base64 = false;
if (storage_type == "base64") then
if (string.len(ivr_menu_greet_long) > 1) then
sql = [[SELECT * FROM v_recordings
WHERE domain_uuid = ']] .. domain_uuid ..[['
AND recording_filename = ']].. ivr_menu_greet_long.. [[' ]];
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[ivr_menu] SQL: " .. sql .. "\n");
--greet long
if (string.len(ivr_menu_greet_long) > 1) then
if (not file_exists(recordings_dir.."/"..greet_long_file_name_only)) then
sql = [[SELECT * FROM v_recordings
WHERE domain_uuid = ']]..domain_uuid..[['
AND recording_filename = ']]..greet_long_file_name_only..[[' ]];
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[ivr_menu] SQL: "..sql.."\n");
end
status = dbh:query(sql, function(row)
--add functions
dofile(scripts_dir.."/resources/functions/base64.lua");
--add the path to filename
ivr_menu_greet_long = recordings_dir.."/"..greet_long_file_name_only;
ivr_menu_greet_long_is_base64 = true;
--save the recording to the file system
if (string.len(row["recording_base64"]) > 32) then
local file = io.open(ivr_menu_greet_long, "w");
file:write(base64.decode(row["recording_base64"]));
file:close();
end
end);
end
end
status = dbh:query(sql, function(row)
--add functions
dofile(scripts_dir.."/resources/functions/base64.lua");
--add the path to filename
ivr_menu_greet_long = recordings_dir.."/"..ivr_menu_greet_long;
--save the recording to the file system
if (string.len(row["recording_base64"]) > 32) then
local file = io.open(ivr_menu_greet_long, "w");
file:write(base64.decode(row["recording_base64"]));
file:close();
--greet short
if (string.len(ivr_menu_greet_short) > 1) then
if (not file_exists(recordings_dir.."/"..greet_short_file_name_only)) then
sql = [[SELECT * FROM v_recordings
WHERE domain_uuid = ']]..domain_uuid..[['
AND recording_filename = ']]..greet_short_file_name_only..[[' ]];
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[ivr_menu] SQL: "..sql.."\n");
end
end);
end
if (string.len(ivr_menu_greet_short) > 1) then
sql = [[SELECT * FROM v_recordings
WHERE domain_uuid = ']] .. domain_uuid ..[['
AND recording_filename = ']].. ivr_menu_greet_short.. [[' ]];
--if (debug["sql"]) then
freeswitch.consoleLog("notice", "[ivr_menu] SQL: " .. sql .. "\n");
--end
status = dbh:query(sql, function(row)
--add functions
dofile(scripts_dir.."/resources/functions/base64.lua");
--add the path to filename
ivr_menu_greet_short = recordings_dir.."/"..ivr_menu_greet_short;
--save the recording to the file system
if (string.len(row["recording_base64"]) > 32) then
local file = io.open(ivr_menu_greet_short, "w");
file:write(base64.decode(row["recording_base64"]));
file:close();
status = dbh:query(sql, function(row)
--add functions
dofile(scripts_dir.."/resources/functions/base64.lua");
--add the path to filename
ivr_menu_greet_short = recordings_dir.."/"..greet_short_file_name_only;
ivr_menu_greet_short_is_base64 = true;
--save the recording to the file system
if (string.len(row["recording_base64"]) > 32) then
local file = io.open(ivr_menu_greet_short, "w");
file:write(base64.decode(row["recording_base64"]));
file:close();
end
end);
end
end
--invalid sound
if (string.len(ivr_menu_invalid_sound) > 1) then
if (not file_exists(recordings_dir.."/"..invalid_sound_file_name_only)) then
sql = [[SELECT * FROM v_recordings
WHERE domain_uuid = ']]..domain_uuid..[['
AND recording_filename = ']]..invalid_sound_file_name_only..[[' ]];
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[ivr_menu] SQL: "..sql.."\n");
end
end);
end
status = dbh:query(sql, function(row)
--add functions
dofile(scripts_dir.."/resources/functions/base64.lua");
--add the path to filename
ivr_menu_invalid_sound = recordings_dir.."/"..invalid_sound_file_name_only;
ivr_menu_invalid_sound_is_base64 = true;
--save the recording to the file system
if (string.len(row["recording_base64"]) > 32) then
local file = io.open(ivr_menu_invalid_sound, "w");
file:write(base64.decode(row["recording_base64"]));
file:close();
end
end);
end
end
--exit sound
if (string.len(ivr_menu_exit_sound) > 1) then
if (not file_exists(recordings_dir.."/"..exit_sound_file_name_only)) then
sql = [[SELECT * FROM v_recordings
WHERE domain_uuid = ']]..domain_uuid..[['
AND recording_filename = ']]..exit_sound_file_name_only..[[' ]];
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[ivr_menu] SQL: "..sql.."\n");
end
status = dbh:query(sql, function(row)
--add functions
dofile(scripts_dir.."/resources/functions/base64.lua");
--add the path to filename
ivr_menu_exit_sound = recordings_dir.."/"..exit_sound_file_name_only;
ivr_menu_exit_sound_is_base64 = true;
--save the recording to the file system
if (string.len(row["recording_base64"]) > 32) then
local file = io.open(ivr_menu_exit_sound, "w");
file:write(base64.decode(row["recording_base64"]));
file:close();
end
end);
end
end
elseif (storage_type == "http_cache") then
--add the path to file name
ivr_menu_greet_long = storage_path.."/"..ivr_menu_greet_long;
ivr_menu_greet_short = storage_path.."/"..ivr_menu_greet_short;
ivr_menu_invalid_sound = storage_path.."/"..ivr_menu_invalid_sound;
ivr_menu_exit_sound = storage_path.."/"..ivr_menu_exit_sound;
end
--adjust file paths
--greet long
if (not file_exists(ivr_menu_greet_long)) then
if (file_exists(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..greet_long_file_name_only)) then
ivr_menu_greet_long = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..greet_long_file_name_only;
elseif (file_exists(recordings_dir.."/"..greet_long_file_name_only)) then
ivr_menu_greet_long = recordings_dir.."/"..greet_long_file_name_only;
end
end
--greet short
if (string.len(ivr_menu_greet_short) > 1) then
if (not file_exists(ivr_menu_greet_short)) then
if (file_exists(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..greet_short_file_name_only)) then
ivr_menu_greet_short = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..greet_short_file_name_only;
elseif (file_exists(recordings_dir.."/"..greet_short_file_name_only)) then
ivr_menu_greet_short = recordings_dir.."/"..greet_short_file_name_only;
end
end
else
ivr_menu_greet_short = ivr_menu_greet_long;
end
--invalid sound
if (not file_exists(ivr_menu_invalid_sound)) then
if (file_exists(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..invalid_sound_file_name_only)) then
ivr_menu_invalid_sound = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..invalid_sound_file_name_only;
elseif (file_exists(recordings_dir.."/"..invalid_sound_file_name_only)) then
ivr_menu_invalid_sound = recordings_dir.."/"..invalid_sound_file_name_only;
end
end
--exit sound
if (not file_exists(ivr_menu_exit_sound)) then
if (file_exists(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..exit_sound_file_name_only)) then
ivr_menu_exit_sound = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..exit_sound_file_name_only;
elseif (file_exists(recordings_dir.."/"..exit_sound_file_name_only)) then
ivr_menu_exit_sound = recordings_dir.."/"..exit_sound_file_name_only;
end
end
--define the ivr menu
function menu()
--increment the tries
@@ -344,6 +426,9 @@
freeswitch.consoleLog("notice", "[ivr_menu] action: " .. action .. " data: ".. data .. "\n");
end
--run the action
if (ivr_menu_exit_sound ~= nil) then
session:streamFile(ivr_menu_exit_sound);
end
session:execute(action, data);
end
end
@@ -375,17 +460,30 @@
--execute
if (action) then
if (string.len(action) == 0) then
session:streamFile(ivr_menu_invalid_entry);
session:streamFile(ivr_menu_invalid_sound);
menu();
end
else
session:streamFile(ivr_menu_invalid_entry);
session:streamFile(ivr_menu_invalid_sound);
menu();
end
end --end function
--answer the session
if ( session:ready() ) then
if (session:ready()) then
session:answer();
menu();
end
--if base64, remove temporary audio files (increases responsiveness when files remain local)
if (storage_type == "base64") then
if (ivr_menu_greet_long_is_base64 and file_exists(ivr_menu_greet_long)) then
--os.remove(ivr_menu_greet_long);
end
if (ivr_menu_greet_short_is_base64 and file_exists(ivr_menu_greet_short)) then
--os.remove(ivr_menu_greet_short);
end
if (ivr_menu_invalid_sound_is_base64 and file_exists(ivr_menu_invalid_sound)) then
--os.remove(ivr_menu_invalid_sound);
end
end

View File

@@ -2,6 +2,68 @@
script_name = argv[0];
file_name = argv[1];
--include config.lua
scripts_dir = string.sub(debug.getinfo(1).source,2,string.len(debug.getinfo(1).source)-(string.len(argv[0])+1));
dofile(scripts_dir.."/resources/functions/config.lua");
dofile(config());
--connect to the database
dofile(scripts_dir.."/resources/functions/database_handle.lua");
dbh = database_handle('system');
--get the variables
domain_name = session:getVariable("domain_name");
domain_uuid = session:getVariable("domain_uuid");
--get the sounds dir, language, dialect and voice
sounds_dir = session:getVariable("sounds_dir");
default_language = session:getVariable("default_language");
default_dialect = session:getVariable("default_dialect");
default_voice = session:getVariable("default_voice");
if (not default_language) then default_language = 'en'; end
if (not default_dialect) then default_dialect = 'us'; end
if (not default_voice) then default_voice = 'callie'; end
--settings
dofile(scripts_dir.."/resources/functions/settings.lua");
settings = settings(domain_uuid);
storage_type = "";
storage_path = "";
if (settings['recordings'] ~= nil) then
if (settings['recordings']['storage_type'] ~= nil) then
if (settings['recordings']['storage_type']['text'] ~= nil) then
storage_type = settings['recordings']['storage_type']['text'];
end
end
if (settings['recordings']['storage_path'] ~= nil) then
if (settings['recordings']['storage_path']['text'] ~= nil) then
storage_path = settings['recordings']['storage_path']['text'];
storage_path = storage_path:gsub("${domain_name}", domain_name);
storage_path = storage_path:gsub("${voicemail_id}", voicemail_id);
storage_path = storage_path:gsub("${voicemail_dir}", voicemail_dir);
end
end
end
temp_dir = "";
if (settings['server'] ~= nil) then
if (settings['server']['temp'] ~= nil) then
if (settings['server']['temp']['dir'] ~= nil) then
temp_dir = settings['server']['temp']['dir'];
end
end
end
--set the recordings directory
if (domain_count > 1) then
recordings_dir = recordings_dir .. "/"..domain_name;
end
--check if a file exists
function file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
--define the on_dtmf call back function
function on_dtmf(s, type, obj, arg)
if (type == "dtmf") then
@@ -28,10 +90,59 @@
end
end
--stream the file
session:answer();
--parse file name
file_name_only = file_name:match("([^/]+)$");
--if base64, get from db, create temp file
if (storage_type == "base64") then
freeswitch.consoleLog("notice", "detected base64.\n");
if (not file_exists(recordings_dir.."/"..file_name_only)) then
sql = [[SELECT * FROM v_recordings
WHERE domain_uuid = ']] .. domain_uuid ..[['
AND recording_filename = ']].. file_name_only.. [[' ]];
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[ivr_menu] SQL: " .. sql .. "\n");
end
status = dbh:query(sql, function(row)
--add functions
dofile(scripts_dir.."/resources/functions/base64.lua");
--add the path to filename
file_name = recordings_dir.."/"..file_name_only;
--save the recording to the file system
if (string.len(row["recording_base64"]) > 32) then
local file = io.open(file_name, "w");
file:write(base64.decode(row["recording_base64"]));
file:close();
end
end);
else
file_name = recordings_dir.."/"..file_name_only;
end
end
--adjust file path
if (not file_exists(file_name)) then
freeswitch.consoleLog("notice", "file " .. file_name .. " doesn't exist.\n");
if (file_exists(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..file_name_only)) then
freeswitch.consoleLog("notice", "file " .. file_name_only .. " found in sounds.\n");
file_name = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..file_name_only;
elseif (file_exists(recordings_dir.."/"..file_name_only)) then
freeswitch.consoleLog("notice", "file " .. file_name_only .. " found in recordings.\n");
file_name = recordings_dir.."/"..file_name_only;
end
end
--stream file if exists
if (session:ready()) then
session:answer();
session:sleep(1000);
session:setInputCallback("on_dtmf", "");
session:streamFile(file_name);
end
--if base64, remove temp file (increases responsiveness when files remain local)
if (storage_type == "base64") then
if (file_exists(file_name)) then
--os.remove(file_name);
end
end

View File

@@ -1000,16 +1000,20 @@ function switch_select_destination($select_type, $select_label, $select_name, $s
if (count($result) > 0) {
$options[] = "<optgroup label='Recordings'>";
foreach ($result as &$row) {
$name = $row["recording_name"];
$filename = $row["recording_filename"];
$recording_name = $row["recording_name"];
$recording_filename = $row["recording_filename"];
$path_mod = ($_SESSION['recordings']['storage_type']['text'] != 'base64') ? $_SESSION['switch']['recordings']['dir'] : null;
if ($select_type == "dialplan") {
$selected = ($select_value == "lua:streamfile.lua ".$_SESSION['switch']['recordings']['dir']."/".$filename || $select_value == "lua:streamfile.lua ".$filename) ? true : false;
$options[] = "<option value='lua:streamfile.lua ".$_SESSION['switch']['recordings']['dir']."/".$filename."' ".(($selected) ? "selected='selected'" : null).">".$name."</option>";
$execute_method = 'lua:';
}
if ($select_type == "ivr") {
$selected = ($select_value == "menu-exec-app:lua streamfile.lua ".$_SESSION['switch']['recordings']['dir']."/".$filename || $select_value == "menu-exec-app:lua streamfile.lua ".$_SESSION['switch']['recordings']['dir']."/".$filename) ? true : false;
$options[] = "<option value='menu-exec-app:lua streamfile.lua ".$_SESSION['switch']['recordings']['dir']."/".$filename."' ".(($selected) ? "selected='selected'" : null).">".$name."</option>";
else if ($select_type == "ivr") {
$execute_method = 'menu-exec-app:lua ';
}
$selected = (
$select_value == $execute_method."streamfile.lua ".$_SESSION['switch']['recordings']['dir']."/".$recording_filename ||
$select_value == $execute_method."streamfile.lua ".$recording_filename
) ? true : false;
$options[] = "<option value='".$execute_method."streamfile.lua ".$path_mod.$recording_filename."' ".(($selected) ? "selected='selected'" : null).">".$recording_name."</option>";
if ($selected) { $selection_found = true; }
}
$options[] = "</optgroup>";
@@ -1017,6 +1021,26 @@ function switch_select_destination($select_type, $select_label, $select_name, $s
}
}
//phrases
if (file_exists($_SERVER['DOCUMENT_ROOT'].PROJECT_PATH."/app/phrases/app_config.php")) {
if ($select_type == "dialplan" || $select_type == "ivr") {
$sql = "select * from v_phrases where domain_uuid = '".$domain_uuid."' ";
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
if (count($result) > 0) {
$options[] = "<optgroup label='Phrases'>";
foreach ($result as &$row) {
$selected = ($select_value == "phrase:".$row["phrase_name"].".".$domain_uuid) ? true : false;
$options[] = "<option value='phrase:".$row["phrase_name"].".".$domain_uuid."' ".(($selected) ? "selected='selected'" : null).">".$row["phrase_name"]."</option>";
if ($selected) { $selection_found = true; }
}
$options[] = "</optgroup>";
}
unset ($prep_statement);
}
}
//ring groups
if (file_exists($_SERVER['DOCUMENT_ROOT'].PROJECT_PATH."/app/ring_groups/app_config.php")) {
if ($select_type == "dialplan" || $select_type == "ivr") {