asterisk variable manipulation and expression tutorial

    In this Article i am going to explain th asterisk variable manipulation with examples, Asterisk variable manipulation is a powerful tool that can be used to control the behavior of Asterisk. By manipulating variables, you can change the way that Asterisk processes calls, displays information, and interacts with other applications.

asterisk variable manipulation

What is a variable?

    A variable is a named location in memory that can store a value. Variables are used to store data that can be changed throughout the execution of a program.
example:
${EXTEN} holds the value of dialed number in asterisk
${CALLERID(num)} holds the value of called person ie callerid.

What is variable manipulation?

    Variable manipulation is the process of changing the value of a variable. This can be done for a variety of reasons, such as to test the effects of different values on a program, or to make a program more efficient.

What is an expression?

    An expression is a combination of variables, operators, and constants that evaluates to a single value. Expressions are used to perform calculations, compare values, and control the flow of a program.

Video Tutorial

Asterisk variable manipulation and expressions

Asterisk variable manipulation is a powerful tool that can be used to control the behavior of Asterisk. By manipulating variables, you can change the way that Asterisk processes calls, displays information, and interacts with other applications.

There are many different ways to manipulate variables in Asterisk. Some common methods include:

Assigning a new value to a variable.
Adding, subtracting, multiplying, or dividing a variable by a constant or another variable.
Comparing a variable to a constant or another variable and taking an action based on the result of the comparison.
Formatting the variable's value in a different way.
Variable manipulation can be used to do a variety of things in Asterisk, such as:

Control the flow of a call.
Display information to the user.
Interact with other applications.
Perform calculations.

Asterisk manipulation syntax:

    Below in the dialplan syntax for the variable manipluation in asterisk for various methods.

Append or preped  : that is either a adding a digit in front of the variable or adding a digit at the end of the variable, it can be single or multiple digits.
x${EXTEN} or ${EXTEN}x
Extract or delete a digit from the actual variable, ie if you want to exact a a value or skip a digits either infront or at the end of the variable then you need to use the below syntax
${{variable:skip:length}}
Expression sytax: If you want to do mathematical operations like addition,subtraction,multiplication and division then use the below syntax
$[${variable}+1] 
$[${variable}-1] 
$[${variable}*1] 
$[${variable}/2] 

Variable manipluation examples

    For the part this tutorial we will take the EXTEN and CALLERID(num) variables as examples.

Append - Example

    I need to add a digit at the end of the dialed number , example add 9 at the end for the EXTEN variable. below is the syntax

${EXTEN}9  
if the EXTEN variable value is 12345 after manipulation its value is  123459

sample diaplan to add 9 at the end of EXTEN or CALLERID(num)
exten => _X.,1,Dial(SIP/voip/${EXTEN}9)
exten => _X.,1,Set(CALLERID(num)=${CALLERID(num)}9)

Prepend- Example

    Prepend or Prefix method add a digit in front of the variable, below is the syntax

9${EXTEN}
if the EXTEN variable value is 12345 after manipulation its value is 912345

sample diaplan to add 9 infront of EXTEN or CALLERID(num)
exten => _X.,1,Dial(SIP/voip/9${EXTEN})
exten => _X.,1,Set(CALLERID(num)=9${CALLERID(num)})

Discard or Delete Manipulation

    The Syntax for the dicard or delete manipulation is  ${VARIABLE:skip:length}.
the Skip and Length values can be either positive or negative value, based on  these value the behaviour depends, we will see with examples.

Positive skip value :Tells asterisk to skip the number of characters from the begining.
Negative skip value : Tells asterisk to count value from the end of the variable, ie skip all the values befor the skiped point from backwards.

Positive Length value: Tells asterisk to extract the number of characters from the skip value
Negative Legth value: Tells asterisk to count the characters from the end of the variable.

Skip Only Example:

    Lets take the EXTEN variable holding value of 12345678
Now with skip value of 2 skips first 2 digit and returns remaining value.

${EXTEN:2}     > 345678

now with negative skip value ie; -2 , skips all digits before the last two digit and returns the last two digit
${EXTEN:-2}     > 78

Skip with Length manipulation:

Exampel 1: both with positive values.

    Lets take ${EXTEN} with value 12345678

${EXTEN:0:3}    >    12345678 > 123
--skip 0 digits fron front and provide remaining 3 digit from that point.

${EXTEN:1:3}     >    12345678  > 234
--skip first digit and provide remaining 3 digits .

Exampel 2: Negative Skip and Positive Length value.
    Lets take ${EXTEN} with value 12345678

${EXTEN:-3:2}    >    12345678    > 678    > 67

minus 3  skips all digit before the last 3 digits (ie 678) and Length value 2 extracts first 2 digits from that point ie(67).

Exampel 3: Negative Skip and Negative Length value.

Lets take ${EXTEN} with value 12345678

${EXTEN:-3:-2}    >    12345678    > 678    >    6

Minus 3 skip value skips all digits before the last 3 digits (ie: 678) and length value of -2 tells asterisk to return all digit before the last 2 digits ie(6)

sytax for the CALLERID(num) is ${CALLERID(num):skip:length}

Variable Expression example

    Below are examples for the Mathematical expression manipulation methods

Syntax $[${VARIABLE} + 1]

Addition        Set(XYZ = $[${EXTEN} + 2])

subtraction    Set(XYZ = $[${EXTEN} - 2])

Multiply        Set(XYZ = $[${EXTEN} * 2])

Division Set(XYZ = $[${EXTEN} / 2 ])

Conclusion:

        Hope this artile is helpful to understand the asterisk variable manipulation and expression, using this article you can dicards digits like + symbol or area code before connecting to phone or agents or  add callerid based on the area code or assign DID based on the phones callerid.
No Comment
Add Comment
comment url