<html>
<head>
    <base href="https://wiki.asterisk.org/wiki">
            <link rel="stylesheet" href="/wiki/s/en/2160/3/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/Tips+and+Tricks">Tips and Tricks</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://wiki.asterisk.org/wiki/display/~mdavenport">Malcolm Davenport</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" > <br>{info} <br></td></tr>
            <tr><td class="diff-changed-lines" >In Asterisk <span class="diff-changed-words">1<span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">.1</span>0</span> an autoservice is automatically started for you by default. <br></td></tr>
            <tr><td class="diff-unchanged" >{info} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="TipsandTricks-LongRunningOperations%28Autoservcie%29"></a>Long Running Operations (Autoservcie)</h2>

<p>Before starting long running operations, an autoservice should be started using the <tt>autoservice_start()</tt> function. An autoservice will ensure that the user hears a continuous stream of audio while your lua code works in the background. This autoservice will automatically be stopped before executing applications and dialplan functions and will be restarted afterwards. The autoservice can be stopped using autoservice_stop() and the autoservice_status() function will return <tt>true</tt> if an autoservice is currently running.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">app.startmusiconhold()

autoservice_start()
do_expensive_db_query()
autoservice_stop()

app.stopmusiconhold()</pre>
</div></div>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/wiki/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>In Asterisk 10 an autoservice is automatically started for you by default.</td></tr></table></div>

<h2><a name="TipsandTricks-DefiningExtensionsDynamically"></a>Defining Extensions Dynamically</h2>

<p>Since extensions are functions in pbx_lua, any function can be used, including closures. A function can be defined that returns extension functions and used to populate the extensions table.</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">extensions = {}
extensions.default = {}

function sip_exten(e)
   return function()
      app.dial("SIP/" .. e)
   end
end

extensions.default[100] = sip_exten(100)
extensions.default[101] = sip_exten(101)</pre>
</div></div>

<h2><a name="TipsandTricks-CreatingCustomAliasesforBuiltinConstructs"></a>Creating Custom Aliases for Built-in Constructs</h2>

<p>If you don't like the <tt>app</tt> table being named 'app' or if you think typing 'channel' to access the <tt>channel</tt> table is too much work, you can rename them.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>I prefer less typing</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">function my_exten(context, extensions)
   c = channel
   a = app

   c.my_variable = "my new channel variable"
   a.dial("SIP/100")
end</pre>
</div></div>

<h2><a name="TipsandTricks-RepurposingThe%7B%7Bprint%7D%7DFunction"></a>Re-purposing The <tt>print</tt> Function</h2>

<p>Lua has a built in "print" function that outputs things to stdout, but for Asterisk, we would rather have the output go in the verbose log. To do so, we could rewrite the <tt>print</tt> function as follows.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">function print(...)
   local msg = ""
   for i=1,select('#', ...) do
      if i == 1 then
         msg = msg .. tostring(select(i, ...))
      else
         msg = msg .. "\t" .. tostring(select(i, ...))
      end
   end

   app.verbose(msg)
end</pre>
</div></div>

<h2><a name="TipsandTricks-SplittingConfigurationintoMultipleFiles"></a>Splitting Configuration into Multiple Files</h2>

<p>The <tt>require</tt> method can be used to load lua modules or additional files.</p>

<h2><a name="TipsandTricks-UsingExternalModules"></a>Using External Modules</h2>

<p>Lua modules can be loaded using the standard <tt>require</tt> lua method. Some of the functionality provided by various lua modules is already included in Asterisk (e.g. func_odbc provides what LuaSQL provides). It is generally better to use code built-in to Asterisk over external lua modules. Specifically, the func_odbc module uses a connection pool to provide database resources, where as with LuaSQL each channel would have to make a new connection to the database on its own.</p>

<h2><a name="TipsandTricks-Compileextensions.lua"></a>Compile extensions.lua</h2>

<p>The <tt>luac</tt> program can be used to compile your <tt>extensions.lua</tt> file into lua bytecode. This will slightly increase performance as pbx_lua will no longer need to parse <tt>extensions.lua</tt> on load. The <tt>luac</tt> compiler will also detect and report any syntax errors. To use <tt>luac</tt>, rename your <tt>extensions.lua</tt> file and then run <tt>luac</tt> as follows.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Assume you name your extensions.lua file extensions.lua.lua</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">luac -o extensions.lua extensions.lua.lua</pre>
</div></div>

<p>The pbx_lua module automatically knows the difference between a lua text file and a lua bytecode file.</p>
    </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/Tips+and+Tricks">View Online</a>
        |
        <a href="https://wiki.asterisk.org/wiki/pages/diffpagesbyversion.action?pageId=16548028&revisedVersion=10&originalVersion=9">View Changes</a>
                |
        <a href="https://wiki.asterisk.org/wiki/display/AST/Tips+and+Tricks?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>