added delayed paging (#6771)

* added delayed paging

* Modified the code to use sched_api as requested

* added the beep
This commit is contained in:
Antonio Fernandez
2024-01-12 16:52:16 -05:00
committed by GitHub
parent c44d1825cf
commit a4bfd1f20f
2 changed files with 206 additions and 147 deletions

View File

@@ -6,6 +6,7 @@
<action application="set" data="destinations=101-103,105" /> <action application="set" data="destinations=101-103,105" />
<action application="set" data="moderator=false" /> <action application="set" data="moderator=false" />
<action application="set" data="mute=true" /> <action application="set" data="mute=true" />
<action application="set" data="delay=false" />
<action application="set" data="check_destination_status=true" enabled="false" /> <action application="set" data="check_destination_status=true" enabled="false" />
<action application="set" data="api_hangup_hook=conference page-${destination_number}@${domain_name} hup all" enabled="false"/> <action application="set" data="api_hangup_hook=conference page-${destination_number}@${domain_name} hup all" enabled="false"/>
<action application="set" data="execute_on_answer=sched_hangup +80 allotted_timeout" enabled="false"/> <action application="set" data="execute_on_answer=sched_hangup +80 allotted_timeout" enabled="false"/>

View File

@@ -25,176 +25,218 @@
-- POSSIBILITY OF SUCH DAMAGE. -- POSSIBILITY OF SUCH DAMAGE.
--set default settings --set default settings
pin_number = ""; pin_number = "";
max_tries = "3"; max_tries = "3";
digit_timeout = "3000"; digit_timeout = "3000";
--define the trim function --define the trim function
require "resources.functions.trim"; require "resources.functions.trim";
--define the explode function --define the explode function
require "resources.functions.explode"; require "resources.functions.explode";
--define the split function --define the split function
require "resources.functions.split"; require "resources.functions.split";
--iterator over numbers. --iterator over numbers.
local function each_number(value) local function each_number(value)
local begin_value, end_value = split_first(value, "-", true) local begin_value, end_value = split_first(value, "-", true)
if (not end_value) or (begin_value == end_value) then if (not end_value) or (begin_value == end_value) then
return function()
local result = begin_value
begin_value = nil
return result
end
end
if string.find(begin_value, "^0") then
assert(#begin_value == #end_value, "number in range with leading `0` should have same length")
end
local number_length = ("." .. tostring(#begin_value))
begin_value, end_value = tonumber(begin_value), tonumber(end_value)
assert(begin_value and end_value and (begin_value <= end_value), "Invalid range: " .. value)
return function() return function()
value, begin_value = begin_value, begin_value + 1 local result = begin_value
if value > end_value then return end begin_value = nil
return string.format("%" .. number_length .. "d", value) return result
end end
end end
if string.find(begin_value, "^0") then
assert(#begin_value == #end_value, "number in range with leading `0` should have same length")
end
local number_length = ("." .. tostring(#begin_value))
begin_value, end_value = tonumber(begin_value), tonumber(end_value)
assert(begin_value and end_value and (begin_value <= end_value), "Invalid range: " .. value)
return function()
value, begin_value = begin_value, begin_value + 1
if value > end_value then return end
return string.format("%" .. number_length .. "d", value)
end
end
--make sure the session is ready --make sure the session is ready
if ( session:ready() ) then if ( session:ready() ) then
--answer the call --answer the call
session:answer(); session:answer();
--get the dialplan variables and set them as local variables --get the dialplan variables and set them as local variables
destination_number = session:getVariable("destination_number"); destination_number = session:getVariable("destination_number");
pin_number = session:getVariable("pin_number"); pin_number = session:getVariable("pin_number");
domain_name = session:getVariable("domain_name"); domain_name = session:getVariable("domain_name");
sounds_dir = session:getVariable("sounds_dir"); sounds_dir = session:getVariable("sounds_dir");
destinations = session:getVariable("destinations"); destinations = session:getVariable("destinations");
rtp_secure_media = session:getVariable("rtp_secure_media"); rtp_secure_media = session:getVariable("rtp_secure_media");
if (destinations == nil) then if (destinations == nil) then
destinations = session:getVariable("extension_list"); destinations = session:getVariable("extension_list");
end end
destination_table = explode(",",destinations); destination_table = explode(",",destinations);
caller_id_name = session:getVariable("caller_id_name"); caller_id_name = session:getVariable("caller_id_name");
caller_id_number = session:getVariable("caller_id_number"); caller_id_number = session:getVariable("caller_id_number");
sip_from_user = session:getVariable("sip_from_user"); sip_from_user = session:getVariable("sip_from_user");
mute = session:getVariable("mute"); mute = session:getVariable("mute");
delay = session:getVariable("delay");
--determine whether to check if the destination is available --determine whether to check if the destination is available
check_destination_status = session:getVariable("check_destination_status"); check_destination_status = session:getVariable("check_destination_status");
if (not check_destination_status) then check_destination_status = 'false'; end if (not check_destination_status) then check_destination_status = 'false'; end
--set the type of auto answer --set the type of auto answer
auto_answer = session:getVariable("auto_answer"); auto_answer = session:getVariable("auto_answer");
if (not auto_answer) then auto_answer = 'call_info'; end if (not auto_answer) then auto_answer = 'call_info'; end
if (auto_answer == 'call_info') then if (auto_answer == 'call_info') then
auto_answer = "sip_h_Call-Info=<sip:"..domain_name..">;answer-after=0"; auto_answer = "sip_h_Call-Info=<sip:"..domain_name..">;answer-after=0";
end end
if (auto_answer == 'sip_auto_answer') then if (auto_answer == 'sip_auto_answer') then
auto_answer = "sip_auto_answer=true"; auto_answer = "sip_auto_answer=true";
end end
--set sip header Alert-Info --set sip header Alert-Info
alert_info = session:getVariable("alert_info"); alert_info = session:getVariable("alert_info");
if (not alert_info) then alert_info = 'ring_answer'; end if (not alert_info) then alert_info = 'ring_answer'; end
if (alert_info == 'auto_answer') then if (alert_info == 'auto_answer') then
alert_info = "sip_h_Alert-Info='Auto Answer'"; alert_info = "sip_h_Alert-Info='Auto Answer'";
elseif (alert_info == 'ring_answer') then elseif (alert_info == 'ring_answer') then
alert_info = "sip_h_Alert-Info='Ring Answer'"; alert_info = "sip_h_Alert-Info='Ring Answer'";
else else
alert_info = "sip_h_Alert-Info='"..alert_info.."'"; alert_info = "sip_h_Alert-Info='"..alert_info.."'";
end end
--set the sounds path for the language, dialect and voice --set the sounds path for the language, dialect and voice
default_language = session:getVariable("default_language"); default_language = session:getVariable("default_language");
default_dialect = session:getVariable("default_dialect"); default_dialect = session:getVariable("default_dialect");
default_voice = session:getVariable("default_voice"); default_voice = session:getVariable("default_voice");
if (not default_language) then default_language = 'en'; end if (not default_language) then default_language = 'en'; end
if (not default_dialect) then default_dialect = 'us'; end if (not default_dialect) then default_dialect = 'us'; end
if (not default_voice) then default_voice = 'callie'; end if (not default_voice) then default_voice = 'callie'; end
--set rtp_secure_media to an empty string if not provided. --set rtp_secure_media to an empty string if not provided.
if (rtp_secure_media == nil) then if (rtp_secure_media == nil) then
rtp_secure_media = 'false'; rtp_secure_media = 'false';
end end
--define the conference name --define the conference name
local conference_profile = "page"; local conference_profile = "page";
local conference_name = "page-"..destination_number.."@"..domain_name; local conference_name = "page-"..destination_number.."@"..domain_name;
local conference_bridge = conference_name.."@"..conference_profile; local conference_bridge = conference_name.."@"..conference_profile;
--set the caller id --set the caller id
if (caller_id_name) then if (caller_id_name) then
--caller id name provided do nothing --caller id name provided do nothing
else else
effective_caller_id_name = session:getVariable("effective_caller_id_name"); effective_caller_id_name = session:getVariable("effective_caller_id_name");
caller_id_name = effective_caller_id_name; caller_id_name = effective_caller_id_name;
end end
if (caller_id_number) then if (caller_id_number) then
--caller id number provided do nothing --caller id number provided do nothing
else else
effective_caller_id_number = session:getVariable("effective_caller_id_number"); effective_caller_id_number = session:getVariable("effective_caller_id_number");
caller_id_number = effective_caller_id_number; caller_id_number = effective_caller_id_number;
end end
--set conference flags --set conference flags
if (mute == "true") then if (mute == "true") then
flags = "flags{mute}"; flags = "flags{mute}";
else else
flags = "flags{}"; flags = "flags{}";
end end
--if the pin number is provided then require it --if annouce delay is active then prompt for recording
if (pin_number) then if (delay == "true") then
--sleep --callback function for the delayed recording
session:sleep(500); function onInputCBF(s, _type, obj, arg)
local k, v = nil, nil
--get the user pin number if (_type == "dtmf") then
min_digits = 2; dtmf_entered = 1; --set this variable to know that the user entered DTMF
max_digits = 20; return 'break'
digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_pass:#", "", "\\d+"); else
return ''
--validate the user pin number
pin_number_table = explode(",",pin_number);
for index,pin_number in pairs(pin_number_table) do
if (digits == pin_number) then
--set the variable to true
auth = true;
--set the authorized pin number that was used
session:setVariable("pin_number", pin_number);
--end the loop
break;
end
end end
end
--sleep
session:sleep(500);
--set variables for page recording
recording_dir = '/tmp/';
filename = "page-"..destination_number.."@"..domain_name..".wav";
recording_filename = string.format('%s%s', recording_dir, filename);
max_len_secs = 20;
silence_threshold = 500;
silence_secs = 1;
dtmf_entered = 0;
silence_triggered = 0;
--ask user to record
session:execute("playback", sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/voicemail/vm-record_message.wav")
session:streamFile("tone_stream://L=1;%(1000, 0, 640)");
--set callback function for when a user clicks DTMF
session:setInputCallback('onInputCBF', '');
--time before starting the recording
startUTCTime = os.time(os.date('!*t'));
--record the page message
silence_triggered = session:recordFile(recording_filename, max_len_secs, silence_threshold, silence_secs);
--time after starting the recording
endUTCTime = os.time(os.date('!*t'));
--total recording time
recording_length = endUTCTime - startUTCTime;
end
--if not authorized play a message and then hangup --if the pin number is provided then require it
if (not auth) then if (pin_number) then
session:streamFile("phrase:voicemail_fail_auth:#"); --sleep
session:hangup("NORMAL_CLEARING"); session:sleep(500);
return;
--get the user pin number
min_digits = 2;
max_digits = 20;
digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", "phrase:voicemail_enter_pass:#", "", "\\d+");
--validate the user pin number
pin_number_table = explode(",",pin_number);
for index,pin_number in pairs(pin_number_table) do
if (digits == pin_number) then
--set the variable to true
auth = true;
--set the authorized pin number that was used
session:setVariable("pin_number", pin_number);
--end the loop
break;
end end
end end
--log the destinations --if not authorized play a message and then hangup
freeswitch.consoleLog("NOTICE", "[page] destinations "..destinations.." available\n"); if (not auth) then
session:streamFile("phrase:voicemail_fail_auth:#");
session:hangup("NORMAL_CLEARING");
return;
end
end
--create the api object --log the destinations
api = freeswitch.API(); freeswitch.consoleLog("NOTICE", "[page] destinations "..destinations.." available\n");
--get the channels --create the api object
if (check_destination_status == 'true') then api = freeswitch.API();
cmd_string = "show channels";
channel_result = api:executeString(cmd_string);
end
--originate the calls --get the channels
destination_count = 0; if (check_destination_status == 'true') then
cmd_string = "show channels";
channel_result = api:executeString(cmd_string);
end
--originate the calls
destination_count = 0;
if (delay ~= "true" or (dtmf_entered == 1 or silence_triggered == 1)) then
for index,value in pairs(destination_table) do for index,value in pairs(destination_table) do
for destination in each_number(value) do for destination in each_number(value) do
@@ -242,17 +284,33 @@
end end
end end
end end
end
--send main call to the conference room --send main call to the conference room
if (destination_count > 0) then
if (destination_count > 0) then
--set moderator flag
if (session:getVariable("moderator") == "true") then if (session:getVariable("moderator") == "true") then
moderator_flag = ",moderator"; moderator_flag = ",moderator";
else else
moderator_flag = ""; moderator_flag = "";
end end
session:execute("conference", conference_bridge.."+flags{endconf,mintwo"..moderator_flag.."}"); --check if delay is true
else if (delay == "true" and (dtmf_entered == 1 or silence_triggered == 1)) then
session:execute("playback", "tone_stream://%(500,500,480,620);loops=3");
end --play the recorded file into the page/conference. Need to wait for the page/conference to actually be started before we can end it.
response = api:executeString("sched_api +2 none conference "..conference_name.." play "..recording_filename);
end --wait for recording to finish then end page/conference
response = api:executeString("sched_api +"..tostring(recording_length+4).." none conference "..conference_name.." hup all");
else
--join the moderator into the page
session:execute("conference", conference_bridge.."+flags{endconf,mintwo"..moderator_flag.."}");
end
else
--error tone due to no destinations
session:execute("playback", "tone_stream://%(500,500,480,620);loops=3");
end
end