Conditional Branching
Now that you’ve learned a bit about expressions and functions, it’s time to put them to use. By using expressions and functions, you can add even more advanced logic to your dialplan. To allow your dialplan to make decisions, you’ll use conditional branching. Let’s take a closer look.
The GotoIf() Application
The key to conditional branching is the GotoIf() application. GotoIf()
evaluates an expression and sends the caller to a specific destination
based on whether the expression evaluates to true or false.
GotoIf() uses a special
syntax, often called the conditional
syntax:
GotoIf(expression?destination1:destination2)
If the expression evaluates to true, the caller is sent to
destination1. If the expression evaluates
to false, the caller is sent to the second destination. So, what is
true and what is false? An empty string and the number 0 evaluate as
false. Anything else evaluates as true.
The destinations can each be one of the following:
A priority label within the same extension, such as
weaselsAn extension and a priority label within the same context, such as
123,weaselsA context, extension, and priority label, such as
incoming,123,weasels
Either of the destinations may be omitted, but not both. If the omitted destination is to be followed, Asterisk simply goes on to the next priority in the current extension.
Let’s use GotoIf() in an
example:
exten => 345,1,Set(TEST=1)
exten => 345,n,GotoIf($[${TEST} = 1]?weasels:iguanas)
exten => 345,n(weasels),Playback(weasels-eaten-phonesys) ...