3.4. Macros

A macro is a kind of subroutine. It can contain complex workflows but is called through a single entry. This reduces repetition in the dialplan and makes it cleaner and smaller. A simple example might look like this:
[macro-incoming]
exten => s,1,Dial(SIP/${MACRO_EXTEN},10)
exten => s,n,VoiceMail(${MACRO_EXTEN})
Ein solches Macro würde im rest des Dialplanes dann wie folgt aufgerufen werden:
[sales]
exten => _2XXX,1,Macro(incoming)

[building-mgr]
exten => _2XXX,1,Macro(incoming)
The effect is not quite so impressive with a two-line macro as it would be with a much longer macro, but the advantages of such an approach should be clear.
The use of macros tends to divide the Asterisk user community into two groups. One feels that macros make the dialplan easier to understand, the other feels that they make the dialplan confusing. We encourage you to draw your own conclusions!

Macro basics

When defining macros, take care to note the following points:
  • When defining a macro, only one extension - the s extension - is allowed.
  • The original ${EXTEN} and ${CONTEXT} variables cannot be used inside a macro. We must use ${MACRO_EXTEN} and ${MACRO_CONTEXT} instead.
  • When calling a macro, additional comma- (",") or pipe-separated ("|") arguments can be supplied. These arguments are called within the macro with ${ARGn} (where n is a positive integer indicating which argument in the sequence).
  • A macro is defined in square brackets ([macro-macroname]) and is called with the Macro() application in an extension.
More information on macros may be found at Section B.58, “Macro().

Tip

The application MacroExclusive() ensures that the specific macro can only be called once at any given time. If another channel is calling the macro, no other channel can call it until it completes (see Section B.59, “MacroExclusive()).