1. 变量设置
使用 Set() 来创建和修改变量:
exten => 1002,1,Set(Favoriteanimal = "Tiger") exten => 1002,n,Set(Favoritenumber = 23)
使用 ${VARIABLENAME}
来读取和打印变量值. 可以在CLI界面打印变量名,通过NoOp()函数。 (verbosity level >=3 ):
exten => 1003,1,NoOp(${Favoriteanimal}) exten => 1003,n,NoOp(${Favoritenumber})
2.几种变量类型
2.1全局变量,在Dialplan的所有条目中均有效,其设置为:<span style="font-family:Courier New;">Set(<variable>=<content>,g)</span>
<span style="font-family:Courier New;"> exten => 1004,1,Set(READABLEANYWHERE = 23,g) exten => 1004,n,NoOp(${READABLEANYWHERE})</span>
<span style="font-family:Courier New;"> 2.2通道(channel)变量,只在当前通道中有效,<span style="font-family:Verdana;">其设置为:</span><span style="font-family:Courier New;">Set(<variable>=<content>)</span></span>
<span style="font-family:Courier New;"> e</span><span style="font-family:Courier New;">xten => 1005,1,Set(READABLEHEREONLY= 42) exten => 1005,n,NoOp(${READABLEHEREONLY})</span>
<span style="font-family:Courier New;"> 2.3系统变量,是指Asterisk预先定义的变量,可以在Dialplan中直接使用,其中的EXTEN表示当前被叫号码</span>
<span style="font-family:Courier New;"> exten => 1006,1,NoOp(Dialed number: ${EXTEN})</span>
<span style="font-family:Courier New;"> 对于如下的语句:</span>
<span style="font-family:Courier New;"> exten => 100,1,Dial(SIP/100) exten => 101,1,Dial(SIP/101) exten => 102,1,Dial(SIP/102) exten => 103,1,Dial(SIP/103) exten => 104,1,Dial(SIP/104) exten => 105,1,Dial(SIP/105) exten => 106,1,Dial(SIP/106) exten => 107,1,Dial(SIP/107) exten => 108,1,Dial(SIP/108) exten => 109,1,Dial(SIP/109) 可以用变量一句话就可以搞定了:</span>
<span style="font-family:Courier New;"> exten => _10X,1,Dial(SIP/${EXTEN})</span>
<span style="font-family:Courier New;">对于字符串变量,定义时最好用双引号括起来:</span>
<span style="font-family:Courier New;"> exten => 1234,1,Set(FRUITTYPES="Apple, Pear, etc.")</span>
<span style="font-family:Courier New;">保留的字符:[ ] $ " /</span>
<span style="font-family:Courier New;"> 如果要使用保留字符的时候,使用/前置,如:</span>
<span style="font-family:Courier New;"> exten => 1234,1,Set(AMOUNT="/$10.00")</span>
<span style="font-family:Courier New;">在extensions.conf中定义全局变量</span>
<span style="font-family:Courier New;"> 全局变量的定义必须放在[globals]中,[globals]放在[general]后,如下例所示:</span>
<span style="font-family:Courier New;"> [general] [globals] RINGTIME=90 [from-intern] exten => _XXX,1,Dial(SIP/${EXTEN},${RINGTIME}) exten => _XXX,n,VoiceMail(${EXTEN}) </span>
<span style="font-family:Courier New;">Set()函数的格式:Set(<variable1>=<value1>[,<variable2>=<value2>][,<option>]) </span>
<span style="font-family:Courier New;"> ; Set a global variable: exten => 10,1,Set(RINGTIME=90,g) ; Set a local channel variable: exten => 10,2,Set(FAVORITEFRUIT="Apple") ; Set two channel variables at once: exten => 10,3,Set(VAR1=10,VAR2=23) ; Print variables to the CLI exten => 10,4,NoOp(RINGTIME = ${RINGTIME}) exten => 10,5,NoOp(FAVORITEFRUIT = ${FAVORITEFRUIT}) exten => 10,6,NoOp(VAR1 = ${VAR1}) exten => 10,7,NoOp(VAR2 = ${VAR2})</span>
<span style="font-family:Courier New;">系统定义的一些变量:</span>
<span style="font-family:Courier New;"> </span><dt><span class="term"><code class="code">${ANSWEREDTIME}</code> :通话时长(以秒为单位)</span></dt>
${BLINDTRANSFER}
:盲转的通道名称(The name of the channel on the other side of a blind transfer.)
<dt><span class="term"><code class="code">${CHANNEL}</code> :当前通道名称</span></dt><dt> </dt><dt><span class="term"><code class="code">${CONTEXT}</code> :当前的上下文</span></dt>
${EPOCH}
:当前的Unix时间(从Unix“epoch"运行开始的总秒数,1970,1,1,0点)
<dt><span class="term"><code class="code">${EXTEN}</code> :当前的被叫号码</span></dt><dd><p> </p></dd><dt><span class="term"><code class="code">${ENV(VARIABLENAME)}</code> :系统环境变量VARIABLENAME的值</span></dt>
${HANGUPCAUSE}
:拆线的原因
${INVALID_EXTEN}
:Used in the i
extension and contains the dialed extension.
<dt><span class="term"><code class="code">${PRIORITY}</code> :当前分机的优先值(</span>Current priority in the current extension.)</dt><dt> </dt><dt><span class="term"><code class="code">${TRANSFER_CONTEXT}</code> :被转移的呼叫的上下文(</span>Context of a transferred call.)</dt><dt> </dt><dt><span class="term"><code class="code">${UNIQUEID}</code> :当前连接的唯一ID值(</span>The unique ID for the current connection.)</dt><dt> </dt><dt><span class="term"><code class="code">${SYSTEMNAME}</code> :定义在<span style="font-family:Courier New;">/etc/asterisk/asterisk.conf中的systemname值</span></span></dt>
VARIABLENAME
[:start
[:length
]]}<dd>例子1:如果用户拨9出局引示号,然后拨被叫号码,9不应该发出局。</dd><dd>exten => _0X.,1,Set(OUTGOINGNUMBER=${EXTEN:1}) //此时OUTGOINGNUMBER=实际的被叫号码</dd><dd>例子2:如果只需被叫号码的后7位的话,那么应该如此:</dd><dd>exten => _0X.,1,Set(LOCALNUMBER=${EXTEN:-7})</dd><dd>例子3:截取被叫号码从第2位开始,3位号码长度:</dd><dd>exten => _0X.,1,Set(AREACODE=${EXTEN:2:3})</dd>
3. Label 和 Goto()语句
<p>在一个条目中的跳转:</p><pre name="code" class="programlisting"> exten => 1007,1,Answer() exten => 1007,n(Start),Wait(1) exten => 1007,n,Playback(hello-world) exten => 1007,n,Goto(Start)
在两个条目中的跳转:
exten => 1008,1,Answer() exten => 1008,n,Goto(1009,Ping) exten => 1009,1(Ping),Playback(hello-world) exten => 1009,n,Wait(2) exten => 1009,n,Goto(1010,Pong) exten => 1010,1(Pong),Playback(tt-weasels) exten => 1010,n,Wait(2) exten => 1010,n,Goto(1009,Ping)
在 contexts 之间跳转:
[hq] exten => 1011,1,Answer() exten => 1011,n,Playback(hello-world) exten => 1011,n,Goto(sales,1012,1) [sales] exten => 1012,1,Playback(hello-world) exten => 1012,n,Hangup()
4. While()循环
exten => 1013,1,Answer() exten => 1013,n,Set(i=1) exten => 1013,n,While($[${i} < 10]) exten => 1013,n,SayNumber(${i}) exten => 1013,n,Wait(1) exten => 1013,n,Set(i=$[${i} + 1]) exten => 1013,n,EndWhile() exten => 1013,n,Hangup()
5. GotoIf()条件判断
exten => 1014,1,Answer() exten => 1014,n,Set(Favoritestation = 0815) exten => 1014,n,NoOp(Check to see if ${Favoritestation} is calling.) exten => 1014,n,GotoIf($[${CALLERID(num)} = ${Favoritestation}]?yes,no) exten => 1014,n(yes),Playback(hello-world) exten => 1014,n,Hangup() exten => 1014,n(no),Playback(tt-monkeys) exten => 1014,n,Hangup()
6. Gosub()子程序
exten => 1015,1,Gosub(cid-set) // 1 <------------ exten => 1015,n,Dial(SIP/${EXTEN}) //4 <----------- exten => 1015,n(cid-set),Set(CALLERID(all)=Apfelmus GmbH <012345678>) //2 <------ exten => 1015,n,Return() //3 <---------