text
stringlengths
0
152
1 2 3 4 4321
As you’ve probably already figured out, you must be very careful about making sure
you have matching parentheses and braces. In the preceding example, we have
labeled the opening parentheses and curly braces with numbers and their corre‐
sponding closing counterparts with the same numbers.
Examples of Dialplan Functions
Functions are often used in conjunction with the Set() application to either get or set
the value of a variable. As a simple example, let’s look at the LEN() function. This
function calculates the string length of its argument:
exten => 205,1,Answer()
same => n,SayDigits(123)
same => n,SayNumber(123)
same => n,SayNumber(${LEN(123)})
Let’s look at another simple example. If we wanted to set one of the various channel
timeouts, we could use the TIMEOUT() function. The TIMEOUT() function accepts one
of three arguments: absolute, digit, and response. To set the digit timeout with the
TIMEOUT() function, we could use the Set() application, like so:
exten => 206,1,Answer()
same => n,Set(TIMEOUT(response)=1)
same => n,Background(enter-ext-of-person)
same => n,WaitExten() ; TIMEOUT() has set this to 1
same => n,Playback(like_to_tell_valid_ext)
same => n,Set(TIMEOUT(response)=5)
same => n,Background(enter-ext-of-person)
same => n,WaitExten() ; Should be 5 seconds now
same => n,Playback(like_to_tell_valid_ext)
same => n,Hangup()
Notice the lack of ${ } surrounding the assignment using the function. Just as if we
were assigning a value to a variable, we assign a value to a function without the use of
the ${ } encapsulation; however, if we want to use the value returned by the function,
then we need the encapsulation.
exten => 207,1,Answer()
same => n,Set(TIMEOUT(response)=1)
same => n,SayNumber(${TIMEOUT(response)})
same => n,Set(TIMEOUT(response)=5)
Dialplan Functions
|
167
same => n,SayNumber(${TIMEOUT(response)})
same => n,Hangup()
You can get a list of all active functions with the following CLI command:
*CLI> core show functions
Or, to see a specific function such as CALLERID(), the command is:
*CLI> core show function CALLERID
Near the end of this chapter, we explore a handful of functions you will want to
experiment with. Later in the book we’ll show you how to create database-based func‐
tions using func_odbc.
Conditional Branching
The advanced logic provided through expressions and functions will allow your
dialplan to make more powerful decisions, which will often result in conditional
branching.
The GotoIf() Application
The key to conditional branching is the GotoIf() application. GotoIf() evaluates an
expression and sends the caller to a specific destination based on whether the expres‐
sion evaluates to true or false.
GotoIf() uses a special syntax, often called the conditional syntax:
GotoIf(expression?destination1:destination2)
If the expression evaluates to true, the caller is sent to destination1. If the expression
evaluates to false, the caller is sent to the second destination. So, what is true and what
is false? An empty string and the number 0 evaluate as false. Anything else evaluates
as true.
The destinations can each be one of the following:
• A priority label within the same extension, such as weasels
• An extension and a priority label within the same context, such as 123,weasels
• A context, extension, and priority label, such as incoming,123,weasels
Let’s use GotoIf() in an example. Here’s a little coin toss application. Call it several
times to properly test.
exten => 209,1,Noop(Test use of conditional branching to labels)
same => n,GotoIf($[ ${RAND(0,1)} = 1 ]?weasels:iguanas)
; same => n,GotoIf(${RAND(0,1)}?weasels:iguanas) ; works too, but won't in every situation
same => n(weasels),Playback(weasels-eaten-phonesys) ; NOTE THIS IS SAME EXTENSION
168