Rewrite speed_dial Lua script. (#2239)
This commit is contained in:
committed by
FusionPBX
parent
d9a18714c4
commit
984e8f16e7
@@ -18,9 +18,8 @@
|
|||||||
-- Portions created by the Initial Developer are Copyright (C) 2016
|
-- Portions created by the Initial Developer are Copyright (C) 2016
|
||||||
-- the Initial Developer. All Rights Reserved.
|
-- the Initial Developer. All Rights Reserved.
|
||||||
|
|
||||||
--set defaults
|
-- load config
|
||||||
expire = {}
|
require "resources.functions.config";
|
||||||
expire["speed_dial"] = "3600";
|
|
||||||
|
|
||||||
--set debug
|
--set debug
|
||||||
--debug["sql"] = false;
|
--debug["sql"] = false;
|
||||||
@@ -31,73 +30,67 @@
|
|||||||
destination_number = session:getVariable("destination_number");
|
destination_number = session:getVariable("destination_number");
|
||||||
context = session:getVariable("context");
|
context = session:getVariable("context");
|
||||||
|
|
||||||
--connect to the database
|
--load libraries
|
||||||
|
local log = require "resources.functions.log"["app:dialplan:outbound:speed_dial"]
|
||||||
local Database = require "resources.functions.database";
|
local Database = require "resources.functions.database";
|
||||||
|
local cache = require "resources.functions.cache";
|
||||||
|
local json = require "resources.functions.lunajson";
|
||||||
|
|
||||||
--include json library
|
-- search in memcache first
|
||||||
local json
|
local key = "app:dialplan:outbound:speed_dial:" .. destination_number .. "@" .. context
|
||||||
if (debug["sql"]) then
|
local source = "memcache"
|
||||||
json = require "resources.functions.lunajson"
|
local value = cache.get(key)
|
||||||
|
|
||||||
|
-- decode value from memcache
|
||||||
|
if value then
|
||||||
|
local t = json.decode(value)
|
||||||
|
if not (t and t.phone_number and t.context) then
|
||||||
|
log.warning("can not decode value from memcache: %s", value)
|
||||||
|
value = nil
|
||||||
|
else
|
||||||
|
value = t
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--prepare the api object
|
-- search in database
|
||||||
api = freeswitch.API();
|
if not value then
|
||||||
|
-- set source flag
|
||||||
|
source = "database"
|
||||||
|
|
||||||
--define the trim function
|
-- connect to database
|
||||||
require "resources.functions.trim";
|
local dbh = Database.new('system');
|
||||||
|
|
||||||
--get the cache
|
-- search real phone number in database
|
||||||
cache = trim(api:execute("memcache", "get app:dialplan:outbound:speed_dial:" .. destination_number .. "@" .. context));
|
local sql = "SELECT phone_number "
|
||||||
|
sql = sql .. "FROM v_contact_phones "
|
||||||
|
sql = sql .. "WHERE phone_speed_dial = :phone_speed_dial "
|
||||||
|
sql = sql .. "AND domain_uuid = :domain_uuid "
|
||||||
|
|
||||||
--get the destination number
|
local params = {phone_speed_dial = destination_number, domain_uuid = domain_uuid};
|
||||||
if (cache == "-ERR NOT FOUND") then
|
|
||||||
local dbh = Database.new('system');
|
|
||||||
|
|
||||||
local sql = "SELECT phone_number "
|
if (debug["sql"]) then
|
||||||
sql = sql .. "FROM v_contact_phones "
|
log.noticef("SQL: %s; params: %s", sql, json.encode(params));
|
||||||
sql = sql .. "WHERE phone_speed_dial = :phone_speed_dial "
|
|
||||||
--sql = sql .. "AND domain_uuid = :domain_uuid "
|
|
||||||
local params = {phone_speed_dial = destination_number};
|
|
||||||
if (debug["sql"]) then
|
|
||||||
freeswitch.consoleLog("notice", "SQL:" .. sql .. "; params: " .. json.encode(params) .. "\n");
|
|
||||||
end
|
|
||||||
dbh:query(sql, params, function(row)
|
|
||||||
|
|
||||||
--set the local variables
|
|
||||||
phone_number = row.phone_number;
|
|
||||||
|
|
||||||
--set the cache
|
|
||||||
result = trim(api:execute("memcache", "set app:dialplan:outbound:speed_dial:" .. destination_number .. "@" .. context .. " 'destination_number=" .. destination_number .. "&phone_number=" .. phone_number.. "&context=" .. context .. "' "..expire["speed_dial"]));
|
|
||||||
|
|
||||||
--log the result
|
|
||||||
freeswitch.consoleLog("notice", "[app:dialplan:outbound:speed_dial] " .. destination_number .. " XML " .. context .. " source: database\n");
|
|
||||||
|
|
||||||
--transfer the call
|
|
||||||
session:transfer(row.phone_number, "XML", context);
|
|
||||||
end);
|
|
||||||
|
|
||||||
else
|
|
||||||
--add the function
|
|
||||||
require "resources.functions.explode";
|
|
||||||
|
|
||||||
--define the array/table and variables
|
|
||||||
local var = {}
|
|
||||||
local key = "";
|
|
||||||
local value = "";
|
|
||||||
|
|
||||||
--parse the cache
|
|
||||||
key_pairs = explode("&", cache);
|
|
||||||
for k,v in pairs(key_pairs) do
|
|
||||||
f = explode("=", v);
|
|
||||||
key = f[1];
|
|
||||||
value = f[2];
|
|
||||||
var[key] = value;
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--send to the console
|
local phone_number = dbh:first_value(sql, params)
|
||||||
freeswitch.consoleLog("notice", "[app:dialplan:outbound:speed_dial] " .. cache .. " source: memcache\n");
|
|
||||||
|
-- release database connection
|
||||||
|
dbh:release()
|
||||||
|
|
||||||
|
-- set the cache
|
||||||
|
if phone_number then
|
||||||
|
value = {context = context, phone_number = phone_number}
|
||||||
|
cache.set(key, json.encode(value), expire["speed_dial"])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- transfer
|
||||||
|
if value then
|
||||||
|
--log the result
|
||||||
|
log.noticef("%s XML %s source: %s", destination_number, value.context, source)
|
||||||
|
|
||||||
--transfer the call
|
--transfer the call
|
||||||
session:transfer(var["phone_number"], "XML", var["context"]);
|
session:transfer(value.phone_number, "XML", value.context);
|
||||||
|
else
|
||||||
|
log.warningf('can not find number: %s in domain: %s', destination_number, domain_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user