How Asterisk Works
This guide teaches you how to use expression evaluation in Asterisk, a key tool for evaluating arithmetic and logical expressions. It includes syntax, practical examples, important notes, and combined usage with variables.
In uContact, $[...] expressions are used in the activities of telephony flows (IVRs, IVR functions and telephony flows), for example in Evaluate or Set variable. In Bots, conditions and values are written in JavaScript instead.
Basic Syntax
$[expr1 operator expr2]Logical Operators
These operators allow you to combine boolean conditions:
expr1 | expr2 (Logical OR)
- Returns
expr1if it evaluates as non-empty/non-zero ("true"). - Otherwise, evaluates and returns
expr2.
- Returns
expr1 & expr2 (Logical AND)
- Returns
expr1if both expressions evaluate as non-empty/non-zero ("true"). - Otherwise, returns zero ("false").
- Returns
!expr (Unary Logical NOT)
- Note: There must be no space between
!andexpr.
- Note: There must be no space between
Using Logical Operators
You can combine conditions within GotoIf or similar commands:
$[ $[ "${FOO}" = "1" ] & $[ "${BAR}" = "2" ] ]In this example, the expression evaluates whether both variables (FOO and BAR) meet their respective values. If they do, the full expression returns 1.
Comparison Operators
These allow value comparisons to control the call flow based on conditions:
expr1 = expr2expr1 != expr2expr1 < expr2expr1 > expr2expr1 <= expr2expr1 >= expr2
Important Notes
- If both arguments are integers, an integer comparison is performed.
- Otherwise, a string comparison is performed using the system’s local sort order.
- Returns 1 if true, 0 if false.
Arithmetic Operators
Asterisk supports basic arithmetic with integers and decimal numbers:
expr1 + expr2: Additionexpr1 - expr2: Subtraction-expr: Negationexpr1 * expr2: Multiplicationexpr1 / expr2: Division. The result can have decimals (for example,$[5 / 2]returns2.5).expr1 % expr2: Modulo (remainder)
To get an integer result, use the rounding functions described below (for example, $[TRUNC(5 / 2)] returns 2).
Other Operators
expr1 : expr2(Regular expression match): comparesexpr1with the regular expressionexpr2, anchored at the beginning. Returns the number of matched characters, or the text captured between\(and\)if the pattern has a group.expr1 =~ expr2(Regular expression match, not anchored): like:, but the match can be anywhere inexpr1.expr1 ? expr2 :: expr3(Conditional): returnsexpr2ifexpr1is true (non-empty and non-zero), orexpr3otherwise.- Example:
$[${COUNT} > 3 ? high :: low]
- Example:
Asterisk Arithmetic Functions
Functions for rounding and truncation are available:
FLOOR(x): Rounds down to the nearest integer.CEIL(x): Rounds up to the nearest integer.ROUND(x): Rounds to the nearest integer. If exactly halfway, it rounds away from zero.RINT(x): Rounds to the nearest integer, preferring the even number in case of a tie.TRUNC(x): Removes the decimal part, rounding toward zero.
Clarifications
Dialplan functions can be called directly within
$[...]without${...}.- Example:
$[3 + FLOOR(2.9)]
- Example:
Variables, however, must use
${...}.- Example:
$[${a} + 2]
- Example:
Arguments in functions must be separated by commas
,when using$[...].
Available Math Functions
Most functions from the C math library are available for more advanced calculations:
Trigonometric (input in radians):
COS(x)SIN(x)TAN(x)
Inverse Trigonometric (input between -1 and 1):
ACOS(x)ASIN(x)
ATAN(x)(returns radians between -π/2 and π/2)ATAN2(x, y)(returns between -π and π)Exponents and Logarithms:
POW(x, y): x to the power of y (x^y)EXP(x): e to the power of xEXP2(x): 2 to the power of xLOG(x): Natural logarithm (base e)LOG2(x): Base 2 logarithmLOG10(x): Base 10 logarithm
Others:
SQRT(x): Square rootREMAINDER(x, y): Returns the mathematical remainder (unlike%). Calculated asx - n*ywheren = x/yrounded to the nearest even integer.