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 the first destination. 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 within the same extension, such as
10
An extension and a priority within the same context, such as
123,10
A context, extension, and priority, such as
incoming,123,10
A named priority within the same extension, such as
passed
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,2,GotoIf($[${TEST} = 1]?10:20) exten => ...
Get Asterisk: The Future of Telephony now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.