<html>
<head>
<base href="https://wiki.asterisk.org/wiki">
<link rel="stylesheet" href="/wiki/s/en/2160/3/7/_/styles/combined.css?spaceKey=AST&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/Interacting+with+Asterisk+from+Lua+%28apps%2C+variables%2C+and+functions%29">Interacting with Asterisk from Lua (apps, variables, and functions)</a></h2>
<h4>Page <b>edited</b> by <a href="https://wiki.asterisk.org/wiki/display/~mnicholson">Matthew Nicholson</a>
</h4>
<br/>
<h4>Changes (0)</h4>
<div id="page-diffs">
<table class="diff" cellpadding="0" cellspacing="0">
<tr><td class="diff-snipped" >...<br></td></tr>
</table>
</div> <h4>Full Content</h4>
<div class="notificationGreySide">
<p>Interaction with is done through a series of predefined objects provided by pbx_lua. The <tt>app</tt> table is used to access dialplan applications. Any asterisk application can be accessed and executed as if it were a function attached to the <tt>app</tt> table. Dialplan variables and functions are accessed and executed via the <tt>channel</tt> table.</p>
<h2><a name="InteractingwithAsteriskfromLua%28apps%2Cvariables%2Candfunctions%29-DialplanApplications"></a>Dialplan Applications</h2>
<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">app.playback("please-hold")
app.dial("SIP/100", nil, "m")</pre>
</div></div>
<p>Any dialplan application can be executed using the <tt>app</tt> table. Application names are case insensitive. Arguments are passed to dialplan applications just as arguments are passed to functions in lua. String arguments must be quoted as they are lua strings. Empty arguments may be passed as <tt>nil</tt> or as empty strings.</p>
<h2><a name="InteractingwithAsteriskfromLua%28apps%2Cvariables%2Candfunctions%29-ChannelVariables"></a>Channel Variables</h2>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Set a Variable</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">channel.my_variable = "my_value"</pre>
</div></div>
<p>After this the channel variable <tt>${my_variable</tt>} contains the value "my_value".</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Read a Variable</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">value = channel.my_variable:get()</pre>
</div></div>
<p>Any channel variable can be read and set using the <tt>channel</tt> table. Local and global lua variables can be used as they normally would and are completely unrelated to channel variables.</p>
<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/wiki/images/icons/emoticons/forbidden.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>The following construct will NOT work.
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">value = channel.my_variable -- does not work as expected (value:get() could be used to get the value after this line)</pre>
</div></div></td></tr></table></div>
<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/wiki/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>If the variable name contains characters that lua considers special use the <tt>[]</tt> operator to access them.
<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"
value = channel["my_variable"]:get()</pre>
</div></div></td></tr></table></div>
<h3><a name="InteractingwithAsteriskfromLua%28apps%2Cvariables%2Candfunctions%29-DialplanFunctions"></a>Dialplan Functions</h3>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Write a Dialplan Function</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">channel.FAXOPT("modems"):set("v17,v27,v29")</pre>
</div></div>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Read a Dialplan Function</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">value = channel.FAXOPT("modems"):get()</pre>
</div></div>
<p>Note the use of the <tt>:</tt> operator with the <tt>get()</tt> and <tt>set()</tt> methods.</p>
<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/wiki/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>If the function name contains characters that lua considers special use the <tt>[]</tt> operator to access them.
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">channel["FAXOPT(modems)"] = "v17,v27,v29"
value = channel["FAXOPT(modems)"]:get()</pre>
</div></div></td></tr></table></div>
<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/wiki/images/icons/emoticons/forbidden.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>The following constructs will NOT work.
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">channel.FAXOPT("modems") = "v17,v27,v29" -- syntax error
value = channel.FAXOPT("modems") -- does not work as expected (value:get() could be used to get the value after this line)</pre>
</div></div></td></tr></table></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>Dialplan function names are case sensitive.</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/Interacting+with+Asterisk+from+Lua+%28apps%2C+variables%2C+and+functions%29">View Online</a>
|
<a href="https://wiki.asterisk.org/wiki/pages/diffpagesbyversion.action?pageId=16548029&revisedVersion=4&originalVersion=3">View Changes</a>
|
<a href="https://wiki.asterisk.org/wiki/display/AST/Interacting+with+Asterisk+from+Lua+%28apps%2C+variables%2C+and+functions%29?showComments=true&showCommentArea=true#addcomment">Add Comment</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>