FreeSWITCH自带很多cdr模块,有时太多了也不好,搞的不知道怎么选择。我觉得不必纠结,用mod_lua可以很容易,现在就试试
-- apt install -y lua-socket, or yum install -y lua-socket
-- luarun simple_cdr.lua
local url = "http://localhost:8080/cdr"
-- local api = freeswitch.API()
local socket = require "socket"
local http = require("socket.http")
local ltn12 = require("ltn12")
function debug(s)
freeswitch.consoleLog("DEBUG", s .. "\n")
end
function post_cdr(e)
http.TIMEOUT = 10
local response_body = {}
local headers = {
["content-Type"] = "application/json",
["content-length"] = #reqbody
}
local reqbody = e:serialize("json")
local r, c, h, s = http.request {
method = "POST",
url = url,
headers = headers,
source = ltn12.source.string(reqbody),
sink = ltn12.sink.table(response_body)
}
debug("HTTP status-code = " .. c )
local res = table.concat(response_body)
debug("response_body = " .. response_body)
end
local con = freeswitch.EventConsumer()
con:bind("SHUTDOWN")
con:bind("CHANNEL_HANGUP_COMPLETE")
while true do
local e = con:pop()
if e then
local event_name = e:getHeader("Event-Name") or ""
if event_name == "SHUTDOWN" then break
elseif event_name == "CHANNEL_HANGUP_COMPLETE" then
post_cdr(e)
end
end
end
-- 暂未调试,如有问题请让我知道