IPv4 Fremnet Logo
TOOLS, TINKERINGS & CODE

Support me

Asterisk Support for LUA · Sept 20, 16:34 by Shannon Wynter

After performing a fresh install of Asterisk (1.6) on a new box (decommissioning an old one) I found that it had support for LUA in the dialplan.

Of course, I’ve never used LUA but I know it’s used in some games as the primary scripting engine – which means it must be fast and powerful right? :)

Well, over the course of experimenting with LUA I’ve found that using it to power an Asterisk Dialplan results in some interesting hacks.

Have a look through the code, some of it is incomplete, some of the call_* functions will be enhanced later, but the basics are there

  1. configuration = {
  2.   area = 7;
  3.   country = 61;
  4. }
  5.  
  6. function linenotinuse()
  7.   app.wait(1)
  8.   app.answer()
  9.   app.playback("tt-monkeys")
  10.   app.hangup()
  11. end
  12.  
  13. function time()
  14.   app.datetime()
  15.   app.wait(1)
  16.   time()
  17. end
  18.  
  19. -- Eventually some of the call_* functions will become more complicated.
  20.  
  21. function call_emergency(ctx, ext)
  22.   app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
  23. end
  24.  
  25. function call_localrate(ctx, ext)
  26.   app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
  27. end
  28.  
  29. function call_freecall(ctx, ext)
  30.   app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
  31. end
  32.  
  33. function call_local(ctx, ext)
  34.   app.dial("SIP/nodephone/0" .. configuration.area .. ext, 600, 'TWK')
  35. end
  36.  
  37. function call_std(ctx, ext)
  38.   app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
  39. end
  40.  
  41. function call_mobile(ctx, ext)
  42.   app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
  43. end
  44.  
  45. function call_international(ctx, ext)
  46.   app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
  47. end
  48.  
  49. function call_lan(ctx, ext)
  50.   app.chanisavail("SIP/" .. ext, "s")
  51.   status = channel["AVAILSTATUS"]:get()
  52.   app.noop("Status: " .. status)
  53.   if status == "4" or status == "5" or status == "20" then
  54.     app.playback("pbx-invalid")
  55.     return
  56.   end
  57.   app.dial("SIP/" .. ext)
  58. end
  59.  
  60. -- For incomming calls from nodephone (possibly others)
  61. function nodephoneincoming()
  62.   -- Make sure both the phones are available
  63.   app.chanisavail("SIP/9100&SIP/9101", "s")
  64.   status = channel["AVAILSTATUS"]:get()
  65.   
  66.   -- If both phones appear to be available
  67.   if string.find(status, '2', 1, true) == nil and string.find(status, '3', 1, true) == nil and string.find(status, '6', 1, true) == nil then
  68.     app.dial("SIP/9100&SIP/9101", 60, 'twk')
  69.     dialstatus = channel["DIALSTATUS"]:get()
  70.   else
  71.     dialstatus = "BUSY"
  72.   end
  73.  
  74.   -- If there was no answer or the line was busy or unavailable, take a message
  75.   if not dialstatus == "ANSWER" then
  76.     app.noop(dialstatus)
  77.     app.goto("takemessage", "s", 1)
  78.   end
  79. end
  80.  
  81. -- Prompt up to 3 times to leave a message
  82. function takemessage()
  83.   if count == nil then count = 0 elseif count < 2 then count = count + 1 else hangup() end
  84.   if count == 0 then
  85.     app.answer()
  86.     app.wait(2)
  87.     if dialstatus == "BUSY" then
  88.       message = "the-party-you-are-calling&is-curntly-busy"
  89.     elseif dialstatus == "CHANUNAVAIL" then
  90.       message = "cannot-complete-network-error"
  91.     else
  92.       message = "away-naughty-boy"
  93.     end
  94.     message = message .. "&"
  95.   else
  96.     message = ""
  97.   end
  98.  
  99.   app.background(message .. "T-to-leave-msg&press-star", "", "", "takemessage")
  100.  
  101.   if not(channel["EXTEN"]:get() == "*") then app.waitexten() end
  102. end
  103.  
  104. function hangup()
  105.   app.playback("thank-you-for-calling")
  106.   app.playback("goodbye")
  107.   app.hangup()
  108. end
  109.  
  110. function voicemail()
  111.   app.voicemail("9100@default")
  112. end
  113.  
  114. function voicemailmain()
  115.   app.nocdr()
  116.   app.voicemailmain()
  117. end
  118.  
  119. extensions = {
  120.   -- Context used for message taking - bit of a hack...
  121.   takemessage = {
  122.     s = takemessage;
  123.     t = takemessage;
  124.     i = takemessage;
  125.     ["*"] = voicemail;
  126.   };
  127.  
  128.   -- Context used to potentially incomming providers
  129.   incoming = {
  130.     nodephone = nodephoneincoming;
  131.     pennytel = linenotinuse;
  132.   };
  133.  
  134.   -- Generally unused context - it's here for voicemail really
  135.   default = {
  136.     s = linenotinuse;
  137.   };
  138.  
  139.   -- All dialplan for the internal lines
  140.   internal = {
  141.     -- It doesn't seem the lua code can directly include the parkedcalls context
  142.     ["_970X"] = function()
  143.       app.goto("parkedcalls", exten, "1")
  144.     end;
  145.     ["101"] = voicemailmain;
  146.     ["102"] = time;
  147.     ["000"] = call_emergency;
  148.     ["_91XX"] = call_lan;
  149.     ["_13ZXXX"] = call_localrate;
  150.     ["_1300XXXXXX"] = call_localrate;
  151.     ["_1800XXXXXX"] = call_freecall;
  152.     ["_NXXXXXXX"] = call_local;
  153.     ["_0[2378]XXXXXXXX"] = call_std;
  154.     ["_04XXXXXXXX"] = call_mobile;
  155.     ["_0011."] = call_international;
  156.   };
  157. }
  158.  
  159. Download this code: extensions.lua (Downloaded 1882 time(s))

Comments

Spam no more - rel=nofollow is active here, spamming my comments will not help your page rank.

  Textile help
---== Copyright Shannon Wynter - All rights reserved - All wrongs avenged ==---