Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在两个lua文件之间传递会话_Lua_Freeswitch - Fatal编程技术网

在两个lua文件之间传递会话

在两个lua文件之间传递会话,lua,freeswitch,Lua,Freeswitch,我想从我的主脚本调用另一个lua脚本,比如 会话:执行(“lua”、“/path/somefile.lua”..somearg1....somearg2..) 它工作正常,somefile.lua正在执行,但假设我也想在那里使用session,也就是说,我正在访问somefile.lua中的数据库,并想使用session在somefile.lua中说出查询结果。(会话:发言(queryResult)) 我还尝试将会话作为参数之一发送 会话:执行(“lua”,“/path/somefile.lua

我想从我的主脚本调用另一个lua脚本,比如 会话:执行(“lua”、“/path/somefile.lua”..somearg1....somearg2..) 它工作正常,somefile.lua正在执行,但假设我也想在那里使用session,也就是说,我正在访问somefile.lua中的数据库,并想使用session在somefile.lua中说出查询结果。(会话:发言(queryResult))

我还尝试将会话作为参数之一发送 会话:执行(“lua”,“/path/somefile.lua”。.session) 但它给出了一个错误“尝试连接全局“会话”(userdata值) 有什么建议吗

第一个lua文件的代码

session:answer();
session:setAutoHangup(false);
session:set_tts_params("flite","kal");
callerId = session:getVariable("caller_id_number");
session:execute("lua ","/etc/freeswitch/scripts/checkbal.lua "..callerId.." "..session);
session:destroy();
第二个lua文件的代码

callerId=argv[1];
session=argv[2];
luasql = require "luasql.postgres";
env=assert(luasql:postgres());
con=assert(env:connect("mydb","postgres","password","127.0.0.1","5432"));
cur=assert(con:execute("select balance from bal where number='"..callerId.."'"));  
session:set_tts_params("flite","kal");
row=cur:fetch({},"a");
res=row.balance;
session:speak(res);

将第二个文件装配为返回函数或函数表的模块。下面是一个示例,其中第二个文件返回一个“speak”函数,您可以根据需要多次重复使用该函数:

第一个Lua文件的代码:

session:answer()
session:setAutoHangup(false)
session:set_tts_params("flite","kal")
callerId = session:getVariable("caller_id_number")
speak = require 'checkbal'
speak(session, callerId)
-- session:execute("lua ","/etc/freeswitch/scripts/checkbal.lua "..callerId.." "..session)
session:destroy()
luasql = require "luasql.postgres"

local env=assert(luasql:postgres())
local con=assert(env:connect("mydb","postgres","password","127.0.0.1","5432"))

local function speak(session, callerId)
    local cur = assert(con:execute("select balance from bal where number='"..callerId.."'"))
    session:set_tts_params("flite","kal")
    row=cur:fetch({},"a")
    res=row.balance
    session:speak(res)
end

return speak
第二个Lua文件的代码:

session:answer()
session:setAutoHangup(false)
session:set_tts_params("flite","kal")
callerId = session:getVariable("caller_id_number")
speak = require 'checkbal'
speak(session, callerId)
-- session:execute("lua ","/etc/freeswitch/scripts/checkbal.lua "..callerId.." "..session)
session:destroy()
luasql = require "luasql.postgres"

local env=assert(luasql:postgres())
local con=assert(env:connect("mydb","postgres","password","127.0.0.1","5432"))

local function speak(session, callerId)
    local cur = assert(con:execute("select balance from bal where number='"..callerId.."'"))
    session:set_tts_params("flite","kal")
    row=cur:fetch({},"a")
    res=row.balance
    session:speak(res)
end

return speak
注意:这是Lua:不需要分号


我会考虑用“说话”的方法把“session”变成一个对象(用方法的表),但是这超出了这个问题的范围,而且不是必要的,可能会导致以后更可维护的代码。p> 一段代码可能会help@W.B.正如您所看到的,这是一个简单的代码。我只是需要一个方法来做这件事。我还尝试使用session:transfer()并从dialplan扩展名执行第二个lua文件,但它也不起作用。