lua - 在两个 lua 文件之间传递 session

标签 lua freeswitch

我想从我的主脚本中调用另一个 lua 脚本,比如
session:execute("lua","/path/somefile.lua "..somearg1..""..somearg2..)
它工作正常并且 somefile.lua 正在执行,但假设我也想在那里使用 session ,即我正在访问 somefile.lua 中的数据库并想通过使用 session 在 somefile.lua 中说出查询结果。 ( session :说话(查询结果))。

我也尝试发送 session 作为参数之一
session:execute("lua","/path/somefile.lua"..session)
但它给出了一个错误“尝试连接全局' session '(用户数据值)”
任何建议..??

第一个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()

第二个 Lua 文件的代码:
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:不需要分号。

我会考虑使用“speak”方法使“ session ”成为一个对象(带有方法的表),但这超出了这个问题的范围并且没有必要,可能只会在以后导致更多可维护的代码。

关于lua - 在两个 lua 文件之间传递 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22421638/

相关文章:

objective-c - lua_touserdata 返回 null

javascript - freeswitch 脚本中使用 mod_v8 的 javascript 文件的要求究竟如何工作?

sql - 当 Dbh 查询在 FreeSWITCH 中返回零行时,如何在 Lua 脚本中匹配 "-ERR no reply"?

python - FreeSwitch - 我如何使用 python 解析和采取行动?

math - 计算径向速度

c++ - 如何创建一个带有可变数量参数的模板函数,将参数传递给对象的正确构造函数?

lua - 表上加载字符串后的 nil 值

lua - 如何通过键删除lua表条目?

linux - freeswitch 的系统启动不一致

delphi - TIdTCPServer 或 TIdTCPClient 哪个更适合 pbx 事件?