Skip to main content

Lua Intercom example

About

Calls a defined list of extensions that are not currently active in a call then auto answers the call.

Dialplan entry

 <extension name="intercom" >
<condition field="destination_number" expression="^\*468$">
<action application="set" data="sounds_dir=/usr/local/freeswitch/sounds"/>
<action application="set" data="pin_number=1234"/>
<action application="set" data="caller_id_name=intercom"/>
<action application="set" data="extension_list=1002,1003"/>
<action application="set" data="caller_id_number=5551231234"/>
<action application="lua" data="intercom.lua"/>
</condition>
</extension>

Script intercom.lua

 --	intercom.lua
-- Part of FusionPBX
-- Copyright (C) 2010 Mark J Crane <markjcrane@fusionpbx.com>
-- 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.
--
-- THIS SOFTWARE IS PROVIDED ``AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES,
-- 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.

pin_number = "";
max_tries = "3";
digit_timeout = "3000";

function trim (s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

function explode ( seperator, str )
local pos, arr = 0, {}
for st, sp in function() return string.find( str, seperator, pos, true ) end do -- for each divider found
table.insert( arr, string.sub( str, pos, st-1 ) ) -- attach chars left of current divider
pos = sp + 1 -- jump past current divider
end
table.insert( arr, string.sub( str, pos ) ) -- attach chars right of last divider
return arr
end

if ( session:ready() ) then
session:answer();
pin_number = session:getVariable("pin_number");
domain_name = session:getVariable("domain_name");
sounds_dir = session:getVariable("sounds_dir");
extension_list = session:getVariable("extension_list");
caller_id_name = session:getVariable("caller_id_name");
caller_id_number = session:getVariable("caller_id_number");
extension_table = explode(",",extension_list);

--if the pin number is provided then require it
if (pin_number) then
min_digits = string.len(pin_number);
max_digits = string.len(pin_number);
digits = session:playAndGetDigits(min_digits, max_digits, max_tries, digit_timeout, "#", sounds_dir.."/custom/please_enter_the_pin_number.wav", "", "\\d+");
if (digits == pin_number) then
--pin is correct
else
session:streamFile(sounds_dir.."/custom/your_pin_number_is_incorect_goodbye.wav");
session:hangup("NORMAL_CLEARING");
return;
end
end

api = freeswitch.API();
for index,value in pairs(extension_table) do
extension_status = "show channels like "..value.."@"..domain_name;
reply = api:executeString(extension_status);
reply = trim(reply);
if (reply == "0 total.") then
--freeswitch.consoleLog("NOTICE", "extension "..value.." available\n");
cmd_string = "bgapi originate {sip_auto_answer=true,absolute_codec_string=PCMU,hangup_after_bridge=false,origination_caller_id_name="..caller_id_name..",origination_caller_id_number="..caller_id_number.."}user/"..value.."@"..domain_name.." conference:intercom@default+flags{mute} inline";
--freeswitch.consoleLog("NOTICE", "cmd_string "..cmd_string.."\n");
api:executeString(cmd_string);
else
--call status busy
end
end

--send main call to the conference room
session:execute("conference", "intercom@default+flags{endconf}");
end