text
stringlengths
0
152
186
|
Chapter 10: Deeper into the Dialplan
The IF() function allows us to pass logic to the Playback() application. We’re effec‐
tively saying, “If it’s true that the time, in seconds, is odd, play the hear-odd-noise
prompt, otherwise, play the good-evening prompt.”
If we line up the code in a more typical fashion, it looks like this (note that some of
the optional spaces have also been removed):
exten => 228,1,Noop(IF)
same => n,Answer()
same => n,Wait(.5)
same => n,Noop(${STRFTIME(,,%S)})
same => n,Noop($[${STRFTIME(,,%S)} % 2])
same => n,Noop(${IF($[$[${STRFTIME(,,%S)} % 2 ] = 1]?odd:even)})
same => n,Playback(${IF($[$[${STRFTIME(,,%S)} % 2 ] = 1]?hear-odd-noise:good-evening)})
The final line is very difficult to read unless you know how we got there, but it dem‐
onstrates the power of nesting.
At first these constructs may seem difficult to write, so break them down and perform
them line by line, and eventually they’ll get easier (and your dialplan will subse‐
quently become more powerful). Play with them.
LEN()
Being able to return the length of something with the LEN() function can be very
handy.
exten => 229,1,Noop(LEN)
same => n,Set(LengthyString=${RAND(1,2000)})
same => n,Noop(${LEN(${LengthyString})})
same => n,Noop(${IF( $[ ${LEN(${LengthyString})} <= 3 ]?tooshort:youcanride)})
REGEX()
Yes, you can use regular expressions within Asterisk. This is a somewhat advanced
topic, not because REGEX() is a complicated function in itself, but because regular
expressions are a study in themselves.
Check out http://www.regular-expressions.info/ for more info, or grab a copy of
O’Reilly’s Mastering Regular Expressions by Jeffrey E. F. Friedl.
Get used to using other functions in Asterisk, get some experience with regular
expressions, and then give REGEX() a try.
STRFTIME()
We just saw the STRFTIME() function in our IF() example. It allows you to return a
time in various formats. In general, you want the input to be empty (which defaults to
the current time). You can also give this function a specific Unix epoch string and it’ll
work from that.
Handy Dialplan Functions
|
187
exten => 230,1,Noop(STRFTIME)
same => n,Noop(${STRFTIME(,,%S)}) ; we've seen this before
same => n,Noop(${STRFTIME(,,%B)}) ; month
same => n,Noop(${STRFTIME(,,%H)}) ; hour in 24hr format
same => n,Noop(${STRFTIME(,,%m)}) ; month as a decimal
same => n,Noop(${STRFTIME(,,%M)}) ; minute
same => n,Noop(${STRFTIME(,,%Y)}) ; year - 4 digits
same => n,Noop(${STRFTIME(,,%Y-%m-%d %H:%m:%S)}) ; string some together
Conclusion
In this chapter, we’ve covered a few more of the many applications in the Asterisk
dialplan, and hopefully we’ve given you some more tools that you can use to further
experiment with creating your own dialplans. As with other chapters, we invite you to
go back and reread any sections that require clarification.
188
|
Chapter 10: Deeper into the Dialplan
CHAPTER 11
PBX Features, Including Parking, Paging,
and Conferencing
I don’t believe in angels, no. But I do have a wee parking angel. It’s on my dashboard and you
wind it up. The wings flap and it’s supposed to give you a parking space. It’s worked so far.
—Billy Connolly
This chapter discusses several peripheral features common to business telephone
environments. We’ll briefly cover the features.conf file, and then spend a few sections
on paging and parking, and finally do a bit of work with Asterisk’s conferencing
engine, confbridge.
First up, let’s copy the features.conf file over from the installation directory, and have a
look at it:
$ sudo cp ~/src/asterisk-16.<TAB>/configs/samples/features.conf.sample \