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

Defining Macros
               Let’s take the dialplan logic we used above to set up voicemail for John and turn it into
               a macro. Then we’ll use the macro to give John and Jane (and the rest of their coworkers)
               the same functionality.

               Macro definitions look a lot like contexts. (In fact, you could argue that they really are
               small, limited contexts.) You define a macro by placing macro- and the name of your
               macro in square brackets, like this:
                   [macro-voicemail]
               Macro names must start with macro-. This distinguishes them from regular contexts.
               The commands within the macro are built almost identically to anything else in the
               dialplan; the only limiting factor is that macros use only the s extension. Let’s add our
               voicemail logic to the macro, changing the extension to s as we go:
                   [macro-voicemail]
                   exten => s,1,Dial(${JOHN},10)
                   exten => s,n,GotoIf($["${DIALSTATUS}" = "BUSY"]?busy:unavail)
                   exten => s,n(unavail),Voicemail(101@default,u)
                   exten => s,n,Hangup()
                   exten => s,n(busy),VoiceMail(101@default,b)
                   exten => s,n,Hangup()
               That’s a start, but it’s not perfect, as it’s still specific to John and his mailbox number.
               To make the macro generic so that it will work not only for John but also for all of his
               coworkers, we’ll take advantage of another property of macros: arguments. But first,
               let’s see how we call macros in our dialplan.


               Calling Macros from the Dialplan
               To use a macro in our dialplan, we use the Macro() application. This application calls
               the specified macro and passes it any arguments. For example, to call our voicemail
               macro from our dialplan, we can do the following:

                   exten => 101,1,Macro(voicemail)
               The Macro() application also defines several special variables for our use. They include:
               ${MACRO_CONTEXT}
                   The original context in which the macro was called.
               ${MACRO_EXTEN}
                   The original extension in which the macro was called.
               ${MACRO_PRIORITY}
                   The original priority in which the macro was called.
               ${ARG n }
                   The nth argument passed to the macro. For example, the first argument would be
                   ${ARG1}, the second ${ARG2}, and so on.



               158 | Chapter 6: More Dialplan Concepts
   181   182   183   184   185   186   187   188   189   190   191