text
stringlengths
0
152
exten => 103,1,Dial(${UserB_SoftPhone})
same => n,Hangup()
We’ve only provided two users with actual, working voicemail, and we’ve only defined
four phones as extensions, and yet we’ve already got a mess of repetitive code, which
is only going to get more and more difficult to maintain and expand. This will
quickly become unmanageable if we don’t find a better way.
Let’s write a subroutine to handle dialing our users. Add the following to the very
bottom of your dialplan:
; SUBROUTINES
[subDialUser]
exten => _[0-9].,1,Noop(Dial extension ${EXTEN},channel: ${ARG1}, mailbox: ${ARG2})
same => n,Noop(mboxcontext: ${ARG3}, timeout ${ARG4})
same => n,Dial(${ARG1},${ARG4})
same => n,GotoIf($["${DIALSTATUS}" = "BUSY"]?busy:unavail)
same => n(unavail),VoiceMail(${ARG2}@${ARG3},u)
same => n,Hangup()
same => n(busy),VoiceMail(${ARG2}@${ARG3},b)
same => n,Hangup()
Now, modify the top of your dialplan as follows:
[OLD_sets] ; what was [sets] is now [OLD_sets] (call it whatever, so long as name changes)
exten => 100,1,Dial(${UserA_DeskPhone},12)
same => n,Voicemail(100@default)
same => n,GotoIf($["${DIALSTATUS}" = "BUSY"]?busy:unavail)
;(etc)
We’ve renamed our [sets] context, which of course breaks our dialplan since our
phones enter the dialplan there. So, we’re going to reinsert it a little farther down:
exten => 103,1,Dial(${UserB_SoftPhone})
same => n,Hangup()
[sets]
exten => 110,1,Dial(${UserA_DeskPhone}&${UserA_SoftPhone}&${UserB_SoftPhone})
same => n,Hangup()
;(etc)
OK, so now we’ve got our [sets] context working again, and also this [OLD_sets]
context that’s got our old, orphaned code. How do we dial our telephones? How does
this subroutine we just wrote help us?
exten => 103,1,Dial(${UserB_SoftPhone})
same => n,Hangup()
[sets]
;subDialUser args:
176
|
Chapter 10: Deeper into the Dialplan
; - ARG1 Channel(s) to dial
; - ARG2 Mailbox
; - ARG3 Mailbox Context
; - ARG4 Timeout
exten => 100,1,Gosub(subDialUser,${EXTEN},1(${UserA_DeskPhone},${EXTEN},default,12))
exten => 101,1,Gosub(subDialUser,${EXTEN},1(${UserA_SoftPhone},${EXTEN},default,3))
exten => 102,1,Gosub(subDialUser,${EXTEN},1(${UserB_DeskPhone},${EXTEN},default,6))
exten => 103,1,Gosub(subDialUser,${EXTEN},1(${UserB_SoftPhone},${EXTEN},default,24))
exten => 110,1,Dial(${UserA_DeskPhone}&${UserA_SoftPhone}&${UserB_SoftPhone})
same => n,Hangup()
Plug that in, reload your dialplan, and make some test calls. Play with the parameters
and see what changes. Add some mailboxes to your database and see what that does.
If you’re inspired, write a new subDialUserNEW subroutine and see what you can
come up with. At this point you can also delete all the code in the [OLD_sets] con‐
text, since it’s now abandoned, but you can also leave it there as it does no harm.
Now, you can add hundreds of extensions, and each one will only use one line of the
dialplan.
Whenever you find yourself writing duplicate dialplan code somewhere, stop. It’s very
likely that it’s time to write a subroutine.
Returning from a Subroutine
The GoSub() dialplan application does not return automatically once it is done exe‐
cuting. If you’re done with the call, you can of course use Hangup(); however, if you
don’t want to disconnect, but rather need to return the call from where it came, you
can use the Return() application.
Since you can nest subroutines within subroutines and also execute them one after
another, as you get into more complex subroutines you will find this an essential
capability.
Local Channels
Local channels are a method of executing other areas of the dialplan from the Dial()
application (as opposed to sending the call out a channel). Think of them as subrou‐
tines you can call from within Dial().