freeswitch.API

This is the second workhorse of FreeSWITCH Lua scripting. The API object allows you to send API commands to FreeSWITCH exactly as if you were at the console.

API commands are provided by mod_commands (look it up in https://freeswitch.org/confluence) and by many other modules (in fact, almost all modules provide additional API commands). You can see a list of all API commands available in your FreeSWITCH server by issuing the following from the FreeSWITCH console (help will do the same):

show api 

Let's look at the freeswitch.API object doing its things: edit the file /usr/local/freeswitch/scripts/test6.lua:

freeswitch.consoleLog("WARNING","before creating the API object
")   
api = freeswitch.API()   
freeswitch.consoleLog("WARNING","after creating the API object
")   
reply = api:executeString("version") 
freeswitch.consoleLog("WARNING","reply is: " .. reply .."
") 
reply = api:executeString("status") 
freeswitch.consoleLog("WARNING","reply is: " .. reply .."
") 
reply = api:executeString("sofia status") 
freeswitch.consoleLog("WARNING","reply is: " .. reply .."
") 
reply = api:executeString("bgapi originate user/1011 5000") 
-- reply = api:executeString("originate user/1011 5000") 
freeswitch.consoleLog("WARNING","reply is: " .. reply .."
") 
counter = 0 
while(counter < 20) do 
        reply = api:executeString("show channels") 
        freeswitch.consoleLog("WARNING","reply #" .. counter .. " is: " .. reply .."
") 
        counter = counter + 1 
        api:executeString("msleep 1000") 
end 
freeswitch.consoleLog("WARNING","about to hangup all calls in the server
") 
reply = api:executeString("hupall") 
freeswitch.consoleLog("WARNING","reply is: " .. reply .."
") 
freeswitch.consoleLog("WARNING","GOODBYE (world)
") 
 

This script will first create the API object, then use it to interact with the FreeSWITCH server as if it were you at the console (it issues the commands version, status, and sofia status, then print back on the console what the server returned after each API command). After those informative actions, our script takes a more decisive route, and originates an outbound call leg toward a phone registered as 1011 to the server. Then it shows the result of the show channels command twenty times, pausing for one second, and eventually hangs up all the calls on the FreeSWITCH server.

You can execute it from the console by issuing lua test6.lua; it will block your console until the script exits. Or you can execute it issuing luarun test6.lua and you'll see the entire process in real time:

Also, note that you can use this same "execute a Lua script" technique from outside the console, that is, from a shell or a cron script, in the following two forms:

fs_cli -x "lua test6.lua" 
fs_cli -x "luarun test6.lua" 

You will have no console printing of logs, obviously. To get your printouts back you can delete the following line:

freeswitch.consoleLog("WARNING","reply #" .. counter .. " is: " .. reply .."
") 

and insert this one at its place:

stream:write("reply #" .. counter .. " is: " .. reply .."
") 

This will not work if you invoke the script with luarun (luarun has no access to the stream object). So, you're stuck collecting your printouts at script's exit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.54.7