text stringlengths 0 152 |
|---|
Expressions and Variable Manipulation |
As we begin our dive into the deeper aspects of dialplans, it is time to introduce you |
to a few tools that will greatly add to the power you can exercise in your dialplan. |
These constructs add incredible intelligence to your dialplan by enabling it to make |
decisions based on different criteria you define. Put on your thinking cap, and let’s get |
started. |
Throughout this chapter we use best practices that have been |
developed over the years in dialplan creation. The primary one |
you’ll notice is that all the first priorities start with the NoOp() |
application (which simply means No Operation; nothing functional |
will happen). The other one is that all following lines will start with |
same => n, which is a shortcut that says, “Use the same extension |
as was just previously defined.” Additionally, the indentation is two |
spaces. |
Basic Expressions |
Expressions are combinations of variables, operators, and values that you string |
together to produce a result. An expression can test values, alter strings, or perform |
163 |
mathematical calculations. Let’s say we have a variable called COUNT. In plain English, |
two expressions using that variable might be [COUNT plus 1], or [COUNT divided by 2]. |
Each of these expressions has a particular result or value, depending on the value of |
the given variable. |
In Asterisk, expressions always begin with a dollar sign and an opening square |
bracket and end with a closing square bracket, as shown here: |
$[expression] |
Thus, we would write our two examples like this: |
$[${COUNT} + 1] |
$[${COUNT} / 2] |
When Asterisk encounters an expression in a dialplan, it replaces the entire expres‐ |
sion with the resulting value. It is important to note that this takes place after variable |
substitution. To demonstrate, let’s look at the following code:1 |
exten => 321,1,NoOp() |
same => n,Answer() |
same => n,Set(COUNT=3) |
same => n,Set(NEWCOUNT=$[${COUNT} + 1]) |
same => n,SayNumber(${NEWCOUNT}) |
In the second priority, we assign the value of 3 to the variable named COUNT. |
In the third priority, only one application—Set()—is involved, but three things |
actually happen: |
1. Asterisk substitutes ${COUNT} with the number 3 in the expression. The expres‐ |
sion effectively becomes this: |
same => n,Set(NEWCOUNT=$[3 + 1]) |
2. Asterisk evaluates the expression, adding 1 to 3, and replaces it with its computed |
value of 4: |
same => n,Set(NEWCOUNT=4) |
3. The Set() application assigns the value 4 to the NEWCOUNT variable. |
The third priority simply invokes the SayNumber() application, which speaks the cur‐ |
rent value of the variable ${NEWCOUNT} (set to the value 4 in priority two). |
Try it out in your own dialplan. |
1 Remember that when you reference a variable you can call it by its name, but when you refer to a variable’s |
value, you have to use the dollar sign and brackets around its name. |
164 |
| |
Chapter 10: Deeper into the Dialplan |
Operators |
When you create an Asterisk dialplan, you’re really writing code in a specialized |
scripting language. This means that the Asterisk dialplan—like any programming |
language—recognizes symbols called operators that allow you to manipulate vari‐ |
ables. Let’s look at the types of operators that are available in Asterisk: |
Boolean operators |
These operators evaluate the “truth” of a statement. In computing terms, that |
essentially refers to whether the statement is something or nothing (nonzero or |
zero, true or false, on or off, and so on). The Boolean operators are: |
expr1 | expr2 |
This operator (called the “or” operator, or “pipe”) returns the evaluation of |
expr1 if it is true (neither an empty string nor zero). Otherwise, it returns |
the evaluation of expr2. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.