System information
Since we know how to use dialplan functions now as well, here is an-
other way of controlling which voicemail prompt (unavailable vs. busy)
is played to the caller. In the following example, we’ll be using the
IF() dialplan function:
[macro-voicemail]
exten => s,1,Dial(${ARG1},20)
same => n,VoiceMail(${MACRO_EXTEN},${IF($[${DIALSTATUS} = BUSY]?b:u)})
This macro depends on a nice side effect of the Dial() application: when you use the
Dial() application, it sets the DIALSTATUS variable to indicate whether the call was suc-
cessful or not. In this case, we’re handling the NOANSWER and BUSY cases, and treating all
other result codes as a NOANSWER.
GoSub()
The GoSub() dialplan application is similar to the Macro() application, in that the pur-
pose is to allow you to call a block of dialplan functionality, pass information to that
block, and return from it (optionally with a return value). GoSub() works in a different
manner from Macro(), though, in that it doesn’t have the stack space requirements, so
it nests effectively. Essentially, GoSub() acts like Goto() with a memory of where it
came from.
In this section we’re going to reimplement what we learned in “Macros” on page 204.
If necessary, you might want to review that section: it explains why we might use a
subroutine, and the goal we’re trying to accomplish.
Defining Subroutines
Unlike with Macro(), there are no special naming requirements when using GoSub() in
the dialplan. In fact, you can use GoSub() within the same context and extension if you
want to. In most cases, however, GoSub() is used in a similar fashion to Macro(), so
defining a new context is common. When creating the context, we like to prepend the
name with sub so we know the context is typically called from the GoSub() application
(of course, there is no requirement that you do so, but it seems a sensible convention).
Here is a simple example of how we might define a subroutine in Asterisk:
[subVoicemail]
Let’s take our example from “Macros” on page 204 and convert it to a subroutine. Here
is how it is defined for use with Macro():
[macro-voicemail]
exten => s,1,Dial(${JOHN},10)
same => n,GotoIf($["${DIALSTATUS}" = "BUSY"]?busy:unavail)
same => n(unavail),VoiceMail(101@default,u)
same => n,Hangup()
GoSub() | 207