text
stringlengths
0
152
Don’t worry about the rest of them. If you need ’em, you’ll know what they are or why
you want to use them.
CHANNEL()
CHANNEL() allows you to interact with an absolute boatload of data relating to the
channel. Some items allow you to modify them, while others will only be useful for
reference (for example, peerip will allow you to read, but not change, the IP address
of the peer). There are also channel variables that only work with certain channel
types (for example, pjsip items can of course only be used on PJSIP channels).
exten => 223,1,Noop(CHANNEL function)
same => n,Answer()
same => n,Noop(CHANNEL(name) is ${CHANNEL(name)})
same => n,Noop(CHANNEL(musicclass) is ${CHANNEL(musicclass)})
same => n,Noop(CHANNEL(rtcp,all_jitter) is ${CHANNEL(rtcp,all_jitter)})
same => n,Noop(CHANNEL(rtcp,all_loss) is ${CHANNEL(rtcp,all_loss)})
same => n,Noop(CHANNEL(rtcp,all_rtt) is ${CHANNEL(rtcp,all_rtt)})
same => n,Noop(CHANNEL(rtcp,txcount) is ${CHANNEL(rtcp,txcount)})
same => n,Noop(CHANNEL(rtcp,rxcount) is ${CHANNEL(rtcp,rxcount)})
same => n,Noop(CHANNEL(pjsip,local_uri) is ${CHANNEL(pjsip,local_uri)})
same => n,Noop(CHANNEL(pjsip,remote_uri) is ${CHANNEL(pjsip,remote_uri)})
same => n,Noop(CHANNEL(pjsip,request_uri) is ${CHANNEL(pjsip,request_uri)})
same => n,Noop(CHANNEL(pjsip,local_tag) is ${CHANNEL(pjsip,local_tag)})
CURL()
CURL() is a simple yet powerful function that provides a one-liner method for resolv‐
ing URLs, which in many cases is all you need for a basic interaction with an external
web service.
exten => 224,1,Noop(CURL function)
same => n,Set(ExternalIP=${CURL(http://whatismyip.akamai.com)})
same => n,Noop(The external IP address is ${ExternalIP})
If you need a more complex interaction with an external service, it could be that you
are going to want an AGI program of some sort. Still, you can embed a ton of data in
a URL, and for simplicity, CURL() is hard to beat.
CUT()
If you need to slice-and-dice your variables, you’ll find the CUT() function essential.
The form is simple:
CUT(varname,char-delim,range-spec)
It can be visually tricky, as the delimiter character can be difficult to see nested in
between two commas (for example, if the delimiter was a dot/decimal/period). Let’s
Handy Dialplan Functions
|
185
expand on the previous example to see what it’s good for (and give you a visual exam‐
ple of how the delimiter can get lost in the syntax).
exten => 225,1,Noop(CUT function)
same => n,Set(ExternalIP=${CURL(http://whatismyip.akamai.com)})
same => n,Noop(The external IP address is ${ExternalIP})
same => n,Answer()
same => n,SayDigits(=${CUT(ExternalIP,.,1)})
same => n,Playback(letters/dot)
same => n,SayDigits(=${CUT(ExternalIP,.,2)})
same => n,Playback(letters/dot)
same => n,SayDigits(=${CUT(ExternalIP,.,3)})
same => n,Playback(letters/dot)
same => n,SayDigits(=${CUT(ExternalIP,.,4)})
Note that you call the CUT() function with the braces ${CUT()}, but
the variable being referenced inside CUT() is defined without the
braces. This is because we are naming the variable, not asking for
its contents (CUT() will deal with the contents, so we just need to
name the variable it will be slicing and dicing, and it will dive into
what is stored there).
IF() and STRFTIME()
The combination of IF() and STRFTIME() is a powerful construct, and you will find it
an essential part of your dialplan logic:
exten => 226,1,Noop(IF)
same => n,Answer()
same => n,Playback(${IF($[$[${STRFTIME(,,%S)} % 2] = 1]?hear-odd-noise:good-evening)})
Wait...what?8
Let’s break this down (we’re going to indent the code in an odd manner in order to
show the progression of the nested functions and operators):
exten => 227,1,Noop(IF)
same => n,Answer()
same => n,Wait(.5)
same => n,Wait(.5)
same => n,Noop(${STRFTIME(,,%S)}) ; current time - just seconds
same => n,Noop($[ ${STRFTIME(,,%S)} % 2 ]) ; divide by 2 - return remainder
same => n,Noop(${IF($[ $[ ${STRFTIME(,,%S)} % 2 ] = 1 ]?odd:even)})
same => n,Playback(${IF($[ $[ ${STRFTIME(,,%S)} % 2 ] = 1 ]?hear-odd-noise:good-evening)})
8 There is a C language function named STRFTIME() that returns the current time as a formatted string. This
works similarly to that. In fact, the format portion of the function takes the exact same syntax as the C func‐
tion.