吴伟贤のBlog

Feed Rss

freeswitch lua 回拨实例

09.16.2013, freeswitch, by .

转自:http://www.8000hz.com/archives/freeswitch-callback-example.html

 

实现:
回拨实例。
使用lua实现,主要用于熟悉originate命令。

调用示例:

lua callback.lua 1377XXX4955 180XXXX9957 2345677 360 /tmp/test.wav

callback.lua

--
-- CALLBACK
--
-- Author: foxgoing@gmail.com / 8000hz.com
-- 
-- 功能描述:先拨打主叫,主叫接通后向主叫播放指定声音文件,然后拨打被叫,同时设置最大通话时长。
--           设置供其他应用使用的客户号。主被叫都为外线号码。

local caller_number = argv[1];     --主叫号码
local callee_number = argv[2];     --被叫号码
local customer_number = argv[3];   --主叫的平台账号
local maxtimes = argv[4];          --最大通话时长(秒)
local sounds = argv[5];            --声音文件路径

api = freeswitch.API();

local callstring = "bgapi originate {ringback="..sounds..",ignore_early_media=true}sofia/gateway/szcmcc/"..caller_number.." \'&lua(call.lua "..maxtimes.." "..callee_number.." "..customer_number..")\'";
freeswitch.consoleLog("notice", callstring.."\n");
api:executeString(callstring);
freeswitch.consoleLog("notice", "Leave callback Scripts\n");

call.lua

--
-- CALL , a part of callback
--
-- Author: foxgoing@gmail.com / 8000hz.com

local maxtimes = argv[1];          --最大通话时常秒
local callee_number = argv[2];     --被叫号码
local customer_number = argv[3];   --主叫的平台账号

session:execute("sched_hangup", "+"..maxtimes.." allotted_timeout");
session:execute("set", "customer_number="..customer_number);

session:execute("bridge", "sofia/gateway/szcmcc/"..callee_number);

freeswitch.consoleLog("notice", "Leave call.lua Scripts\n");

小贴士:
originate命令执行时,如果收到了183或200就会立即执行 ‘&’ 后面的语句,换句话说就是只要来了媒体就执行后面。

但人们的需求不一嘛,有些人需要只有接通主叫后才能执行’&’后面的语句。那么如果你就可以设置:

 {ignore_early_media=true}

如果要实现主被叫同时呼叫,但你的运营商只返回180,但你还是想要主被叫同时呼叫。那么你可以这样设置:

 {return_ring_ready=true}

评论已关闭。