<html>
<head>
    <base href="https://wiki.asterisk.org/wiki">
            <link rel="stylesheet" href="/wiki/s/en/2160/1/7/_/styles/combined.css?spaceKey=AST&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://wiki.asterisk.org/wiki/display/AST/Examples">Examples</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://wiki.asterisk.org/wiki/display/~mnicholson">Matthew Nicholson</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >extensions = e <br>{code} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;"> <br>h2. Less Clutter v2 <br> <br>In this example, we use a fancy function to register extensions. <br> <br>{code:title=extensions.lua} <br>function register(context, extension, func) <br>   if not extensions then <br>      extensions = {} <br>   end <br> <br>   if not extensions[context] then <br>      extensions[context] = {} <br>   end <br> <br>   extensions[context][extension] = func <br>end <br> <br>function include(context, included_context) <br>   if not extensions then <br>      extensions = {} <br>   end <br> <br>   if not extensions[context] then <br>      extensions[context] = {} <br>   end <br> <br>   if not extensions[context].include then <br>      extensions[context].include = {} <br>   end <br> <br>   table.insert(extensions[context].include, included_context) <br>end <br> <br>-- this function serves as an extension function directly <br>function call_user(c, user) <br>   app.dial(&quot;SIP/&quot; .. user, 60) <br>end <br> <br>-- this function returns an extension function <br>function call_sales_queue(queue) <br>  return function(c, e) <br>      app.queue(queue) <br>   end <br>end <br> <br>include(&quot;default&quot;, &quot;users&quot;) <br>include(&quot;default&quot;, &quot;sales&quot;) <br> <br>register(&quot;users&quot;, &quot;100&quot;, call_user) <br>register(&quot;users&quot;, &quot;101&quot;, call_user) <br> <br>register(&quot;sales&quot;, &quot;5000&quot;, call_sales_queue(&quot;sales1&quot;)) <br>register(&quot;sales&quot;, &quot;6000&quot;, call_sales_queue(&quot;sales2&quot;)) <br>register(&quot;sales&quot;, &quot;7000&quot;, function() <br>   app.queue(&quot;sales3&quot;) <br>end) <br> <br>{code} <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Some example <tt>extensions.lua</tt> files can be found below. They demonstrate various ways to organize extensions.</p>

<h2><a name="Examples-LessClutter"></a>Less Clutter</h2>

<p>Instead of defining every extension inline, you can use this method to create a neater <tt>extensions.lua</tt> file. Since the extensions table and each context are both normal lua tables, you can treat them as such and build them piece by piece.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>extensions.lua</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">-- this function serves as an extension function directly
function call_user(c, user)
   app.dial("SIP/" .. user, 60)
end

-- this function returns an extension function
function call_sales_queue(queue)
  return function(c, e)
      app.queue(queue)
   end
end

e = {}

e.default = {}
e.default.include = {"users", "sales"}

e.users = {}
e.users["100"] = call_user
e.users["101"] = call_user

e.sales = {}
e.sales["5000"] = call_sales_queue("sales1")
e.sales["6000"] = call_sales_queue("sales2")

extensions = e</pre>
</div></div>

<h2><a name="Examples-LessClutterv2"></a>Less Clutter v2</h2>

<p>In this example, we use a fancy function to register extensions.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>extensions.lua</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">function register(context, extension, func)
   if not extensions then
      extensions = {}
   end

   if not extensions[context] then
      extensions[context] = {}
   end

   extensions[context][extension] = func
end

function include(context, included_context)
   if not extensions then
      extensions = {}
   end

   if not extensions[context] then
      extensions[context] = {}
   end

   if not extensions[context].include then
      extensions[context].include = {}
   end

   table.insert(extensions[context].include, included_context)
end

-- this function serves as an extension function directly
function call_user(c, user)
   app.dial("SIP/" .. user, 60)
end

-- this function returns an extension function
function call_sales_queue(queue)
  return function(c, e)
      app.queue(queue)
   end
end

include("default", "users")
include("default", "sales")

register("users", "100", call_user)
register("users", "101", call_user)

register("sales", "5000", call_sales_queue("sales1"))
register("sales", "6000", call_sales_queue("sales2"))
register("sales", "7000", function()
   app.queue("sales3")
end)</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://wiki.asterisk.org/wiki/users/removespacenotification.action?spaceKey=AST">Stop watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://wiki.asterisk.org/wiki/users/editmyemailsettings.action">Change email notification preferences</a>
</div>
        <a href="https://wiki.asterisk.org/wiki/display/AST/Examples">View Online</a>
        |
        <a href="https://wiki.asterisk.org/wiki/pages/diffpagesbyversion.action?pageId=16548020&revisedVersion=3&originalVersion=2">View Changes</a>
                |
        <a href="https://wiki.asterisk.org/wiki/display/AST/Examples?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>