freeswitch的lua脚本记录

--输出日志
local test = "test";
--输出info日志
freeswitch.consoleLog("INFO", test);
--输出error日志
freeswitch.consoleLog("err", test);
--输出debug日志
freeswitch.consoleLog("debug", test);    
获取通道中的变量
-- 获取通道变量中的主叫
local callerNumber=session:getVariable("caller_id_number");
-- 获取通道变量中的被叫
local calledNumber=session:getVariable("destination_number");
-- 获取通道变量中的uuid,每通通话都是唯一的
local uuid=session:getVariable("uuid");
--注:获取通道中的变量方式session:getVariable("{变量}");

--脚本接收外部传入参数
--传入第一个参数
local argv1 = argv[1];
--传入第二个参数
local argv2 = argv[2];
--...
--注:传入的参数不能为空,为空下一个参数会填充


--脚本简单的一些set操作
--将通话接听
session:answer();
--播放ivr音
session:execute("playback", "/home/test.wav");
--将vccid为1007set到通道变量中
session:execute("set", "vccid=1007");
--通话录音
session:execute("record_session", "/home/record.wav")

--进行外呼操作
--定义参数
local called="18649760218"
--(1)第一种方式
--呼叫18649760218号码,简单的用ip端口外呼方式,lua脚本拼接参数是用..
session:execute("bridge", "sofia/external/"..called.."@172.16.xx.xx:5080")
--使用13000000000为主叫进行外呼
session:execute("bridge", "{origination_caller_id_number=13000000000}sofia/external/"..called.."@172.16.xx.xx:5080")
--呼叫18649760218号码,使用gateway方式呼叫, gatewayName是动态的,在profile里配置的网关
session:execute("bridge", "sofia/gateway/{gatewayName}/"..called.."")
--使用13000000000为主叫进行外呼
session:execute("bridge", "{origination_caller_id_number=13000000000}sofia/gateway/{gatewayName}/"..called.."")
--呼叫18649760218号码,使用轮训组方式呼叫,使用xhsbc这个轮训组,profile为xhsbc
session:execute("bridge", "sofia/gateway/${distributor(xhsbc ${sofia(profile test gwlist down)})}/"..called.."")
--使用13000000000为主叫进行外呼
session:execute("bridge", "{origination_caller_id_number=13000000000}sofia/gateway/${distributor(xhsbc ${sofia(profile test gwlist down)})}/"..called.."")

--(2)第二种方式,将呼叫命令提取出来,单独定义
local command="{origination_caller_id_number=13000000000}sofia/external/"..called.."@172.16.xxx.xx:5080";
session:execute("bridge", command)

--(3)第三种方式,将呼叫命令提取出来,单独定义,并且拆分多个内容
local ostrs = {};
ostrs[1] = "{maxDuration=3600,origination_caller_id_number=13000000000";
ostrs[2] = ",vccid=1007";
ostrs[3] = ",servicekey=1007";
ostrs[4] = "}";
ostrs[5]="sofia/external/"..called.."@172.16.246.38:5080";
--将上述所有指令进行整合
ostr0 = table.concat(ostrs);
--最后是这样的
ostr0="{maxDuration=3600,origination_caller_id_number=13000000000,vccid=1007,servicekey=1007}sofia/external/"..called.."@172.16.246.38:5080";
--最后进行外呼操作
session:execute("bridge", ostr0)

--调用另一个脚本使用bgapi命令
local str1="str1";
local str2="str2";

--注意:传入的参数,两个参数之间要空格
commad = "bgapi lua test.lua "..str1.." "..str2.."";
api:executeString(commad);


--使用uuid方式进行放音
api = freeswitch.API();
-- 获取通道变量中的uuid,每通通话都是唯一的
local uuid=session:getVariable("uuid");
local callCaller = "uuid_broadcast " .. uuid .. " /home/test.wav";
api:executeString(callCaller);


--将某些操作封装成方法
function playback()
    session:execute("playback", "/home/test.wav");
end

--执行这个方法
playback();

--判断文件是否存在
function exists(recordHintTone)
    local path="/usr/local/freeswitch/sounds/en/us/callie/";
    local exists=loadConfig(path..recordHintTone);
    if exists  then
        return "true";
    else
        return "false";

    end
end

--执行这个方法
local exists=exists("test.wav");

--lua脚本get请求url
--(1)没有返回值
require "curl"
--请求完整路径(包含请求参数)
local requestUrl = "http://172.16.xx.xx:80/forward/eventInfo?vccid=1007";
c:setopt(curl.OPT_URL, requestUrl)
c:perform()
freeswitch.consoleLog("INFO", record_id.."|"..callStatus.."|状态推送成功\n");

--(2)有返回值,返回json字符串
require "curl"
local cjson = require "cjson"
c = curl.easy_init()
--主函数
function main()
    
    requestUrl="http://172.16.xx.xx:80/forward/eventInfo?vccid=1007";
    freeswitch.consoleLog("INFO", "请求url|"..requestUrl.."\n")
    c:setopt(curl.OPT_URL, requestUrl)
    c:setopt(curl.OPT_WRITEFUNCTION, function(buffer)
        freeswitch.consoleLog("info", "打印日志: "..buffer.."\r\n");
        --将字符转转换成json
        result =  cjson.decode(buffer);
        --获取code参数
        local code= result["code"];
        return #buffer
    end)
    c:perform()
end

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值