2012-11-24 19:22:25 +01:00
|
|
|
-- ivr_menu.lua
|
|
|
|
|
-- Part of FusionPBX
|
2015-08-14 19:26:11 +02:00
|
|
|
-- Copyright (C) 2012-2015 Mark J Crane <markjcrane@fusionpbx.com>
|
2012-11-24 19:22:25 +01:00
|
|
|
-- All rights reserved.
|
|
|
|
|
--
|
|
|
|
|
-- Redistribution and use in source and binary forms, with or without
|
|
|
|
|
-- modification, are permitted provided that the following conditions are met:
|
|
|
|
|
--
|
|
|
|
|
-- 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
-- this list of conditions and the following disclaimer.
|
|
|
|
|
--
|
|
|
|
|
-- 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
-- notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
-- documentation and/or other materials provided with the distribution.
|
|
|
|
|
--
|
2016-01-26 00:53:09 +01:00
|
|
|
-- THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
2012-11-24 19:22:25 +01:00
|
|
|
-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
|
-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
|
-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
|
-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
-- POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
2012-11-30 11:44:58 +01:00
|
|
|
--set the debug options
|
|
|
|
|
debug["action"] = false;
|
|
|
|
|
debug["sql"] = false;
|
|
|
|
|
debug["regex"] = false;
|
|
|
|
|
debug["dtmf"] = false;
|
|
|
|
|
debug["tries"] = false;
|
|
|
|
|
|
2013-03-31 10:18:41 +02:00
|
|
|
--include config.lua
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.config";
|
2012-11-24 19:22:25 +01:00
|
|
|
|
2016-09-04 21:05:47 +02:00
|
|
|
--include Database class
|
|
|
|
|
local Database = require "resources.functions.database";
|
2012-11-24 19:22:25 +01:00
|
|
|
|
2016-02-04 14:05:35 +01:00
|
|
|
--get logger
|
|
|
|
|
local log = require "resources.functions.log".ivr_menu
|
|
|
|
|
|
2016-01-28 16:47:31 +01:00
|
|
|
--include functions
|
|
|
|
|
require "resources.functions.format_ringback"
|
2016-02-03 14:53:22 +01:00
|
|
|
require "resources.functions.split"
|
2016-01-28 16:47:31 +01:00
|
|
|
|
2012-11-24 19:22:25 +01:00
|
|
|
--get the variables
|
|
|
|
|
domain_name = session:getVariable("domain_name");
|
2013-01-27 08:28:19 +01:00
|
|
|
context = session:getVariable("context");
|
2012-11-24 19:22:25 +01:00
|
|
|
ivr_menu_uuid = session:getVariable("ivr_menu_uuid");
|
|
|
|
|
caller_id_name = session:getVariable("caller_id_name");
|
|
|
|
|
caller_id_number = session:getVariable("caller_id_number");
|
2015-03-31 02:50:13 +02:00
|
|
|
domain_uuid = session:getVariable("domain_uuid");
|
|
|
|
|
|
2016-02-04 14:05:35 +01:00
|
|
|
local recordings_dir = recordings_dir .. "/" .. domain_name
|
|
|
|
|
|
2016-09-04 21:05:47 +02:00
|
|
|
--connect to the database
|
|
|
|
|
dbh = Database.new('system');
|
|
|
|
|
|
2015-03-31 02:50:13 +02:00
|
|
|
--settings
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.settings";
|
2015-03-31 02:50:13 +02:00
|
|
|
settings = settings(domain_uuid);
|
2015-04-01 08:52:45 +02:00
|
|
|
storage_type = "";
|
|
|
|
|
storage_path = "";
|
2015-04-06 16:14:32 +02:00
|
|
|
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'];
|
2015-04-04 11:09:09 +02:00
|
|
|
end
|
|
|
|
|
end
|
2015-04-06 16:14:32 +02:00
|
|
|
if (settings['recordings']['storage_path'] ~= nil) then
|
|
|
|
|
if (settings['recordings']['storage_path']['text'] ~= nil) then
|
|
|
|
|
storage_path = settings['recordings']['storage_path']['text'];
|
2015-04-06 16:06:11 +02:00
|
|
|
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);
|
2015-04-04 11:09:09 +02:00
|
|
|
end
|
|
|
|
|
end
|
2015-03-31 02:50:13 +02:00
|
|
|
end
|
2015-08-07 10:11:27 +02:00
|
|
|
if (not temp_dir) or (#temp_dir == 0) then
|
|
|
|
|
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
|
2015-04-04 11:09:09 +02:00
|
|
|
end
|
|
|
|
|
end
|
2015-03-31 02:50:13 +02:00
|
|
|
end
|
|
|
|
|
|
2015-08-11 04:06:33 +02:00
|
|
|
--define the trim function
|
|
|
|
|
require "resources.functions.trim"
|
2012-11-30 11:44:58 +01:00
|
|
|
|
2012-11-25 08:13:14 +01:00
|
|
|
--check if a file exists
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.file_exists"
|
2012-11-25 08:13:14 +01:00
|
|
|
|
2015-03-19 00:31:14 +01:00
|
|
|
--prepare the api object
|
|
|
|
|
api = freeswitch.API();
|
|
|
|
|
|
2012-11-25 08:13:14 +01:00
|
|
|
--get the ivr menu from the database
|
2016-03-11 14:21:52 +01:00
|
|
|
sql = [[SELECT * FROM v_ivr_menus
|
2012-11-24 19:22:25 +01:00
|
|
|
WHERE ivr_menu_uuid = ']] .. ivr_menu_uuid ..[['
|
|
|
|
|
AND ivr_menu_enabled = 'true' ]];
|
|
|
|
|
if (debug["sql"]) then
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("SQL: " .. sql);
|
2012-11-24 19:22:25 +01:00
|
|
|
end
|
2016-02-04 14:05:35 +01:00
|
|
|
dbh:query(sql, function(row)
|
2015-03-31 02:50:13 +02:00
|
|
|
domain_uuid = row["domain_uuid"];
|
2012-11-24 19:22:25 +01:00
|
|
|
ivr_menu_name = row["ivr_menu_name"];
|
|
|
|
|
--ivr_menu_extension = row["ivr_menu_extension"];
|
|
|
|
|
ivr_menu_greet_long = row["ivr_menu_greet_long"];
|
|
|
|
|
ivr_menu_greet_short = row["ivr_menu_greet_short"];
|
|
|
|
|
ivr_menu_invalid_sound = row["ivr_menu_invalid_sound"];
|
|
|
|
|
ivr_menu_exit_sound = row["ivr_menu_exit_sound"];
|
|
|
|
|
ivr_menu_confirm_macro = row["ivr_menu_confirm_macro"];
|
|
|
|
|
ivr_menu_confirm_key = row["ivr_menu_confirm_key"];
|
|
|
|
|
ivr_menu_tts_engine = row["ivr_menu_tts_engine"];
|
|
|
|
|
ivr_menu_tts_voice = row["ivr_menu_tts_voice"];
|
|
|
|
|
ivr_menu_confirm_attempts = row["ivr_menu_confirm_attempts"];
|
|
|
|
|
ivr_menu_timeout = row["ivr_menu_timeout"];
|
|
|
|
|
--ivr_menu_exit_app = row["ivr_menu_exit_app"];
|
|
|
|
|
--ivr_menu_exit_data = row["ivr_menu_exit_data"];
|
|
|
|
|
ivr_menu_inter_digit_timeout = row["ivr_menu_inter_digit_timeout"];
|
|
|
|
|
ivr_menu_max_failures = row["ivr_menu_max_failures"];
|
|
|
|
|
ivr_menu_max_timeouts = row["ivr_menu_max_timeouts"];
|
|
|
|
|
ivr_menu_digit_len = row["ivr_menu_digit_len"];
|
2013-01-27 08:28:19 +01:00
|
|
|
ivr_menu_direct_dial = row["ivr_menu_direct_dial"];
|
2012-11-24 19:22:25 +01:00
|
|
|
--ivr_menu_description = row["ivr_menu_description"];
|
|
|
|
|
ivr_menu_ringback = row["ivr_menu_ringback"];
|
2013-02-12 12:19:52 +01:00
|
|
|
ivr_menu_cid_prefix = row["ivr_menu_cid_prefix"];
|
2012-11-24 19:22:25 +01:00
|
|
|
end);
|
|
|
|
|
|
2016-09-04 21:05:47 +02:00
|
|
|
--disconnect from db
|
|
|
|
|
dbh:release()
|
|
|
|
|
|
2013-02-12 12:19:52 +01:00
|
|
|
--set the caller id name
|
2016-02-24 14:28:38 +01:00
|
|
|
if caller_id_name and #caller_id_name > 0 and ivr_menu_cid_prefix and #ivr_menu_cid_prefix > 0 then
|
2016-02-04 14:05:35 +01:00
|
|
|
caller_id_name = ivr_menu_cid_prefix .. "#" .. caller_id_name;
|
|
|
|
|
session:setVariable("caller_id_name", caller_id_name);
|
|
|
|
|
session:setVariable("effective_caller_id_name", caller_id_name);
|
2013-02-12 12:19:52 +01:00
|
|
|
end
|
2015-03-13 17:45:05 +01:00
|
|
|
|
2015-03-20 05:56:25 +01:00
|
|
|
--set ringback
|
2016-01-28 16:47:31 +01:00
|
|
|
ivr_menu_ringback = format_ringback(ivr_menu_ringback);
|
2015-03-20 05:56:25 +01:00
|
|
|
session:setVariable("ringback", ivr_menu_ringback);
|
|
|
|
|
session:setVariable("transfer_ringback", ivr_menu_ringback);
|
2013-02-12 12:19:52 +01:00
|
|
|
|
2012-11-25 08:13:14 +01:00
|
|
|
--get the sounds dir, language, dialect and voice
|
|
|
|
|
sounds_dir = session:getVariable("sounds_dir");
|
2016-02-03 14:53:22 +01:00
|
|
|
default_language = session:getVariable("default_language") or 'en';
|
|
|
|
|
default_dialect = session:getVariable("default_dialect") or 'us';
|
|
|
|
|
default_voice = session:getVariable("default_voice") or 'callie';
|
2012-11-25 08:13:14 +01:00
|
|
|
|
|
|
|
|
--make the path relative
|
2016-02-04 11:20:30 +01:00
|
|
|
local strip_pattern = "^$${sounds_dir}/${default_language}/${default_dialect}/${default_voice}/"
|
|
|
|
|
ivr_menu_greet_long = string.gsub(ivr_menu_greet_long, strip_pattern, "")
|
|
|
|
|
ivr_menu_greet_short = string.gsub(ivr_menu_greet_short, strip_pattern, "")
|
|
|
|
|
ivr_menu_invalid_sound = string.gsub(ivr_menu_invalid_sound, strip_pattern, "")
|
|
|
|
|
ivr_menu_exit_sound = string.gsub(ivr_menu_exit_sound, strip_pattern, "")
|
2015-04-24 04:47:10 +02:00
|
|
|
|
|
|
|
|
--parse file names
|
2016-02-03 14:53:22 +01:00
|
|
|
greet_long_file_name = ivr_menu_greet_long:match("([^/]+)$") or "";
|
|
|
|
|
greet_short_file_name = ivr_menu_greet_short:match("([^/]+)$") or "";
|
|
|
|
|
invalid_sound_file_name = ivr_menu_invalid_sound:match("([^/]+)$") or "";
|
|
|
|
|
exit_sound_file_name = ivr_menu_exit_sound:match("([^/]+)$") or "";
|
2012-11-25 08:13:14 +01:00
|
|
|
|
2015-03-31 02:50:13 +02:00
|
|
|
--get the recordings from the database
|
2015-04-24 04:47:10 +02:00
|
|
|
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;
|
2015-03-31 02:50:13 +02:00
|
|
|
if (storage_type == "base64") then
|
2016-02-04 11:20:30 +01:00
|
|
|
--add functions
|
|
|
|
|
require "resources.functions.mkdir";
|
|
|
|
|
|
2016-09-04 21:05:47 +02:00
|
|
|
--connect to the database
|
|
|
|
|
local dbh = Database.new('system', 'base64/read')
|
|
|
|
|
|
2016-02-04 11:20:30 +01:00
|
|
|
--make sure the recordings directory exists
|
2016-02-04 14:05:35 +01:00
|
|
|
mkdir(recordings_dir);
|
2016-02-04 11:20:30 +01:00
|
|
|
|
|
|
|
|
--define function to load file from db
|
2016-02-04 14:05:35 +01:00
|
|
|
local function load_file(file_name)
|
|
|
|
|
local full_path = recordings_dir .. "/" .. file_name
|
2016-02-04 11:20:30 +01:00
|
|
|
if file_exists(full_path) then
|
|
|
|
|
return full_path
|
2015-04-24 04:47:10 +02:00
|
|
|
end
|
2016-02-04 11:20:30 +01:00
|
|
|
|
|
|
|
|
local sql = [[SELECT * FROM v_recordings WHERE domain_uuid = ']]..domain_uuid..
|
|
|
|
|
[['AND recording_filename = ']]..file_name..[[' ]];
|
|
|
|
|
|
|
|
|
|
if (debug["sql"]) then
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("SQL: "..sql);
|
2016-02-04 11:20:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local is_base64
|
|
|
|
|
dbh:query(sql, function(row)
|
|
|
|
|
if #row.recording_base64 > 32 then
|
2016-09-04 21:05:47 +02:00
|
|
|
--include the file io
|
|
|
|
|
local file = require "resources.functions.file"
|
|
|
|
|
|
|
|
|
|
--write decoded string to file
|
|
|
|
|
local ok, err = file.write_base64(full_path, row.recording_base64);
|
|
|
|
|
if not ok then
|
|
|
|
|
log.err("can not create file: "..full_path.."; Error - " .. tostring(err));
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-04 11:20:30 +01:00
|
|
|
is_base64 = true;
|
|
|
|
|
end
|
|
|
|
|
end);
|
|
|
|
|
|
|
|
|
|
-- return path in any case
|
|
|
|
|
return full_path, is_base64
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--greet long
|
|
|
|
|
if #ivr_menu_greet_long > 1 then
|
2016-02-04 14:05:35 +01:00
|
|
|
ivr_menu_greet_long, ivr_menu_greet_long_is_base64 = load_file(greet_long_file_name)
|
2015-04-03 21:06:16 +02:00
|
|
|
end
|
2016-02-04 11:20:30 +01:00
|
|
|
|
2015-04-24 04:47:10 +02:00
|
|
|
--greet short
|
2016-02-04 11:20:30 +01:00
|
|
|
if #ivr_menu_greet_short > 1 then
|
2016-02-04 14:05:35 +01:00
|
|
|
ivr_menu_greet_short, ivr_menu_greet_short_is_base64 = load_file(greet_short_file_name)
|
2015-04-24 04:47:10 +02:00
|
|
|
end
|
2016-02-04 11:20:30 +01:00
|
|
|
|
2015-04-24 04:47:10 +02:00
|
|
|
--invalid sound
|
2016-02-04 11:20:30 +01:00
|
|
|
if #ivr_menu_invalid_sound > 1 then
|
2016-02-04 14:05:35 +01:00
|
|
|
ivr_menu_invalid_sound, ivr_menu_invalid_sound_is_base64 = load_file(invalid_sound_file_name)
|
2015-04-24 04:47:10 +02:00
|
|
|
end
|
2016-02-04 11:20:30 +01:00
|
|
|
|
2015-04-24 04:47:10 +02:00
|
|
|
--exit sound
|
2016-02-04 11:20:30 +01:00
|
|
|
if #ivr_menu_exit_sound > 1 then
|
2016-02-04 14:05:35 +01:00
|
|
|
ivr_menu_exit_sound, ivr_menu_exit_sound_is_base64 = load_file(exit_sound_file_name)
|
2015-04-24 04:47:10 +02:00
|
|
|
end
|
2016-02-04 11:20:30 +01:00
|
|
|
|
2016-09-04 21:05:47 +02:00
|
|
|
dbh:release()
|
|
|
|
|
|
2015-04-06 09:34:02 +02:00
|
|
|
elseif (storage_type == "http_cache") then
|
2015-04-06 16:06:11 +02:00
|
|
|
--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;
|
2015-04-24 04:47:10 +02:00
|
|
|
ivr_menu_invalid_sound = storage_path.."/"..ivr_menu_invalid_sound;
|
|
|
|
|
ivr_menu_exit_sound = storage_path.."/"..ivr_menu_exit_sound;
|
2015-03-31 02:50:13 +02:00
|
|
|
end
|
|
|
|
|
|
2015-04-24 04:47:10 +02:00
|
|
|
--adjust file paths
|
2016-02-04 11:20:30 +01:00
|
|
|
local function adjust_file_path(full_path, file_name)
|
|
|
|
|
return file_exists(full_path)
|
2016-02-04 14:05:35 +01:00
|
|
|
or file_exists(recordings_dir.."/"..file_name)
|
2016-02-04 11:20:30 +01:00
|
|
|
or file_exists(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..file_name)
|
|
|
|
|
or full_path
|
|
|
|
|
end
|
2015-04-24 04:47:10 +02:00
|
|
|
--greet long
|
2016-02-04 11:20:30 +01:00
|
|
|
ivr_menu_greet_long = adjust_file_path(ivr_menu_greet_long, greet_long_file_name)
|
2015-04-24 04:47:10 +02:00
|
|
|
--greet short
|
2016-02-03 14:53:22 +01:00
|
|
|
if #ivr_menu_greet_short > 1 then
|
2016-02-04 11:20:30 +01:00
|
|
|
ivr_menu_greet_short = adjust_file_path(ivr_menu_greet_short, greet_short_file_name)
|
2015-04-24 04:47:10 +02:00
|
|
|
else
|
2016-02-04 11:20:30 +01:00
|
|
|
ivr_menu_greet_short = ivr_menu_greet_long
|
2015-04-24 04:47:10 +02:00
|
|
|
end
|
|
|
|
|
--invalid sound
|
2016-02-04 11:20:30 +01:00
|
|
|
ivr_menu_invalid_sound = adjust_file_path(ivr_menu_invalid_sound, invalid_sound_file_name)
|
2015-04-24 04:47:10 +02:00
|
|
|
--exit sound
|
2016-02-04 11:20:30 +01:00
|
|
|
ivr_menu_exit_sound = adjust_file_path(ivr_menu_exit_sound, exit_sound_file_name)
|
2015-04-24 04:47:10 +02:00
|
|
|
|
2012-11-30 11:44:58 +01:00
|
|
|
--define the ivr menu
|
2016-02-04 11:20:30 +01:00
|
|
|
local menu_options, menu
|
|
|
|
|
local tries = 0;
|
2012-11-30 11:44:58 +01:00
|
|
|
function menu()
|
2016-02-04 17:42:42 +01:00
|
|
|
-- check number of failures
|
|
|
|
|
if (tries > 0) and (tries >= tonumber(ivr_menu_max_failures)) then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-03 14:53:22 +01:00
|
|
|
-- increment the tries
|
|
|
|
|
tries = tries + 1;
|
2016-02-04 17:42:42 +01:00
|
|
|
|
|
|
|
|
--log the dtmf digits
|
|
|
|
|
if (debug["tries"]) then
|
2016-02-04 18:00:12 +01:00
|
|
|
log.noticef("tries: %d/%d", tries, tonumber(ivr_menu_max_failures) or '-1');
|
2016-02-04 17:42:42 +01:00
|
|
|
end
|
|
|
|
|
|
2016-02-03 14:53:22 +01:00
|
|
|
-- set the minimum dtmf lengts
|
|
|
|
|
local min_digits = 1;
|
|
|
|
|
|
|
|
|
|
-- set sound file and number of attempts
|
|
|
|
|
local sound, sound_type, attempts
|
|
|
|
|
|
|
|
|
|
if tries == 1 then
|
|
|
|
|
sound, sound_type, attempts = ivr_menu_greet_long or "", "long", 1
|
|
|
|
|
else
|
|
|
|
|
sound, sound_type, attempts = ivr_menu_greet_short or "", "short", tonumber(ivr_menu_max_timeouts) or 3
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-03 19:58:49 +02:00
|
|
|
if (debug["tries"]) then
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("greet " .. sound_type .. ": " .. sound);
|
2015-04-03 19:58:49 +02:00
|
|
|
end
|
2016-02-03 14:53:22 +01:00
|
|
|
|
|
|
|
|
-- read dtmf
|
|
|
|
|
local dtmf_digits
|
|
|
|
|
if attempts > 0 then
|
|
|
|
|
dtmf_digits = session:playAndGetDigits(min_digits, ivr_menu_digit_len, attempts, ivr_menu_timeout, ivr_menu_confirm_key, sound, "", ".*");
|
|
|
|
|
-- need pause before stream file
|
2015-04-28 19:24:32 +02:00
|
|
|
session:setVariable("slept", "false");
|
2015-04-03 19:58:49 +02:00
|
|
|
end
|
2016-02-03 14:53:22 +01:00
|
|
|
|
|
|
|
|
-- proceed dtmf
|
|
|
|
|
if dtmf_digits and #dtmf_digits > 0 then
|
|
|
|
|
if (debug["tries"]) then
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("dtmf_digits: " .. dtmf_digits);
|
2016-02-03 14:53:22 +01:00
|
|
|
end
|
|
|
|
|
return menu_options(session, dtmf_digits);
|
2015-04-03 19:58:49 +02:00
|
|
|
end
|
2016-02-03 14:53:22 +01:00
|
|
|
|
2016-02-04 17:42:42 +01:00
|
|
|
return menu();
|
2012-11-24 19:22:25 +01:00
|
|
|
end
|
|
|
|
|
|
2013-02-12 12:19:52 +01:00
|
|
|
function menu_options(session, digits)
|
2012-11-30 11:44:58 +01:00
|
|
|
|
|
|
|
|
--log the dtmf digits
|
|
|
|
|
if (debug["dtmf"]) then
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("dtmf: " .. digits);
|
2012-11-30 11:44:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--get the ivr menu options
|
|
|
|
|
sql = [[SELECT * FROM v_ivr_menu_options WHERE ivr_menu_uuid = ']] .. ivr_menu_uuid ..[[' ORDER BY ivr_menu_option_order asc ]];
|
|
|
|
|
if (debug["sql"]) then
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("SQL: " .. sql);
|
2012-11-30 11:44:58 +01:00
|
|
|
end
|
2016-02-03 14:53:22 +01:00
|
|
|
|
2016-09-04 21:05:47 +02:00
|
|
|
--connect to the database
|
|
|
|
|
local dbh = Database.new('system')
|
|
|
|
|
|
|
|
|
|
--select actions to execute
|
2016-02-05 08:20:09 +01:00
|
|
|
local actions = {}
|
2016-02-03 14:53:22 +01:00
|
|
|
dbh:query(sql, function(row)
|
2016-02-05 08:20:09 +01:00
|
|
|
-- declare vars
|
|
|
|
|
local action, script, data
|
2016-02-03 14:53:22 +01:00
|
|
|
|
2012-11-30 11:44:58 +01:00
|
|
|
--check for matching options
|
2016-02-03 14:53:22 +01:00
|
|
|
if tonumber(row.ivr_menu_option_digits) then
|
2012-12-05 07:52:03 +01:00
|
|
|
row.ivr_menu_option_digits = "^"..row.ivr_menu_option_digits.."$";
|
|
|
|
|
end
|
2016-02-03 14:53:22 +01:00
|
|
|
|
|
|
|
|
if api:execute("regex", "m:~"..digits.."~"..row.ivr_menu_option_digits) ~= "true" then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if row.ivr_menu_option_action == "menu-exec-app" then
|
|
|
|
|
--get the action and data
|
|
|
|
|
action, data = split_first(row.ivr_menu_option_param, ' ', true)
|
|
|
|
|
data = data or ""
|
|
|
|
|
|
|
|
|
|
--check if the option uses a regex
|
|
|
|
|
local regex = string.find(row.ivr_menu_option_digits, "(", 0, true);
|
|
|
|
|
if regex then
|
|
|
|
|
--get the regex result
|
|
|
|
|
regex = "m:~"..digits.."~"..row.ivr_menu_option_digits.."~$1"
|
|
|
|
|
local result = trim(api:execute("regex", regex));
|
|
|
|
|
if (debug["regex"]) then
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("regex "..regex);
|
|
|
|
|
log.notice("result: "..result);
|
2015-04-28 19:24:32 +02:00
|
|
|
end
|
2016-02-03 14:53:22 +01:00
|
|
|
|
|
|
|
|
--replace the $1 and the domain name
|
|
|
|
|
data = data:gsub("$1", result);
|
|
|
|
|
data = data:gsub("${domain_name}", domain_name);
|
|
|
|
|
end --if regex
|
|
|
|
|
end --if menu-exex-app
|
|
|
|
|
|
|
|
|
|
if row.ivr_menu_option_action == "phrase" then
|
|
|
|
|
action = 'phrase';
|
|
|
|
|
data = row.ivr_menu_option_param;
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if action == "lua" then
|
|
|
|
|
script = split_first(data, " ", true)
|
2012-12-05 00:00:27 +01:00
|
|
|
end
|
|
|
|
|
|
2016-02-03 14:53:22 +01:00
|
|
|
-- break loop
|
|
|
|
|
if action and #action > 0 then
|
2016-02-04 17:05:24 +01:00
|
|
|
actions[#actions + 1] = {action, script, data}
|
|
|
|
|
return
|
2016-02-03 14:53:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- we have unsupported IVR action
|
2016-02-04 14:05:35 +01:00
|
|
|
log.warning("invalid action in ivr: " .. row.ivr_menu_option_action);
|
2012-11-30 11:44:58 +01:00
|
|
|
end); --end results
|
2016-09-04 21:05:47 +02:00
|
|
|
dbh:release()
|
2012-12-05 08:21:59 +01:00
|
|
|
|
2016-02-03 14:53:22 +01:00
|
|
|
--execute
|
2016-02-04 17:05:24 +01:00
|
|
|
if #actions > 0 then
|
|
|
|
|
for _, t in ipairs(actions) do
|
|
|
|
|
local action, script, data = t[1],t[2],t[3]
|
2016-02-03 14:53:22 +01:00
|
|
|
-- send to the log
|
|
|
|
|
if (debug["action"]) then
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("action: " .. action .. " data: ".. data);
|
2016-02-03 14:53:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- run the action (with return to menu)
|
|
|
|
|
if action == 'phrase' or script == 'streamfile.lua' then
|
|
|
|
|
session:execute(action, data);
|
2016-02-04 17:05:24 +01:00
|
|
|
else
|
|
|
|
|
if ivr_menu_exit_sound and #ivr_menu_exit_sound > 0 then
|
|
|
|
|
session:streamFile(ivr_menu_exit_sound);
|
|
|
|
|
end
|
|
|
|
|
-- run the action (without return to menu)
|
|
|
|
|
return session:execute(action, data);
|
2016-02-03 14:53:22 +01:00
|
|
|
end
|
2016-02-04 17:05:24 +01:00
|
|
|
end
|
|
|
|
|
return menu();
|
2016-02-03 14:53:22 +01:00
|
|
|
end
|
|
|
|
|
|
2013-07-15 23:47:22 +02:00
|
|
|
--direct dial
|
2016-02-03 14:53:22 +01:00
|
|
|
if ivr_menu_direct_dial == "true" and #digits > 0 and #digits < 6 then
|
|
|
|
|
-- remove *#
|
|
|
|
|
digits = digits:gsub("[*#]", "");
|
|
|
|
|
|
|
|
|
|
-- check to see if the user extension exists
|
|
|
|
|
local cmd = "user_exists id ".. digits .." "..domain_name;
|
|
|
|
|
local result = api:executeString(cmd);
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("[direct dial] "..cmd.." "..result);
|
2016-02-03 14:53:22 +01:00
|
|
|
if result == "true" then
|
|
|
|
|
--log the action
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("[direct dial] "..digits.." XML "..context);
|
2016-02-03 14:53:22 +01:00
|
|
|
--run the action
|
|
|
|
|
return session:execute("transfer", digits.." XML "..context);
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--run the menu again (without play ivr_menu_invalid_sound)
|
|
|
|
|
return menu();
|
2013-07-15 23:47:22 +02:00
|
|
|
end
|
|
|
|
|
|
2016-02-03 14:53:22 +01:00
|
|
|
--invalid input try again
|
|
|
|
|
if (debug["action"]) then
|
2016-02-04 14:05:35 +01:00
|
|
|
log.notice("unrecgnized action");
|
2016-02-03 14:53:22 +01:00
|
|
|
end
|
|
|
|
|
if ivr_menu_invalid_sound and #ivr_menu_invalid_sound then
|
|
|
|
|
session:streamFile(ivr_menu_invalid_sound);
|
2012-12-05 08:21:59 +01:00
|
|
|
end
|
2016-02-03 14:53:22 +01:00
|
|
|
return menu();
|
2012-11-30 11:44:58 +01:00
|
|
|
end --end function
|
|
|
|
|
|
|
|
|
|
--answer the session
|
2015-04-24 04:47:10 +02:00
|
|
|
if (session:ready()) then
|
2012-11-30 11:44:58 +01:00
|
|
|
session:answer();
|
|
|
|
|
menu();
|
2015-04-24 04:47:10 +02:00
|
|
|
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);
|
2016-03-11 14:21:52 +01:00
|
|
|
end
|
2015-04-24 04:47:10 +02:00
|
|
|
if (ivr_menu_greet_short_is_base64 and file_exists(ivr_menu_greet_short)) then
|
|
|
|
|
--os.remove(ivr_menu_greet_short);
|
2016-03-11 14:21:52 +01:00
|
|
|
end
|
2015-04-24 04:47:10 +02:00
|
|
|
if (ivr_menu_invalid_sound_is_base64 and file_exists(ivr_menu_invalid_sound)) then
|
|
|
|
|
--os.remove(ivr_menu_invalid_sound);
|
2016-03-11 14:21:52 +01:00
|
|
|
end
|
2015-03-19 00:31:14 +01:00
|
|
|
end
|