Call forwarding in Auerswald PBX (lua)

Example: LUA Template Call forwarding in Auerswald PBX

call_fwd_in_pbx.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <templates>
  3. <!-- switch the call forwarding for not responding within the PBX on or off
  4. parameter: identity
  5. parameter: target: call forwarding target
  6. -->
  7. <template name="@string/call_forwarding_no_response_title">
  8. <keyConfiguration>
  9. <lua>
  10. <code>
  11. local valid = false
  12. local active = false
  13. local on = "*61*"
  14. local off = "#61#"
  15. local userpath = "/identities/identity["..identity.."]/"
  16. local host = config.get(userpath .. "host")
  17. local user = config.get(userpath .. "username")
  18.  
  19. local function updateLed()
  20. if valid then
  21. if active then
  22. key:setLed( "green" )
  23. else
  24. key:setLed( "off" )
  25. end
  26. else
  27. key:setLed( "red", true )
  28. end
  29. end
  30.  
  31. local unescape = function(url)
  32. return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
  33. end
  34.  
  35. local function check_sub(data, uri, handle)
  36. local x = xml.eval(data)
  37. local test_uri_on = "sip:"..on..target.."#"..user.."@"..host
  38. if unescape(uri) == test_uri_on and x ~= nil then
  39. for _,dlg in ipairs(x) do
  40. state = dlg:find("state")
  41. if state and state[1] == "trying" then
  42. active = true
  43. elseif state and state[1] == "terminated" then
  44. active = false
  45. end
  46. end
  47. end
  48. updateLed()
  49. end
  50.  
  51. local function check_valid()
  52. if identity =="" or target=="" then
  53. valid = false
  54. else
  55. valid = true
  56. end
  57. end
  58.  
  59. local function sub()
  60. sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
  61. end
  62.  
  63. function onKeyUp()
  64. if active and valid then
  65. sip.invite(off.."#"..user)
  66. elseif valid then
  67. sip.invite(on..target.."#"..user)
  68. end
  69. end
  70.  
  71. check_valid()
  72. updateLed()
  73. sub()
  74. </code>
  75. <params>
  76. <!-- These parameters are available by name within the lua-script (code-section).
  77. Their value is passed from the parameters-section (see below) -->
  78. <param name="identity"/>
  79. <param name="target"/>
  80. </params>
  81. </lua>
  82. </keyConfiguration>
  83. <parameters>
  84. <!-- entered by user in phone-UI or on website when configuring this key-function.
  85. Will then be passed as value to the lua-scripts params-section. -->
  86. <parameter name="@string/identity">
  87. <path>//param[@name="identity"]/value</path>
  88. </parameter>
  89. <parameter name="@string/target">
  90. <path>//param[@name="target"]/value</path>
  91. </parameter>
  92. </parameters>
  93. </template>
  94. </templates>