Skip to main content

Lua arguments calling functions

About

This article is brief code snippet to show how you can call functions in a script by using arguments to the script.

Click here to expand Table of Contents

Call a lua function via an argument passed to the script.

add this to your dialplan, and dial 582

    <extension name="test lua">
<condition field="destination_number" expression="^(lua|582)$">
<action application="lua" data="test.lua"/>
</condition>
</extension>


This example is using bind_digit_action to make this happen. You can dial *, #, or 0 to hear script in action

local uuid = session:getVariable("uuid");
local wav1 = "ivr/ivr-welcome_to_freeswitch.wav";
local wav2 = "ivr/ivr-please_hold_while_party_contacted.wav";

function func1()
session:consoleLog("CRIT","function 1 executed");
session:execute("playback", wav1);
session:destroy();
end

function func2()
session:consoleLog("CRIT","function 2 executed");
session:execute("playback", wav2);
session:destroy();
end

if argv[1] == "myarg1" then
func1()
elseif argv[1] == "myarg2" then
func2()
end

session:execute("bind_digit_action","myrealm,*,exec:lua,test.lua myarg1");
session:execute("bind_digit_action","myrealm,#,exec:lua,test.lua myarg2");
session:execute("bind_digit_action","myrealm,0,exec:hangup");
session:answer();

--HIGH BEEP
session:execute("playback","tone_stream://%(500,500,1000,800,500);loops=1");

--ENDLESS TONE
session:execute("playback","tone_stream://%(100,0,150);loops=-1");


in this test case you initially hear a high beep then land on endless tone... dialing star(*), the bind_digit_action is calling up a second instance of the script, so in those argumentative functions 1 & 2 you call "session:destroy();" when finished with that function to end that secondary instance of script, and return you back to first instance of script. Notice you do not hear the high beep this time, so basically picking up where you left off on first lua instance, landing you back on the endless tone from which you came before hitting * or #... Dial 0 to hangup...