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

exten => iguanas,1,Playback(office-iguanas)
                   exten => iguanas,n,Hangup()
               By changing the value assigned to TEST in the first line, you should be able to have your
               Asterisk server play a different greeting.
               Let’s look at another example of conditional branching. This time, we’ll use both
               Goto() and GotoIf() to count down from 10 and then hang up:
                   exten => 123,1,Set(COUNT=10)
                   exten => 123,n(start),GotoIf($[${COUNT} > 0]?:goodbye)
                   exten => 123,n,SayNumber(${COUNT})
                   exten => 123,n,Set(COUNT=$[${COUNT} - 1])
                   exten => 123,n,Goto(start)
                   exten => 123,n(goodbye),Hangup()
               Let’s analyze this example. In the first priority, we set the variable COUNT to 10. 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. If COUNT is less than or equal to 0, control goes to priority label
               goodbye, and the call is hung up.
               The classic example of conditional branching is affectionately known as the anti-girl-
               friend logic. If the Caller ID number of the incoming call matches the phone number
               of the recipient’s ex-girlfriend, Asterisk gives a different message than it ordinarily
               would to any other caller. While somewhat simple and primitive, it’s a good example
               for learning about conditional branching within the Asterisk dialplan.
               This example uses the CALLERID function, which allows us to retrieve the Caller ID
               information on the inbound call. Let’s assume for the sake of this example that the
               victim’s phone number is 888-555-1212:
                   exten => 123,1,GotoIf($[${CALLERID(num)} = 8885551212]?reject:allow)
                   exten => 123,n(allow),Dial(Zap/4)
                   exten => 123,n,Hangup()
                   exten => 123,n(reject),Playback(abandon-all-hope)
                   exten => 123,n,Hangup()
               In priority 1, we call the GotoIf() application. It tells Asterisk to go to priority label
               reject if the Caller ID number matches 8885551212, and otherwise to go to priority label
               allow  (we  could  have  simply  omitted  the  label  name,  causing  the  GotoIf()  to  fall
               through). If the Caller ID number matches, control of the call goes to priority label
               reject, which plays back an uninspiring message to the undesired caller. Otherwise,
               the call attempts to dial the recipient on channel Zap/4.

               Time-Based Conditional Branching with GotoIfTime()

               Another way to use conditional branching in your dialplan is with the GotoIfTime()
               application. Whereas GotoIf() evaluates an expression to decide what to do, GotoIf


                                                                     Conditional Branching | 151
   174   175   176   177   178   179   180   181   182   183   184