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

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)
                   exten => 345,n,Hangup()
                   exten => 345,n(iguanas),Playback(office-iguanas)
                   exten => 345,n,Hangup()

                           You will notice that we have used the Hangup() application following
                           each 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 exten-
                           sions broken up in to multiple components (protected from each other
                           by the Hangup() command), each one acting as steps executed following
                           a GotoIf().


                                 Providing Only a False Conditional Path

                  If we wanted to, we could have crafted the preceding example like this:
                   exten => 345,1,Set(TEST=1)
                   exten => 345,n,GotoIf($[${TEST} = 1]?:iguanas) ; we don't have the weasels label anymore,
                   ; but this will still work
                   exten => 345,n,Playback(weasels-eaten-phonesys)
                   exten => 345,n,Hangup()
                   exten => 345,n(iguanas),Playback(office-iguanas)
                   exten => 345,n,Hangup()
                  There is nothing between the ? and the :, so if the statement evaluates to true, execution
                  of the dialplan will continue at the next step. Since that is what we want, a label is not
                  needed.
                  We don’t really recommend doing this, because this is hard to read, but you will see
                  dialplans like this, so it’s good to be aware that this syntax is totally correct.


               Typically when you have this type of layout where you end up wanting to limit Asterisk
               from falling through to the next priority after you’ve performed that jump, it’s probably
               better to jump to separate extensions instead of priority labels. If anything, it makes it
               a bit more clear when reading the dialplan. We could rewrite the previous bit of dialplan
               like this:

                   exten => 345,1,Set(TEST=1)
                   exten => 345,n,GotoIf($[${TEST} = 1]?weasels,1:iguanas,1); now we're going to
                   ; extension,priority
                   exten => weasels,1,Playback(weasels-eaten-phonesys); this is NOT a label.
                   ; It is a different extension
                   exten => weasels,n,Hangup()

               150 | Chapter 6: More Dialplan Concepts
   173   174   175   176   177   178   179   180   181   182   183