How To Configure Asterisk: Using Macros
In my last How To Configure Asterisk Article, I showed you how to setup and deploy a basic Asterisk PBX system. In this article, I will demonstrate the power of using Macros in your dial plan.
Macros assist you by significantly reducing the amount of code in your dialplan. Once you define a Macro, they can be called with a single line. You can pass arguments to Macros just like a function call in a programming language.
Lets look at the Macro I defined in my last article:
[macro-phone]
exten => s,1,Dial(SIP/${MACRO_EXTEN},25)
exten => s,n,Goto(${DIALSTATUS},1)
exten => ANSWER,1,Hangup
exten => CANCEL,1,Hangup
exten => NOANSWER,1,Voicemail(${MACRO_EXTEN}@default,u)
exten => BUSY,1,Voicemail(${MACRO_EXTEN}@default,b)
exten => CONGESTION,1,Voicemail(${MACRO_EXTEN}@default,b)
exten => CHANUNAVAIL,1,Voicemail(${MACRO_EXTEN}@default,u)
exten => a,1,VoicemailMain(${MACRO_EXTEN}@default)
Asterisk Macros utilize the ’s’ or starting extension. In this Macro, we start by dialing a SIP based VoIP Endpoint, then the Goto application causes a change in the extension and priority by using a variable. The ${DIALSTATUS} is a variable that gets set inside the Dial application and represents the disposition of the current call, which we utilize the result as the new ‘extension’ to go to.
The DIALSTATUS based extensions determine the next operation (step) that is to be completed for this call, based on what happened in the Dial application. In the example above, we either Hangup or send the call off to the Voicemail application. The ‘a’ extension would get executed if the caller pressed the * (star) DTMF key within the Voicemail application.
Hopefully you can see that Macros are a very powerful addition to your Asterisk dialplan. Lets dig a little deeper. I will show you another way Macro’s can be utilized within Asterisk.
One can create a macro to deploy a simple, but effective Interactive Response System (IVR), without complicating your PBX with Agents or Queues.
[macro-main-greeting]
exten => s,1,GotoIfTime(9:00-19:30|mon-sat|*|*?open,1)
exten => s,2,Voicemail(u${ARG2}@default)
exten => s,3,Hangup
exten => open,1,Dial(${ARG1}|25|m)
exten => open,n,Goto(${DIALSTATUS},1)
exten => ANSWER,1,Hangup
exten => CANCEL,1,Hangup
exten => NOANSWER,1,Voicemail(u${ARG2}@default)
exten => BUSY,1,Voicemail(b${ARG2}@default)
exten => CONGESTION,1,Voicemail(b${ARG2}@default)
exten => CHANUNAVAIL,1,Voicemail(u${ARG2}@default)
exten => a,1,VoicemailMain(${ARG2}@default)
[from-pstn]
exten => _X.,1,Answer
exten => _X.,2,Set(count=0)
exten => _X.,3,Wait,2
exten => _X.,4,Background,custom/main-greeting
exten => _X.,n,Background,custom/2-silence
exten => _X.,n,Set(count=$[${count} + 1])
exten => _X.,n,GotoIf($[${count} >= 3]?hangup)
exten => _X.,n,Goto(${EXTEN},3)
exten => _X.,n(hangup),Playback,vm-goodbye
exten => _X.,n(hangup),Hangup
exten => 1,1,Macro(main-greeting,SIP/100,100)
exten => 2,1,Macro(main-greeting,SIP/200,200)
exten => 3,1,Macro(main-greeting,Zap/g2/12487249999,300)
I am sure you have noticed the main-greeting Macro is very similar to our first macro, with a simple, but very powerful tweak. This time we utilize GotoIfTime, which checks the current time. If the condition checks to be true the call will get sent to the ‘open’ extension, which sends the call out to the SIP endpoint and does the DIALSTATUS condition processing.
The from-pstn context is where we glue everything together. We answer the call, set a variable, play the greeting and listen for a digit to be pressed, along with making a decision about how many times to play the prompt before giving up — hearing no DTMF.
In this case our main-greeting could be, “Thank you for calling XYZ Corp. Press 1 for Sales, Press 2 for Accounting or Press 3 for Support”
When someone pressed 1 or 2, Asterisk calls the main-greeting macro with the SIP device and the voicemail box number. Unlike our previous Macro, this time we are not assuming that we will always be using SIP.
Lets say we had a Digium FXO Card. We could send calls out via the Zaptel Channel Driver. Remember, Asterisk seamlessly combines channel technology types, allowing you to create a seriously powerful PBX platform with very little cost.
Note: To put this sort of IVR system in to operation, I have found that you must disable Call Waiting on your SIP devices. This way calls will cleanly make it into Voicemail when a call is already active, without any interruptions.
I hope this provides you additional insight as to how you can use Macros to save a lot of time, energy and limit the amount of duplicated configuration, which can cause bugs and frustration.
|
Related Posts:
VoIP Blog
How To Configure Asterisk: The Series
Run Your Own SIP Server, Today!
Tell Me What You Really Think About NuFone
UPDATED: SunRocket Closes Without Notice….Asterisk To The Rescue!
