Skip to content

Instantly share code, notes, and snippets.

@moteus
Last active April 18, 2017 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moteus/74c410a0a15509bf4aae84620721f9be to your computer and use it in GitHub Desktop.
Save moteus/74c410a0a15509bf4aae84620721f9be to your computer and use it in GitHub Desktop.
FusionPBX. Use Lua code to build CF dialplan from Web UI
local Database = require "resources.functions.database"
local route_to_bridge = require "resources.functions.route_to_bridge"
local json = require "resources.functions.lunajson"
local function decode_value(v)
return (string.gsub(v, [[\u00(%x%x)]], {
['22'] = '"';
['27'] = "'";
['26'] = "&";
['3C'] = "<"; ['3c'] = "<";
['3E'] = ">"; ['3e'] = ">";
}))
end
local function decode_values(t)
for k, v in pairs(t) do
if type(v) == 'string' then
t[k] = decode_value(v)
elseif type(v) == 'table' then
decode_values(v)
end
end
return t
end
local function decode_json(s)
-- tread NULL as empty string
return decode_values(json.decode(s, nil, ''))
end
local domain_uuid = argv[1]
local params = decode_json(argv[2])
local dbh = Database.new('system')
local bridge = route_to_bridge(dbh, domain_uuid, params)
if bridge then
table.insert(bridge, 1, bridge.bridge)
bridge.bridge = nil
end
stream:write(
json.encode(bridge)
)
--[[ PHP function
function event_socket_route_to_bridge($domain_uuid, $variables) {
//connect to fs
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if (!$fp) {
return false;
}
//call Lua command
if ($fp) {
//encode variables to pass as arguments (require PHP >= 5.3.0)
$variables = json_encode($variables, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);
//build and send the command to freeswitch
$switch_cmd = "lua call_forward_api.lua '$domain_uuid' '$variables'";
$switch_result = trim(event_socket_request($fp, 'api '.$switch_cmd));
fclose($fp);
//check result
if (substr($switch_result, 0, 3) == "-ERR") {
return false;
}
//decode result
$bridge = json_decode($switch_result);
//return value
if (empty($bridge)) {
return false;
}
return $bridge;
}
//can not create directory
return false;
}
]]
-- usage from call_forward PHP class
--[[
$bridge = event_socket_route_to_bridge($_SESSION['domain_uuid'], Array(
'destination_number' => $this->forward_all_destination,
'${user_exists}' => 'false',
'${toll_allow}' => $this->toll_allow,
'${caller_id_number}' => $this->extension,
));
if (($bridge === false) || (empty($bridge[0]))) { {
$dial_string .= 'error/NO_ROUTE_DESTINATION';
}
else {
$dial_string .= $bridge[0];
}
]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment