Skip to content

Instantly share code, notes, and snippets.

@xopr
Last active October 28, 2022 15:14
Show Gist options
  • Save xopr/25b3fbae972ff7d398a675d02aa7a828 to your computer and use it in GitHub Desktop.
Save xopr/25b3fbae972ff7d398a675d02aa7a828 to your computer and use it in GitHub Desktop.
-- Lua implementation of PHP scandir function
function scandir(directory)
freeswitch.consoleLog( "debug", "scanning " .. directory );
local i, t, popen = 0, {}, io.popen
local pfile = popen("ls -a "..directory)
for filename in pfile:lines() do
i = i + 1
t[i] = filename
end
pfile:close()
return t
end
function randomWait( max )
wait = math.random(3000, max) ;
session:sleep(wait);
end
function randomize()
api = freeswitch.API();
seed = tonumber(api:execute("global_getvar", "randomseed"));
if seed == 12345678 or seed == "" or seed == nil then
seed = os.time();
end
math.randomseed(seed);
seed = math.random(1,999999)
api:execute("global_setvar", "randomseed=" .. seed)
end
seed=12345678;
randomize()
--freeswitch.consoleLog("info", "poem.lua up and running\n");
sound_base = session:getVariable('sounds_dir')
session:preAnswer();
session:sleep( 200 );
if ( session:ready() ) then
session:answer();
session:sleep( 1500 );
session:streamFile( "../../../poems/Groove_Asks_For_A_Poem.ogg" );
randomWait(8000);
local files = scandir(sound_base .. '/poems/*.wav')
session:streamFile( files[math.random(1,#files)] );
session:hangup("NORMAL_CLEARING");
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment