text
stringlengths
0
152
|
Chapter 10: Deeper into the Dialplan
same => n,Hangup()
same => n(iguanas),Playback(office-iguanas) ; STILL THE SAME EXTENSION
same => n,Hangup()
You will notice that we have used the Hangup() application follow‐
ing each use of the Playback() application. This is done so that
when we jump to the weasels label, the call stops before execution
gets to the office-iguanas sound file. It is becoming increasingly
common to see extensions broken up into multiple components
(protected from each other by the Hangup() command), each one a
distinct sequence of steps executed following a GotoIf().
Providing Only a False Conditional Path
Either of the destinations may be omitted (but not both). If an expression evaluates to
a blank destination, Asterisk simply goes on to the next priority in the current
extension.
We could have crafted the preceding example like this:
exten => 209,1,Noop(Test use of conditional branching)
same => n,GotoIf($[ ${RAND(0,1)} = 1 ]?:iguanas)
same => n,Playback(weasels-eaten-phonesys) ; No weasels label anymore
same => n,Hangup()
same => n(iguanas),Playback(office-iguanas) ; NOTE THIS IS THE SAME EXTEN
same => n,Hangup()
There’s nothing between the ? and the : so if the statement evaluates to true, execu‐
tion will continue at the next step. Since that’s what we want, a label isn’t needed.
We don’t really recommend doing this, because it’s hard to read. Nevertheless, you
will see dialplans like this, so it’s good to be aware that this syntax is technically
correct.
Rather than using labels, we could also send the call to different extensions. Since
they’re not dialable, we can use alphabet characters rather than digits for the exten‐
sion “numbers.” In this example, the conditional branch sends the call to completely
different extensions within the same context. The result is otherwise the same.
exten => 210,1,Noop(Test use of conditional branching to extensions)
same => n,GotoIf($[ ${RAND(0,1)} = 1 ]?weasels,1:iguanas,1)
exten => weasels,1,Playback(weasels-eaten-phonesys) ; DIFFERENT EXTENSION
same => n,Hangup()
exten => iguanas,1,Playback(office-iguanas) ; ALSO A DIFFERENT EXTEN
same => n,Hangup()
Conditional Branching
|
169
Let’s look at another example of conditional branching. This time, we’ll use both
Goto() and GotoIf() to count down from 5 and then hang up:
exten => 211,1,NoOp()
same => n,Answer()
same => n,Set(COUNT=5)
same => n(start),GotoIf($[ ${COUNT} > 0 ]?:goodbye)
same => n,SayNumber(${COUNT})
same => n,Set(COUNT=$[ ${COUNT} - 1 ])
same => n,Goto(start)
same => n(goodbye),Playback(vm-goodbye)
same => n,Hangup()
Let’s analyze this example. In the second priority, we set the variable COUNT to 5. Next,
we check to see if COUNT is greater than 0. If it is, we move on to the next priority.
(Don’t forget that if we omit a destination in the GotoIf() application, control goes to
the next priority.) From there, we speak the number, subtract 1 from COUNT, and go
back to priority label start. Again, if COUNT is less than or equal to 0, control goes to
priority label goodbye; otherwise, we run through the loop one more time.
Quoting and Prefixing Variables in Conditional Branches
Now is a good time to take a moment to look at some nitpicky stuff with conditional
branches. In Asterisk, it is invalid to have a null value on either side of the compari‐
son operator. Let’s look at examples that would produce an error:
$[ = 0 ]
$[ foo = ]
$[ > 0 ]
$[ 1 + ]
Any of our examples would produce a warning like this:
WARNING[28400][C-000000eb]: ast_expr2.fl:470 ast_yyerror: ast_yyerror():
syntax error: syntax error, unexpected '=', expecting $end; Input:
= 0
^