freeswitch lua会议模块
2015-12-30 14:21
387 查看
总共三个脚本
1.
--[[
FreeSWITCH lua dbh debug and other utils
参数说明:
debug = 脚本执行日志
dbh = 数据库操作
用法:
备注:
--]]
ODBCName = "fs_manager";
ODBCUser = "root";
ODBCPwd = "root";
debug = {} --freeSWITCH Script Log info
t_dbh = {} --freeSWITCH DBH
--create the DB handle
function t_dbh.connect()
dbh = freeswitch.Dbh("odbc://"..ODBCName..":"..ODBCUser..":"..ODBCPwd.."") -- connect to Mysql ODBC database
if(assert(dbh:connected())) then
freeswitch.consoleLog("notice","mysql db connected !\n");
end
end
-- release the Db handle
function t_dbh.close()
dbh:release()
end
-- query db
function t_dbh.query (queryString)
local table={};
local i = 1;
assert(dbh:query(queryString,function(row)
table[i]= row;
i=i+1;
end))
return table;
end;
-- exec update delete and modify
function t_dbh.exec(queryString)
-- local dbh = freeswitch.Dbh("odbc://"..ODBCName..":"..ODBCUser..":"..ODBCPass.."")
assert(dbh:query(queryString))
local affected_rows = dbh:affected_rows();
-- dbh:release() -- optional
return affected_rows ;
end
function GetFormatDate()
local date=os.date("%Y%m%d%H%M%S");
return date
end
function debug.var(k, v)
v = v or 'nil'
freeswitch.consoleLog("notice", " ====DebugVar=== " .. k .. ": " .. v .. "\n")
end
-- Log INFO
function debug.info(s)
freeswitch.consoleLog("info", "Script "..argv[0].." info :"..s .. "\n")
end
--Log NOTICE
function debug.notice(s)
freeswitch.consoleLog("notice", "Script "..argv[0].." notice :"..s .. "\n")
end
--Log ERR
function debug.err(s)
freeswitch.consoleLog("err", "Script "..argv[0].." error :"..s .. "\n");
end
2.
--[[
FreeSWITCH 会议流程处理脚本
参数说明:
用法:
备注:
]]
-- 头文件
require "conference/common";
function session_hangup_hook(status)
status=status or 'nil';
session:streamFile("conference/conf-goodbye.wav");
debug.info(status.."\n")
t_dbh.close();
session:hangup();
error();
end
function check_input_conference_pin(min, max, attempts, timeout, pin_number)
local pin_attempt = 1
local pin_max_attempt = 3
while pin_attempt <= pin_max_attempt do
session:flushDigits();
conference_pin = session:playAndGetDigits(min, max, attempts, timeout, '#', 'conference/conf-enter_conf_pin.wav', '', '\\d+|\\*')
session:say(conference_pin, "en", "name_spelled", "iterated");
if tonumber(conference_pin) == tonumber(pin_number) then
return true
else
session:streamFile("conference/conf-bad-pin.wav");
end
pin_attempt = pin_attempt + 1
end
return false
end
function get_participant_count(confroom)
local total = 2;
api = freeswitch.API();
local output = api:executeString("conference "..confroom.." list count");
debug.info("output.."..output);
if (string.find(output,"not")) then
total = 0;
else
total = output;
end
debug.info("Get_Participant_Count.."..total);
return total;
end
function say_other_participant_count(participant_count)
if(participant_count == 0) then
debug.info("This is the first person in the conference");
session:streamFile("conference/conf-alone.wav");
else
session:streamFile("conference/conf-has_joined.wav");
session:say(participant_count, "en", "name_spelled", "iterated");
session:streamFile("conference/conf-listeners_in_conference.wav");
end
end
function get_autocall_conf_member(mysession,conf_id,profile_name,conf_no,gw_name)
if(tonumber(conf_id) >= 0) then
mysession:setVariable("conference_auto_outcall_timeout", "5");
mysession:setVariable("conference_auto_outcall_caller_id_name", conf_no);
mysession:setVariable("conference_auto_outcall_caller_id_number",conf_no);
-- api_hangup_hook todo
mysession:setVariable("conference_auto_outcall_prefix", "{sip_auto_answer=true,api_hangup_hook='lua Hangup.lua',session_in_hangup_hook=true,ignore_early_media=false,mark=conference}");
mysession:setVariable("conference_auto_outcall_profile", "default");
sql_autocall_member = [[select call_number,call_type,moderator,call_uuid]]..
[[ from ep_conference_member ]]..
[[ where conf_id = ]]..conf_id..[[ and is_originate = 0 ]];
automemData = t_dbh.query(sql_autocall_member);
if #automemData > 0 then
for i = 1, #automemData do
local call_number = automemData[i]["call_number"]
local call_type = automemData[i]["call_type"]
local moderator = automemData[i]["moderator"]
local call_uuid = automemData[i]["call_uuid"];
local dial_string = '' ;
if(tonumber(call_type) == 0 ) then
dial_string=[[{origination_uuid=]]..call_uuid..[[}user/]]..tostring(call_number)
elseif(tonumber(call_type) == 1 ) then
dial_string=[[{origination_uuid=]]..call_uuid..[[}sofia/gateway/]]..gw_name..[[/0]].. tostring(call_number)
else
debug.err("call_type error ! conf_id : "..conf_id.."call_number : "..call_number)
do break end;
end
mysession:execute("conference_set_auto_outcall",dial_string)
if(tonumber(moderator) == 1 ) then
mysession:setVariable("conference_auto_outcall_flags", "moderator");
end
end
end
else
debug.err("conf_id error ! \n");
end
end
session:setHangupHook("session_hangup_hook")
t_dbh.connect()
conf_id = argv[1]
debug.info("############ Lua Script do_conf CONFERENCE IS CRATING !\n");
if session:ready() then
local call_number = session:getVariable("call_number");
local call_type = session:getVariable("call_type");
local moderator = session:getVariable("moderator");
local company_id = session:getVariable("company_id");
local gw_name = session:getVariable("gw_name");
sql_conf_info = [[select conf_no,conf_pin,company_id,conf_profile,domain from ep_conference where id = ]]..conf_id;
conf_data = t_dbh.query(sql_conf_info) ;
if #conf_data == 1 then
local conf_no = conf_data[1]["conf_no"];
local conf_pin = conf_data[1]["conf_pin"];
local company_id = conf_data[1]["company_id"];
local conf_profile = conf_data[1]["conf_profile"];
local domain = conf_data[1]["domain"];
--check the conference had set password or not
if conf_pin == nil or conf_pin == "" then
debug.notice("string.format('Conference %s has no PIN, Creating conference\n', tostring(conf_no))")
--join the conference
get_autocall_conf_member(session,conf_id,conf_profile,conf_no,gw_name);
session:execute("conference", string.format("%s@default", conf_no..'-'..domain))
else
--check the conf pwd 3 times default
if (check_input_conference_pin(2,5,3,3000,conf_pin) == true ) then
debug.notice(string.format("Conference %s correct PIN entered,Creating conference\n", conf_no))
get_autocall_conf_member(session,conf_id,conf_profile,conf_no,gw_name);
session:execute("conference", string.format("%s@default+%s", conf_no..'-'..domain,conf_pin))
else
debug.err("conference pin error ! \n");
do return end ;
end
end
-- the conference has been created
debug.info("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxa the end \n")
else
debug.info("create conference err, conference number doesn't exist \n");
do return end ;
end
end
function TaskCurrentCallDesc(task_Id)
DbhDataExec([[update [ReportTask] set [CurrentCall]=[CurrentCall]- 1 where Task_Id = ]].. task_Id)
end
3
--[[
FreeSWITCH Lua Conference Room Function
参数说明:
argv[1] = 会议Id
用法:
luarun conference/scriptname.lua conf_id
备注:
]]
-- 头文件
require "conference/common"
debug.info("############ Lua Script [" .. argv[0] .. "] Starting ############### CONFERENCE IS CRATING !\n");
-- 脚本参数判断
if argv[1] == nil then
debug.err("argv error ,please input the conference id \n")
do return end;
end
conf_id = tonumber(argv[1]) -- 创建会议Id
debug.info("############conf_id############"..conf_id);
--max_channels = 1000 ; --初始化系统最大通道数 ,默认最大通道数 1000 线
--serverIP = '10.0.0.85'
t_dbh.connect()
api = freeswitch.API()
-- 获取freeSWITCH 当前通道数量 在此做系统检查 todo
reply = api:executeString("show channels count");
_,_,number,_= string.find(reply,"(%d+)%s*(%a+)"); -- 解析字符串
channelCount = tonumber(number);
sql_conf_member = [[select call_number,call_type,moderator,company_id,call_uuid ]]..
[[ from ep_conference_member m ]]..
[[ where conf_id = ]]..conf_id..[[ and is_originate = 1]];
--
debug.info("###############sql_conf_member: : "..sql_conf_member)
-- 执行 外呼 操作 [ argv1: query string ]
taskData = t_dbh.query(sql_conf_member);
if #taskData == 1 then
local call_number = taskData[1]["call_number"];
local call_type = taskData[1]["call_type"];
local moderator = taskData[1]["moderator"];
local company_id = taskData[1]["company_id"];
local call_uuid = taskData[1]["call_uuid"];
local gw_name = "mygatew";--强制写死
--定义 呼叫 字符串
if tonumber(call_type) == 0 then -- 内网小号
dial_string=[[{api_hangup_hook='lua Hangup.lua',session_in_hangup_hook=true]]..
[[,gw_name=]]..gw_name..[[,conf_number=]]..call_number..
[[,company_id=]]..company_id..[[,call_type=]]..call_type..
[[,ignore_early_media=true,hangup_after_bridge=true]]..
[[,mark=conference,origination_uuid=]]..call_uuid..[[}user/]]..tostring(call_number);
elseif tonumber(call_type) == 1 then -- PSTN 手机固话
dial_string=[[{api_hangup_hook='lua Hangup.lua',session_in_hangup_hook=true]]..
[[,gw_name=]]..gw_name..[[,conf_number=]]..call_number..
[[,company_id=]]..company_id..[[,call_type=]]..call_type..
[[,ignore_early_media=true,hangup_after_bridge=true]]..
[[,mark=conference,origination_uuid=]]..call_uuid..[[}sofia/gateway/]]..gw_name..[[/0]].. tostring(call_number);
else
debug.err("call_number err !\n");
do return end;
end
debug.info("###############dial_string : : "..dial_string)
-- 接通会议主叫方 进入会议路由脚本
api:execute("bgapi", [[originate ]] .. dial_string .. [[ '&lua(conference/do_conf.lua ]]..conf_id..[[)']])
freeswitch.msleep(200);
end
--close the db handler
t_dbh.close();
1.
--[[
FreeSWITCH lua dbh debug and other utils
参数说明:
debug = 脚本执行日志
dbh = 数据库操作
用法:
备注:
--]]
ODBCName = "fs_manager";
ODBCUser = "root";
ODBCPwd = "root";
debug = {} --freeSWITCH Script Log info
t_dbh = {} --freeSWITCH DBH
--create the DB handle
function t_dbh.connect()
dbh = freeswitch.Dbh("odbc://"..ODBCName..":"..ODBCUser..":"..ODBCPwd.."") -- connect to Mysql ODBC database
if(assert(dbh:connected())) then
freeswitch.consoleLog("notice","mysql db connected !\n");
end
end
-- release the Db handle
function t_dbh.close()
dbh:release()
end
-- query db
function t_dbh.query (queryString)
local table={};
local i = 1;
assert(dbh:query(queryString,function(row)
table[i]= row;
i=i+1;
end))
return table;
end;
-- exec update delete and modify
function t_dbh.exec(queryString)
-- local dbh = freeswitch.Dbh("odbc://"..ODBCName..":"..ODBCUser..":"..ODBCPass.."")
assert(dbh:query(queryString))
local affected_rows = dbh:affected_rows();
-- dbh:release() -- optional
return affected_rows ;
end
function GetFormatDate()
local date=os.date("%Y%m%d%H%M%S");
return date
end
function debug.var(k, v)
v = v or 'nil'
freeswitch.consoleLog("notice", " ====DebugVar=== " .. k .. ": " .. v .. "\n")
end
-- Log INFO
function debug.info(s)
freeswitch.consoleLog("info", "Script "..argv[0].." info :"..s .. "\n")
end
--Log NOTICE
function debug.notice(s)
freeswitch.consoleLog("notice", "Script "..argv[0].." notice :"..s .. "\n")
end
--Log ERR
function debug.err(s)
freeswitch.consoleLog("err", "Script "..argv[0].." error :"..s .. "\n");
end
2.
--[[
FreeSWITCH 会议流程处理脚本
参数说明:
用法:
备注:
]]
-- 头文件
require "conference/common";
function session_hangup_hook(status)
status=status or 'nil';
session:streamFile("conference/conf-goodbye.wav");
debug.info(status.."\n")
t_dbh.close();
session:hangup();
error();
end
function check_input_conference_pin(min, max, attempts, timeout, pin_number)
local pin_attempt = 1
local pin_max_attempt = 3
while pin_attempt <= pin_max_attempt do
session:flushDigits();
conference_pin = session:playAndGetDigits(min, max, attempts, timeout, '#', 'conference/conf-enter_conf_pin.wav', '', '\\d+|\\*')
session:say(conference_pin, "en", "name_spelled", "iterated");
if tonumber(conference_pin) == tonumber(pin_number) then
return true
else
session:streamFile("conference/conf-bad-pin.wav");
end
pin_attempt = pin_attempt + 1
end
return false
end
function get_participant_count(confroom)
local total = 2;
api = freeswitch.API();
local output = api:executeString("conference "..confroom.." list count");
debug.info("output.."..output);
if (string.find(output,"not")) then
total = 0;
else
total = output;
end
debug.info("Get_Participant_Count.."..total);
return total;
end
function say_other_participant_count(participant_count)
if(participant_count == 0) then
debug.info("This is the first person in the conference");
session:streamFile("conference/conf-alone.wav");
else
session:streamFile("conference/conf-has_joined.wav");
session:say(participant_count, "en", "name_spelled", "iterated");
session:streamFile("conference/conf-listeners_in_conference.wav");
end
end
function get_autocall_conf_member(mysession,conf_id,profile_name,conf_no,gw_name)
if(tonumber(conf_id) >= 0) then
mysession:setVariable("conference_auto_outcall_timeout", "5");
mysession:setVariable("conference_auto_outcall_caller_id_name", conf_no);
mysession:setVariable("conference_auto_outcall_caller_id_number",conf_no);
-- api_hangup_hook todo
mysession:setVariable("conference_auto_outcall_prefix", "{sip_auto_answer=true,api_hangup_hook='lua Hangup.lua',session_in_hangup_hook=true,ignore_early_media=false,mark=conference}");
mysession:setVariable("conference_auto_outcall_profile", "default");
sql_autocall_member = [[select call_number,call_type,moderator,call_uuid]]..
[[ from ep_conference_member ]]..
[[ where conf_id = ]]..conf_id..[[ and is_originate = 0 ]];
automemData = t_dbh.query(sql_autocall_member);
if #automemData > 0 then
for i = 1, #automemData do
local call_number = automemData[i]["call_number"]
local call_type = automemData[i]["call_type"]
local moderator = automemData[i]["moderator"]
local call_uuid = automemData[i]["call_uuid"];
local dial_string = '' ;
if(tonumber(call_type) == 0 ) then
dial_string=[[{origination_uuid=]]..call_uuid..[[}user/]]..tostring(call_number)
elseif(tonumber(call_type) == 1 ) then
dial_string=[[{origination_uuid=]]..call_uuid..[[}sofia/gateway/]]..gw_name..[[/0]].. tostring(call_number)
else
debug.err("call_type error ! conf_id : "..conf_id.."call_number : "..call_number)
do break end;
end
mysession:execute("conference_set_auto_outcall",dial_string)
if(tonumber(moderator) == 1 ) then
mysession:setVariable("conference_auto_outcall_flags", "moderator");
end
end
end
else
debug.err("conf_id error ! \n");
end
end
session:setHangupHook("session_hangup_hook")
t_dbh.connect()
conf_id = argv[1]
debug.info("############ Lua Script do_conf CONFERENCE IS CRATING !\n");
if session:ready() then
local call_number = session:getVariable("call_number");
local call_type = session:getVariable("call_type");
local moderator = session:getVariable("moderator");
local company_id = session:getVariable("company_id");
local gw_name = session:getVariable("gw_name");
sql_conf_info = [[select conf_no,conf_pin,company_id,conf_profile,domain from ep_conference where id = ]]..conf_id;
conf_data = t_dbh.query(sql_conf_info) ;
if #conf_data == 1 then
local conf_no = conf_data[1]["conf_no"];
local conf_pin = conf_data[1]["conf_pin"];
local company_id = conf_data[1]["company_id"];
local conf_profile = conf_data[1]["conf_profile"];
local domain = conf_data[1]["domain"];
--check the conference had set password or not
if conf_pin == nil or conf_pin == "" then
debug.notice("string.format('Conference %s has no PIN, Creating conference\n', tostring(conf_no))")
--join the conference
get_autocall_conf_member(session,conf_id,conf_profile,conf_no,gw_name);
session:execute("conference", string.format("%s@default", conf_no..'-'..domain))
else
--check the conf pwd 3 times default
if (check_input_conference_pin(2,5,3,3000,conf_pin) == true ) then
debug.notice(string.format("Conference %s correct PIN entered,Creating conference\n", conf_no))
get_autocall_conf_member(session,conf_id,conf_profile,conf_no,gw_name);
session:execute("conference", string.format("%s@default+%s", conf_no..'-'..domain,conf_pin))
else
debug.err("conference pin error ! \n");
do return end ;
end
end
-- the conference has been created
debug.info("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxa the end \n")
else
debug.info("create conference err, conference number doesn't exist \n");
do return end ;
end
end
function TaskCurrentCallDesc(task_Id)
DbhDataExec([[update [ReportTask] set [CurrentCall]=[CurrentCall]- 1 where Task_Id = ]].. task_Id)
end
3
--[[
FreeSWITCH Lua Conference Room Function
参数说明:
argv[1] = 会议Id
用法:
luarun conference/scriptname.lua conf_id
备注:
]]
-- 头文件
require "conference/common"
debug.info("############ Lua Script [" .. argv[0] .. "] Starting ############### CONFERENCE IS CRATING !\n");
-- 脚本参数判断
if argv[1] == nil then
debug.err("argv error ,please input the conference id \n")
do return end;
end
conf_id = tonumber(argv[1]) -- 创建会议Id
debug.info("############conf_id############"..conf_id);
--max_channels = 1000 ; --初始化系统最大通道数 ,默认最大通道数 1000 线
--serverIP = '10.0.0.85'
t_dbh.connect()
api = freeswitch.API()
-- 获取freeSWITCH 当前通道数量 在此做系统检查 todo
reply = api:executeString("show channels count");
_,_,number,_= string.find(reply,"(%d+)%s*(%a+)"); -- 解析字符串
channelCount = tonumber(number);
sql_conf_member = [[select call_number,call_type,moderator,company_id,call_uuid ]]..
[[ from ep_conference_member m ]]..
[[ where conf_id = ]]..conf_id..[[ and is_originate = 1]];
--
debug.info("###############sql_conf_member: : "..sql_conf_member)
-- 执行 外呼 操作 [ argv1: query string ]
taskData = t_dbh.query(sql_conf_member);
if #taskData == 1 then
local call_number = taskData[1]["call_number"];
local call_type = taskData[1]["call_type"];
local moderator = taskData[1]["moderator"];
local company_id = taskData[1]["company_id"];
local call_uuid = taskData[1]["call_uuid"];
local gw_name = "mygatew";--强制写死
--定义 呼叫 字符串
if tonumber(call_type) == 0 then -- 内网小号
dial_string=[[{api_hangup_hook='lua Hangup.lua',session_in_hangup_hook=true]]..
[[,gw_name=]]..gw_name..[[,conf_number=]]..call_number..
[[,company_id=]]..company_id..[[,call_type=]]..call_type..
[[,ignore_early_media=true,hangup_after_bridge=true]]..
[[,mark=conference,origination_uuid=]]..call_uuid..[[}user/]]..tostring(call_number);
elseif tonumber(call_type) == 1 then -- PSTN 手机固话
dial_string=[[{api_hangup_hook='lua Hangup.lua',session_in_hangup_hook=true]]..
[[,gw_name=]]..gw_name..[[,conf_number=]]..call_number..
[[,company_id=]]..company_id..[[,call_type=]]..call_type..
[[,ignore_early_media=true,hangup_after_bridge=true]]..
[[,mark=conference,origination_uuid=]]..call_uuid..[[}sofia/gateway/]]..gw_name..[[/0]].. tostring(call_number);
else
debug.err("call_number err !\n");
do return end;
end
debug.info("###############dial_string : : "..dial_string)
-- 接通会议主叫方 进入会议路由脚本
api:execute("bgapi", [[originate ]] .. dial_string .. [[ '&lua(conference/do_conf.lua ]]..conf_id..[[)']])
freeswitch.msleep(200);
end
--close the db handler
t_dbh.close();
相关文章推荐
- lua math.floor()的一个BUG
- LUA string库详解
- 【Lua】将Lua5.1的setfenv迁移到Lua5.2的_ENV
- lua 中 常用的 string 函数
- lua堆栈操作
- 关于lua中如何修改引用的值
- Lua简易教程
- Visual Sutdio2015 C++嵌入Lua脚本环境配置
- 在lua中如何remove掉table里面的数据
- C/C++,VS下使用luajit(环境配置)
- Windows下LuaJIT的编译和使用,将Lua脚本编译为bytecode
- lua table 计算长度
- 一次生活引发的“Lua随机数生成”问题
- Lua学习笔记(6) : 在Lua中调用C++之函数
- lua 的常用math 函数
- Coco2dx-3.0中怎样调用LUA
- lua通过bunlde读注意事项
- lua在线编程工具
- python――Excel导入导出Lua/Json/Xml
- os函数 lua