<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/Tips+and+Tricks">Tips and Tricks</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" >In Asterisk 1.10 an autoservice is automatically started for you by default. <br>{info} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;"> <br>h2. Creating Custom Aliases for Built-in Constructs <br> <br>If you don&#39;t like the {{app}} table being named &#39;app&#39; or if you think typing &#39;channel&#39; to access the {{channel} table is too much work, you can rename them. <br> <br>{code:title=I prefer less typing} <br>function my_exten(context, extensions) <br>   c = channel <br>   a = app <br> <br>   c.my_variable = &quot;my new channel variable&quot; <br>   a.dial(&quot;SIP/100&quot;) <br>end <br>{code} <br> <br>h2. Re-purposing The {{print}} Function <br> <br>Lua has a built in &quot;print&quot; 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 {{print}} function as follows. <br> <br>{code} <br>function print(...) <br>   local msg = &quot;&quot; <br>   for i=1,select(&#39;#&#39;, ...) do <br>      if i == 1 then <br>         msg = msg .. tostring(select(i, ...)) <br>      else <br>         msg = msg .. &quot;\t&quot; .. tostring(select(i, ...)) <br>      end <br> <br>   app.verbose(msg) <br>end <br>{code} <br> <br>h2. Splitting Configuration into Multiple Files <br> <br>The {{require}} method can be used to load lua modules or additional files. <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 1.10 an autoservice is automatically started for you by default.</td></tr></table></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 {{channel} 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

   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>
    </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=4&originalVersion=3">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>