<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/Dialplan+to+Lua+Reference">Dialplan to Lua Reference</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" >The {{app.goto()}} function will not work as expected in pbx_lua in Asterisk 1.8. If you must use {{app.goto()}} you must manually return control back to asterisk using {{return}} from the dialplan extension function, otherwise execution will continue after the call to {{app.goto()}}. Calls to {{app.goto()}} should work as expected 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> but still should not be necessary in most cases. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code:title=In Asterisk 1.8, use return} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Below is a quick reference that can be used to translate traditional <tt>extensions.conf</tt> dialplan concepts to their analog in <tt>extensions.lua</tt>.</p>

<div>
<ul>
    <li><a href='#DialplantoLuaReference-ExtensionPatterns'>Extension Patterns</a></li>
    <li><a href='#DialplantoLuaReference-ContextIncludes'>Context Includes</a></li>
    <li><a href='#DialplantoLuaReference-Loops'>Loops</a></li>
    <li><a href='#DialplantoLuaReference-Variables'>Variables</a></li>
    <li><a href='#DialplantoLuaReference-Applications'>Applications</a></li>
    <li><a href='#DialplantoLuaReference-Macros%2FGoSub'>Macros/GoSub</a></li>
    <li><a href='#DialplantoLuaReference-Goto'>Goto</a></li>
</ul></div>

<h2><a name="DialplantoLuaReference-ExtensionPatterns"></a>Extension Patterns</h2>

<p>Extension pattern matching syntax on logic works the same for <tt>extensions.conf</tt> and <tt>extensions.lua</tt>.</p>

<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr>
<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.conf"></a>extensions.conf</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">[users]
exten =&gt; _1XX,1,Dial(SIP/${EXTEN})

exten =&gt; _2XX,1,Voicemail(${EXTEN:1})</pre>
</div></div>
</div></div></td>

<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.lua"></a>extensions.lua</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">extensions = {}
extensions.users = {}

extensions.users["_1XX"] = function(c, e)
   app.dial("SIP/" .. e)
end

extensions.users["_2XX"] = function(c, e)
  app.voicemail("1" .. e:sub(2))
end</pre>
</div></div>
</div></div></td></tr></tbody></table> 

<h2><a name="DialplantoLuaReference-ContextIncludes"></a>Context Includes</h2>
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr>
<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.conf"></a>extensions.conf</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">[users]
exten =&gt; 100,1,Noop
exten =&gt; 100,n,Dial("SIP/100")

[demo]
exten =&gt; s,1,Noop
exten =&gt; s,n,Playback(demo-congrats)

[default]
include =&gt; demo
include =&gt; users</pre>
</div></div>
</div></div></td>


<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.lua"></a>extensions.lua</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">extensions = {
   users = {
      [100] = function()
         app.dial("SIP/100")
      end;
   };

   demo = {
      ["s"] = function()
         app.playback(demo-congrats)
      end;
   };
      
   default = {
      include = {"demo", "users"};
   };
}</pre>
</div></div>
</div></div></td></tr></tbody></table> 

<h2><a name="DialplantoLuaReference-Loops"></a>Loops</h2>
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr>
<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.conf"></a>extensions.conf</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">exten =&gt; 100,1,Noop
exten =&gt; 100,n,Set(i=0)
exten =&gt; 100,n,While($[i &lt; 10])
exten =&gt; 100,n,Verbose(i = ${i})
exten =&gt; 100,n,EndWhile</pre>
</div></div>
</div></div></td>

<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.lua"></a>extensions.lua</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">i = 0
while i &lt; 10 do
  app.verbose("i = " .. i)
end</pre>
</div></div>
</div></div></td></tr></tbody></table>

<h2><a name="DialplantoLuaReference-Variables"></a>Variables</h2>
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr>
<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.conf"></a>extensions.conf</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">exten =&gt; 100,1,Set(my_variable=my_value)
exten =&gt; 100,n,Verbose(my_variable = ${my_variable})</pre>
</div></div>
</div></div></td>

<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.lua"></a>extensions.lua</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">channel.my_variable = "my_value"
app.verbose("my_variable = " .. channel.my_variable:get())</pre>
</div></div>
</div></div></td></tr></tbody></table>

<h2><a name="DialplantoLuaReference-Applications"></a>Applications</h2>
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr>
<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.conf"></a>extensions.conf</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">exten =&gt; 100,1,Dial("SIP/100",,m)</pre>
</div></div>
</div></div></td>

<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.lua"></a>extensions.lua</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">app.dial("SIP/100", nil, "m")</pre>
</div></div>
</div></div></td></tr></tbody></table>

<h2><a name="DialplantoLuaReference-Macros%2FGoSub"></a>Macros/GoSub</h2>

<p><em>Macros can be defined in pbx_lua by naming a context 'macro-*' just as in <tt>extensions.conf</tt>, but generally where you would use macros or gosub in <tt>extensions.conf</tt> you would simply use a function in lua.</em></p>

<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr>
<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.conf"></a>extensions.conf</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">[macro-dial]
exten =&gt; s,1,Noop
exten =&gt; s,n,Dial(${ARG1})

[default]
exten =&gt; 100,1,Macro(dial,SIP/100)</pre>
</div></div>
</div></div></td>

<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.lua"></a>extensions.lua</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">extensions = {}
extensions.default = {}

function dial(resource)
   app.dial(resource)
end

extensions.default[100] = function()
   dial("SIP/100")
end</pre>
</div></div>
</div></div></td></tr></tbody></table>

<h2><a name="DialplantoLuaReference-Goto"></a>Goto</h2>
<p><em>While <tt>Goto</tt> is an extenstions.conf staple, it should generally be avoided in pbx_lua in favor of functions.</em></p>

<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr>
<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.conf"></a>extensions.conf</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">[default]
exten =&gt; 100,1,Goto(102,1)

exten =&gt; 102,1,Playback("demo-thanks")
exten =&gt; 102,n,Hangup</pre>
</div></div>
</div></div></td>

<td class="confluenceTd" valign="top" width="50%">
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<h2><a name="DialplantoLuaReference-extensions.lua"></a>extensions.lua</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">extensions = {}
extensions.default = {}

function do_hangup()
   app.playback("demo-thanks")
   app.hangup()
end

extensions.default[100] = function()
   do_hangup()
end</pre>
</div></div>
</div></div></td></tr></tbody></table>

<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>The <tt>app.goto()</tt> function will not work as expected in pbx_lua in Asterisk 1.8. If you must use <tt>app.goto()</tt> you must manually return control back to asterisk using <tt>return</tt> from the dialplan extension function, otherwise execution will continue after the call to <tt>app.goto()</tt>. Calls to <tt>app.goto()</tt> should work as expected in Asterisk 10 but still should not be necessary in most cases.

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>In Asterisk 1.8, use return</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">function extension_function(c, e)
   return app.goto("default", "100", 1)

   -- without that 'return' the rest of the function would execute normally
   app.verbose("Did you forget to use 'return'?")
end</pre>
</div></div></td></tr></table></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/Dialplan+to+Lua+Reference">View Online</a>
        |
        <a href="https://wiki.asterisk.org/wiki/pages/diffpagesbyversion.action?pageId=16548022&revisedVersion=7&originalVersion=6">View Changes</a>
                |
        <a href="https://wiki.asterisk.org/wiki/display/AST/Dialplan+to+Lua+Reference?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>