Saturday, February 11, 2012

Dialplan Expressions and Variable Manipulation

Basic Expressions
Expressions always begin with a dollar sign and an opening square bracket and end with a closing square bracket:
$[expression]
Example:
$[${COUNT}+1]
Example in dialplan
exten => 321,1,Set(COUNT=3)
exten => 321,n,Set(NEWCOUNT=$[${COUNT}+1])
exten => 321,n,SayNumber(${NEWCOUNT})

Operators
Boolean operators
expr1 expr2 (OR)
expr1 & expr2 (AND)
expr1 {=, >, >=, <, <=, !=} expr2

Mathematical operators
expr1 {+, -} expr2
expr1 {*, /, %} expr2

Dialplan Functions
Function_Name(argument)
${FUNCTION_NAME(argument)}


Conditional Branching
The GotoIf() Application evaluates an expression and sends the caller to a specific destination based on whether the expression 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. Anthing else evaluates as true.
The destinations can each be one of the following:


  • A priority label with 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

Example:


exten => 345,1,Set(TEST=1)


exten => 345,n,GotoIf($[${TEST}=1]?weasels:iguanas)


exten => 345,n(weasels), Playback(weasels-eaten-phonesys)


exten => 345,n, Hangup()

Asterisk Dialplan - Pattern Matching

Pattern Matching syntax
When using pattern matching, certain letters and symbols represent what we are trying to match. Patterns always start with an underscore(_). This tells Asterisk that we're matching on a pattern, and not on an explicit extension nam.
X: matches any single digit from 0 to 9.
Z: matches any single digit from 1 to 9.
N: matches any single digit from 2 to 9.
[15-7]: Matches a single digit from the range of digits specified. In this case, the pattern matches a singel 1, 5,6,7.
. : wild card match; matches one or more characters, no matter what they are.
! : wild card match; matches zero or more characters, no matter what they are.

Using the ${EXTEN} channel variable
exten => _XXX, 1, SayDigits(${EXTEN})

To manipulate the ${EXTEN} by stripping a certain number of digits off the front of the extension. use the syntax ${EXTEN:x}, where x is where you want the returned string to start, from left to right. For example, if the value of EXTEN is 9123456, ${EXTEN:1} equals 123456.

The ${EXTEN} variable has the syntax ${EXTEN:x:y}, where x is the starting position, and y is the number of digits to return.
Example: 94169671111
${EXTEN:1:3} would contain 416.
${EXTEN:4:7} would contain 9671111
${EXTEN:-4:4} would start four digits from the end, and return four digits, giving 1111.

Enabling Outbound Dialing
The first thing we'll do is add a variable to the [globals] context to define which channel will be used for outbound calls:
[globals]
JOHN=Zap/1
JANE=SIP/Jane
OUBOUNDTRUNK=Zap/4
Next, add contexts to dialplan for outbound dialing.
The reason for adding contexts is to regulate and control which callers have permission to make outbound calls, and which type of outboud calls they are allowed to make.
[outbound-local]
exten => _9NXXXXXX,1,Dial(${OUTBOUNDTRUNK}/${EXTEN:1})
exten => _9NXXXXXX,n,Congestion()
exten => _9NXXXXXX,n,Hangup()

exten => 911,1,Dial(${OUTBOUNDTRUNK}/911)
exten => 9911,1,Dial(${OUTBOUNDTRUNK}/911)

Includes:
The include statement takes the following form, where context is the name of the remote context we want to include in the current context:
include => context

Asterisk Dialplan Variables

Three types of variables in Asterisk dialplan: global variables, channel variables, and environment variables.
1. Global variables should be declared in the [globals] context at the beginning of the extensions.conf file. They can also be defined programmatically, using the GLOBAL() dialplan function. For example,
[globals]
JOHN=Zap/1

[employees]
exten => 212, 1, Set(GLOBAL(GEORGE)=SIP/George)

2. Channel variables are variables that are associated only with a particular call. Channel variabbles are defined only for the duration of the current call and are available only to the channels participating in that call.
There are many predefined channel variables availabe for use within the dialplan, which are explained in the channelvariables.txt file in the doc subdirectory of Asterisk source. Channel variables are set via the Set() application:
exten => 125, 1, Set(MAGICNUMBER=42)

3. Environment variables are a way of accessing Unix environment variables from within Asterisk. These are referenced using the ENV() dialplan function. The syntax looks like ${ENV(var)}, where var is the Unix environment variable you wish to reference.

Asterisk Dialplan - Building an Interactive Dialplan

The Background(), WaitExten(), and Goto Applications
The Background() is similar to Playback() application, takes the same argument, the difference is Background() will stop and process the press callers enter digit. Norally used for voice menus (auto-attendants or phone trees).
exten => 212, 1, Answer()
exten => 212, n, Background(main-menu)
exten => 212, n, WaitExten() ;Normally follows after Background(), wait for the caller to enter DTMF digits.
WaitExten(5) means waiting for 5 seconds.
Goto Syntax: Goto(context, extension, priority)

Example:
[incoming]
exten => 212, 1, Answer()
exten => 212, n, Background(main-menu)
exten => 212, n, WaitExten(5)

exten => 1, 1, Playback(digit/1)
exten => 1, n, Goto(incoming, 212, 1)

Handling Invalid Entries and Timeouts
i extension and t extension
Example:
[incoming]
exten => 212, 1, Answer()
exten => 212, n, Background(main-menu)
exten => 212, n, WaitExten(5)

exten => 1, 1, Playback(digit/1)
exten => 1, n, Goto(incoming, 212, 1)

exten => i, 1, Playback(invalid-digit)
exten => i, n, Goto(incoming, 212,1)

exten => t, 1, Playback(invalid-digt)
exten => t, n, Hangup()

Dial() Application takes up to four arguments.
The first is the destination you are attempting to call, which is made up of a technology across which to make the call, a forward slash, and the remote endpoint or resource. Common technology types include Zap (for analog and T1/E1/J1 channels), SIP, and IAX2.
For example, let's assume that we want to call a Zap endpint identified by Zap/1, which is an FXS channel with an analog phone plugged into it. The technology is Zap, and the resource is 1. Similarly, a call to a SIP device (as defined in sip.conf) might hava a destination of SIP/Jane, and a call to an IAX defice (defined in iax.conf) might have a destination of IAX2/Fred. If we wanted Asterisk to ring the Zap/1 channel when extension 212 is reached in the dialplan, we'd add the following extension:
exten => 212, 1, Dial(Zap/1)
To dial multiple channels at the same time, by concatenating the destinations with an ampersand (&):
exten => 212, 1, Dial(Zap/1&Zap/2&SIP/Jane)

The Dial() application also allows you to connect to a remote VoIP endpoint not previously defined in one of the channel configuration files. Syntax is:
Dial(technology/user[:password]@remote_host[:port][/remote_extension])
The full syntax for the Dial() application is slightly different when dealing with Zap channels:
Dial(Zap/channel_or_group/remote_extension)
For example, here is how you would dial 01263123456 on Zap channel number 1
exten => 200,1,Dial(Zap/1/01263123456)

The second argument to the Dial() application is a timeout, specified in seconds.
exten => 212, 1, Dial(Zap/1, 10)
exten => 212, n, Playback(vm-nobodyavail)
exten => 212, n, Hangup()

The third arguement to Dial() is an option string. (m for music-on-hold)
The fourth argument to the Dial() application is a URL.

Asterisk Dialplan Syntax - 2

The s Extension
When calls enter a contet without a specific destination extension, they are passed to the s extension. eg:
[incoming]
exten => s,1,application()
exten => s,n,application()
exten => s,n,application()

The Answer(), Playback(), and Hangup() Applications
Answer() takes no argument.
Playback() application is used for playing a previously recorded sound file over a channel. Default directory for sound files is /var/lib/asterisk/sounds.
Hangup() takes no argument.

Asterisk Dialplan Syntax - 1

The Asterisk dialplan's configuration file name is extensions.conf which resides in /etc/asterisk/ directory.
The dialplan is made up of 4 main concepts: context, extensions, priorities, and applications.
1. Contexts: Contexts are named groups of extensions. Contexts keep different parts of the dialplan from interacting with one another.
Two sepcial contexts: [general] and [globals]
2. Extensions: Extensions are series of steps that Asterisk will take that call through.
Syntax: "exten => name, priority, application()"
Example: "exten => 212,1,Answer()
3. Priorities: Must be sequence from 1
exten => 212,1, Answer()
exten => 212,n, Hangup()
4. Applications: Each application performs a specific action on the current channel. such as Answer(), Hangup().