use macros in asterisk

I was just watching an episode of Systm the other day with a friend… Episode 5, the one where they describe how to build a basic Asterisk server. First, I’ve gotta say, I love that Kevin Rose and the other guys over there have created Systm… it’s a very cool idea, and something that honestly just sounds like a blast to do.

Anyway, while watching it, I remembered back when I was learning asterisk that many would-be mentors simply showed how to create a static definition in extensions.conf for every extension. While that works for a small system, it quickly becomes needlessly complicated when you need to change the way things flow for every extension after that. Just imagine changing something as small as the voicemail context for 50 extensions. It just doesn’t make sense. That’s where macros come in. They make it possible to cover most of the extensions with one script. The issue, I’ve found, is that most example macros just aren’t useful enough. :)

For my users, I want to give them the ability to call their own number in order to go straight to voicemail. That, and the Polycom 501 actually defaults to calling itself when you hit the ‘messages’ button. So we need to create a macro that will perform the normal functions, but will also know when you’re calling yourself.

First, in the main context you’ll need a line like this:

exten => _10XX,1,Macro(sipphone,${EXTEN})

This assumes extensions between 1000 and 1099. Change it to suit your needs. The point of this line is that when someone dials a ‘1000’ extension, it will send the call to the ‘sipphone’ macro, sending the actual extension dialed along with it.

Then, somewhere else in the file, perhaps just above your main context, add:


exten => s,1,GotoIf($[“${ARG1}” = “${CALLERIDNUM}”]?5:2)
exten => s,2,Dial(SIP/${ARG1},20)
exten => s,3,Voicemail(su${ARG1})
exten => s,4,Hangup
exten => s,5,VoicemailMain(${CALLERIDNUM})
exten => s,6,Hangup

So when a call is sent to this macro, ${ARG} would equal the extension dialed, and ${CALLERIDNUM} would be the extension dialing. On the first line of the macro, we’re simply saying “if the caller id and dialed number are equal, send the call to line 5, or voicemail. Otherwise, we follow the normal flow of a macro and ring the extension for 20 seconds, and then send the call to voicemail. Also, notice that if you call yourself, we use the VoicemailMain command instead of the Voicemail command… that sends you in to listen rather than record.

Leave a Reply