Skip to content

Call forwarding in Auerswald PBX (lua)#

Example: LUA Template Call forwarding in Auerswald PBX

call_fwd_in_pbx.xml
<?xml version="1.0" encoding="UTF-8"?>
<templates>
    <!-- switch the call forwarding for not responding within the PBX on or off
    parameter: identity
    parameter: target: call forwarding target
    -->
    <template name="@string/call_forwarding_no_response_title">
    <keyConfiguration>
      <lua><code><![CDATA[
local valid = false
local active = false
local on = "*61*"
local off = "#61#"
local userpath = "/identities/identity["..identity.."]/"
local host = config.get(userpath .. "host")
local user = config.get(userpath .. "username")

local function updateLed()
  if valid then
    if active then
      key:setLed( "green" )
    else
      key:setLed( "off" )
    end
  else
    key:setLed( "red", true )
  end
end

local unescape = function(url)
  return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
end

local function check_sub(data, uri, handle)
  local x = xml.eval(data)
  local test_uri_on = "sip:"..on..target.."#"..user.."@"..host
  if unescape(uri) == test_uri_on and x ~= nil then
    for _,dlg in ipairs(x) do
      state = dlg:find("state")
      if state and state[1] == "trying" then
        active = true
      elseif state and state[1] == "terminated" then
        active = false
      end
    end
  end
  updateLed()
end

local function check_valid()
  if identity =="" or target=="" then
    valid = false
  else
    valid = true
  end
end

local function sub()
  sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
end

function onKeyUp()
  if active and valid then
    sip.invite(off.."#"..user)
  elseif valid then
    sip.invite(on..target.."#"..user)
  end
end

check_valid()
updateLed()
sub()
]]></code>
          <params>
            <!-- These parameters are available by name within the lua-script (code-section).
                 Their value is passed from the parameters-section (see below) -->
            <param name="identity"/>
            <param name="target"/>
          </params>
        </lua>
      </keyConfiguration>
      <parameters>
        <!-- entered by user in phone-UI or on website when configuring this key-function.
             Will then be passed as value to the lua-scripts params-section. -->
        <parameter name="@string/identity">         
      <path>//param[@name="identity"]/value</path>
        </parameter>
        <parameter name="@string/target">
          <path>//param[@name="target"]/value</path>
        </parameter>
      </parameters>
    </template>
</templates>