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

exten => 8,1,Directory(default,incoming,f)
                   exten => 9,1,Directory(default,incoming)
               If callers press 8, they’ll get a directory by first name. If they dial 9, they’ll get the
               directory by last name.

               Macros

                      ‖
               Macros  are a very useful construct designed to avoid repetition in the dialplan. They
               also help in making changes to the dialplan. To illustrate this point, let’s look at our
               sample dialplan again. If you remember the changes we made for voicemail, we ended
               up with the following for John’s extension:
                   exten => 101,1,Dial(${JOHN},10)
                   exten => 101,n,GotoIf($["${DIALSTATUS}" = "BUSY"]?busy:unavail)
                   exten => 101,n(unavail),Voicemail(101@default,u)
                   exten => 101,n,Hangup()
                   exten => 101,n(busy),VoiceMail(101@default,b)
                   exten => 101,n,Hangup()
               Now imagine you have a hundred users on your Asterisk system—setting up the ex-
               tensions would involve a lot of copying and pasting. Then imagine that you need to
               make a change to the way your extensions work. That would involve a lot of editing,
               and you’d be almost certain to have errors.

               Instead, you can define a macro that contains a list of steps to take, and then have all
               of the phone extensions refer to that macro. All you need to change is the macro, and
               everything in the dialplan that references that macro will change as well.


                           If you’re familiar with computer programming, you’ll recognize that
                           macros are similar to subroutines in many modern programming lan-
                           guages. If you’re not familiar with computer programming, don’t worry
                           —we’ll walk you through creating a macro.

               The best way to appreciate macros is to see one in action, so let’s move right along.








               ‖ Although Macro seems like a general-purpose dialplan subroutine, it has a stack overflow problem that means
                 you should not try to nest Macro calls more than five levels deep. As of this writing, we do not know whether
                 the Macro application will be patched for 1.4, or if it will be rewritten for future versions. If you plan to do a
                 lot of macros within macros (and call complex functions within them), you may run into stability problems.
                 You will know you have a problem with just one test call, so if your dialplan tests out, you’re good to go. We
                 also recommend that you take a look at the Gosub and Return applications, as a lot of macro functionality can
                 be implemented without actually using Macro(). Also, please note that we are not suggesting that you don’t
                 use Macro(). It is fantastic and works very well; it just doesn’t nest efficiently.

                                                                              Macros | 157
   180   181   182   183   184   185   186   187   188   189   190