Page 177 - Asterisk™: The Future of Telephony
P. 177

exten => 123,1,Set(TEST=example)
                   exten => 123,n,SayNumber(${LEN(${TEST})})
               The above example would evaluate the string example as having seven characters, assign
               the number of characters to the variable length, and then speak the number to the user
               with the SayNumber() application.
               Let’s look at another simple example. If we wanted to set one of the various channel
               timeouts, we could use the TIMEOUT() function. The TIMEOUT() function accepts one of
               three  arguments:  absolute,  digit,  and  response.  To  set  the  digit  timeout  with  the
               TIMEOUT() function, we could use the Set() application, like so:
                   exten => s,1,Set(TIMEOUT(digit)=30)
               Notice the lack of ${ } surrounding the function. Just as if we were assigning a value
               to a variable, we assign a value to a function without the use of the ${ } encapsulation.

               A complete list of available functions can be found by typing core show functions at
               the Asterisk command-line interface. You can also look them up in Appendix F.

               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 expres-
               sion 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 weasels
                 • An extension and a priority label within the same context, such as 123,weasels
                 • A context, extension, and priority label, such as incoming,123,weasels





                                                                     Conditional Branching | 149
   172   173   174   175   176   177   178   179   180   181   182