Operators in Asterisk with Linux
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 variables.
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 (non-zero 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.
expr1 & expr2
This operator (called “and”) returns the evaluation of expr1 if both expressions
are true (i.e., neither expression evaluates to an empty string or zero).
Otherwise, it returns zero.
expr1 {=, >, >=, <, <=, !=} expr2
These operators return the results of an integer comparison if both arguments
are integers; otherwise, they return the results of a string comparison.
The result of each comparison is 1 if the specified relation is true, or 0 if the
relation is false. (If you are doing string comparisons, they will be done in a
manner that’s consistent with the current locale settings of your operating
system.)
Mathematical operators
Want to perform a calculation? You’ll want one of these:
expr1 {+, -} expr2
These operators return the results of the addition or subtraction of integervalued
arguments.
expr1 {*, /, %} expr2
These return, respectively, the results of the multiplication, integer division,
or remainder of integer-valued arguments.
Regular expression operator
You can also use the regular expression operator in Asterisk:
expr1 : expr2
This operator matches expr1 against expr2, where expr2 must be a regular
expression.* The regular expression is anchored to the beginning of the
string with an implicit ^.†
If the match succeeds and the pattern contains at least one regular expression
subexpression—\(...\)—the string corresponding to \1 is returned;
otherwise, the matching operator returns the number of characters matched.
If the match fails and the pattern contains a regular expression subexpression,
the null string is returned; otherwise, 0 is returned.
Conditional Branching
Now that you’ve learned a bit about expressions and functions, it’s time to put them
to use. By using expressions and functions, you can add even more advanced logic to
your dialplan. To allow your dialplan to make decisions, you’ll use 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 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 the first destination. 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.
Let’s use GotoIf( ) in an example:
exten =>123,1,Set(TEST=1)
exten =>123,2,GotoIf($[{$TEST} = 1]?10:20)
exten => 123,10,Playback(WELCOME)
exten => 123,20,Playback(THANKS)
Time-Based Conditional Branching with GotoIfTime( )
Another way to use conditional branches in your dialplan is with the GotoIfTime( )
application. Whereas GotoIf( ) evaluates an expression to decide what to do,
GotoIfTime( ) looks at the current system time and uses that to decide whether or not
to follow a different branch in the dialplan.
The most obvious use of this application is to give your callers a different greeting
before and after normal business hours.
The syntax for the GotoIfTime( ) application looks like this:
GotoIfTime(times,days_of_week,days_of_month,months?label)
In short, GotoIfTime( ) sends the call to the specified label if the current date and
time match the criteria specified by times, days_of_week, days_of_month, and months.
Let’s look at each argument in more detail:
times
This is a list of one or more time ranges, in 24-hour format. As an example, 9:00
a.m. through 5:00 p.m. would be specified as 09:00-17:00. The day starts at 0:00
and ends at 23:59.
days_of_week
This is a list of one or more days of the week. The days should be specified as
mon, tue, wed, thu, fri, sat, and/or sun. Monday through Friday would be
expressed as mon-fri. Tuesday and Thursday would be expressed as tue,thu.
days_of_month
This is a list of the numerical days of the month. Days are specified by the numbers
1 through 31. The 7th through the 12th would be expressed as 7-12, and the
15th and 30th of the month would be written as 15,30.
months
This is a list of one or more months of the year. The months should be written as
jan, feb, mar, apr, and so on.
////////////////Creating Mailboxes////////////////
Inside each voicemail context, we define different mailboxes. The syntax for defining
a mailbox is:
mailbox => password,name[,email[,pager_email[,options]]]
Let’s explain what each part of the mailbox definition does:
mailbox
This is the mailbox number. It usually corresponds with the extension number
of the associated set.
password
This is the numeric password the mailbox owner will use to access her voicemail.
If the user changes her password, the system will update this field in the
voicemail.conf file.
name
This is the name of the mailbox owner. The company directory uses the text in
this field to allow callers to spell usernames.
email
This is the email address of the mailbox owner. Asterisk can send voicemail notifications
(including the voicemail message itself) to the specified email box.
pager_email
This is the email address of the mailbox owner’s pager or cell phone. Asterisk
can send a short voicemail notification message to the specified email address.
options
This field is a list of options that sets the mailbox owner’s time zone and overrides
the global voicemail settings. There are nine valid options: attach,
serveremail, tz, saycid, review, operator, callback, dialout, and exitcontext.
These options should be in option=value pairs, separated by the pipe character
(|). The tz option sets the user’s time zone to a time zone previously defined in
the [zonemessages] section of voicemail.conf, and the other eight options override
the global voicemail settings with the same names.
A typical mailbox definition might look something like this:
101 => 123,sharjeel,bilali.sharjeel@somedomain.com,shaghil@pagergateway.net,tz=central|attach=yes
Continuing with our dialplan from the last chapter, let’s set up voicemail boxes for
sharjeel and shaghil. We’ll give sharjeel a password of 123 and shaghil a password of 4444
(remember, these go in voicemail.conf, not extensions.conf):
[default]
101 => 123,bilali.sharjeel@asteriskdocs.org,sharjeel@pagergateway.tld
102 => 4444,shaghil,shaghil@asteriskdocs.org,shaghil@pagergateway.tld
Adding Voicemail to the Dialplan
Now that we’ve created mailboxes for Jane and John, let’s allow callers to leave messages
for them if they don’t answer the phone. To do this, we’ll use the VoiceMail( )
application.
The VoiceMail( ) application sends the caller to the specified mailbox, so that he can
leave a message. The mailbox should be specified as mailbox@context, where context
is the name of the voicemail context. The mailbox number can also be prefixed by
the letter b or the letter u. If the letter b is used, the caller will hear the mailbox
owner’s busy message. If the letter u is used, the caller will hear the mailbox owner’s
unavailable message (if one exists).
Let’s use this in our sample dialplan. Previously, we had a line like this in our
[internal] context, which allowed us to call sharjeel:
exten => 101,1,Dial(${sharjeel},,r)
Now, let’s change it so that if John is busy (on another call), it’ll send us to his voicemail,
where we’ll hear his busy message (don’t forget that the Dial( ) application
sends the caller to priority n+101 if the dialed line is busy):
exten => 101,1,Dial(${sharjeel},,r)
Accessing Voicemail
Users can retrieve their voicemail messages, change their voicemail options, and
record their voicemail greetings by using the VoiceMailMain( ) application. In its typical
form, VoiceMailMain( ) is called without any arguments. Let’s add extension 500
to the [internal] context of our dialplan so that internal users can dial it to access
their voicemail messages:
exten => 500,1,VoiceMailMain( )
exten => 101,102,VoiceMail(b101@default)
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 variables.
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 (non-zero 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.
expr1 & expr2
This operator (called “and”) returns the evaluation of expr1 if both expressions
are true (i.e., neither expression evaluates to an empty string or zero).
Otherwise, it returns zero.
expr1 {=, >, >=, <, <=, !=} expr2
These operators return the results of an integer comparison if both arguments
are integers; otherwise, they return the results of a string comparison.
The result of each comparison is 1 if the specified relation is true, or 0 if the
relation is false. (If you are doing string comparisons, they will be done in a
manner that’s consistent with the current locale settings of your operating
system.)
Mathematical operators
Want to perform a calculation? You’ll want one of these:
expr1 {+, -} expr2
These operators return the results of the addition or subtraction of integervalued
arguments.
expr1 {*, /, %} expr2
These return, respectively, the results of the multiplication, integer division,
or remainder of integer-valued arguments.
Regular expression operator
You can also use the regular expression operator in Asterisk:
expr1 : expr2
This operator matches expr1 against expr2, where expr2 must be a regular
expression.* The regular expression is anchored to the beginning of the
string with an implicit ^.†
If the match succeeds and the pattern contains at least one regular expression
subexpression—\(...\)—the string corresponding to \1 is returned;
otherwise, the matching operator returns the number of characters matched.
If the match fails and the pattern contains a regular expression subexpression,
the null string is returned; otherwise, 0 is returned.
Conditional Branching
Now that you’ve learned a bit about expressions and functions, it’s time to put them
to use. By using expressions and functions, you can add even more advanced logic to
your dialplan. To allow your dialplan to make decisions, you’ll use 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 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 the first destination. 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.
Let’s use GotoIf( ) in an example:
exten =>123,1,Set(TEST=1)
exten =>123,2,GotoIf($[{$TEST} = 1]?10:20)
exten => 123,10,Playback(WELCOME)
exten => 123,20,Playback(THANKS)
Time-Based Conditional Branching with GotoIfTime( )
Another way to use conditional branches in your dialplan is with the GotoIfTime( )
application. Whereas GotoIf( ) evaluates an expression to decide what to do,
GotoIfTime( ) looks at the current system time and uses that to decide whether or not
to follow a different branch in the dialplan.
The most obvious use of this application is to give your callers a different greeting
before and after normal business hours.
The syntax for the GotoIfTime( ) application looks like this:
GotoIfTime(times,days_of_week,days_of_month,months?label)
In short, GotoIfTime( ) sends the call to the specified label if the current date and
time match the criteria specified by times, days_of_week, days_of_month, and months.
Let’s look at each argument in more detail:
times
This is a list of one or more time ranges, in 24-hour format. As an example, 9:00
a.m. through 5:00 p.m. would be specified as 09:00-17:00. The day starts at 0:00
and ends at 23:59.
days_of_week
This is a list of one or more days of the week. The days should be specified as
mon, tue, wed, thu, fri, sat, and/or sun. Monday through Friday would be
expressed as mon-fri. Tuesday and Thursday would be expressed as tue,thu.
days_of_month
This is a list of the numerical days of the month. Days are specified by the numbers
1 through 31. The 7th through the 12th would be expressed as 7-12, and the
15th and 30th of the month would be written as 15,30.
months
This is a list of one or more months of the year. The months should be written as
jan, feb, mar, apr, and so on.
////////////////Creating Mailboxes////////////////
Inside each voicemail context, we define different mailboxes. The syntax for defining
a mailbox is:
mailbox => password,name[,email[,pager_email[,options]]]
Let’s explain what each part of the mailbox definition does:
mailbox
This is the mailbox number. It usually corresponds with the extension number
of the associated set.
password
This is the numeric password the mailbox owner will use to access her voicemail.
If the user changes her password, the system will update this field in the
voicemail.conf file.
name
This is the name of the mailbox owner. The company directory uses the text in
this field to allow callers to spell usernames.
This is the email address of the mailbox owner. Asterisk can send voicemail notifications
(including the voicemail message itself) to the specified email box.
pager_email
This is the email address of the mailbox owner’s pager or cell phone. Asterisk
can send a short voicemail notification message to the specified email address.
options
This field is a list of options that sets the mailbox owner’s time zone and overrides
the global voicemail settings. There are nine valid options: attach,
serveremail, tz, saycid, review, operator, callback, dialout, and exitcontext.
These options should be in option=value pairs, separated by the pipe character
(|). The tz option sets the user’s time zone to a time zone previously defined in
the [zonemessages] section of voicemail.conf, and the other eight options override
the global voicemail settings with the same names.
A typical mailbox definition might look something like this:
101 => 123,sharjeel,bilali.sharjeel@somedomain.com,shaghil@pagergateway.net,tz=central|attach=yes
Continuing with our dialplan from the last chapter, let’s set up voicemail boxes for
sharjeel and shaghil. We’ll give sharjeel a password of 123 and shaghil a password of 4444
(remember, these go in voicemail.conf, not extensions.conf):
[default]
101 => 123,bilali.sharjeel@asteriskdocs.org,sharjeel@pagergateway.tld
102 => 4444,shaghil,shaghil@asteriskdocs.org,shaghil@pagergateway.tld
Adding Voicemail to the Dialplan
Now that we’ve created mailboxes for Jane and John, let’s allow callers to leave messages
for them if they don’t answer the phone. To do this, we’ll use the VoiceMail( )
application.
The VoiceMail( ) application sends the caller to the specified mailbox, so that he can
leave a message. The mailbox should be specified as mailbox@context, where context
is the name of the voicemail context. The mailbox number can also be prefixed by
the letter b or the letter u. If the letter b is used, the caller will hear the mailbox
owner’s busy message. If the letter u is used, the caller will hear the mailbox owner’s
unavailable message (if one exists).
Let’s use this in our sample dialplan. Previously, we had a line like this in our
[internal] context, which allowed us to call sharjeel:
exten => 101,1,Dial(${sharjeel},,r)
Now, let’s change it so that if John is busy (on another call), it’ll send us to his voicemail,
where we’ll hear his busy message (don’t forget that the Dial( ) application
sends the caller to priority n+101 if the dialed line is busy):
exten => 101,1,Dial(${sharjeel},,r)
Accessing Voicemail
Users can retrieve their voicemail messages, change their voicemail options, and
record their voicemail greetings by using the VoiceMailMain( ) application. In its typical
form, VoiceMailMain( ) is called without any arguments. Let’s add extension 500
to the [internal] context of our dialplan so that internal users can dial it to access
their voicemail messages:
exten => 500,1,VoiceMailMain( )
exten => 101,102,VoiceMail(b101@default)
Comments
Post a Comment