Page 164 - Asterisk™: The Future of Telephony
P. 164
your dialplan and change all of those references to Zap/2. It would be a long and error-
prone process, to say the least.
On the other hand, if you had defined a global variable with the value Zap/1 at the
beginning of your dialplan and then referenced that instead, you would have to change
only one line.
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. Here is an example of how both methods look inside of a dialplan.
The first shows the setting of a global variable named JOHN with a value of Zap/1. This
variable is set at the time Asterisk parses the dialplan. The second example shows how
a global variable can be set in the dialplan. In this case, the variable named George is
being assigned the value of SIP/George when extension 124 is dialed in the [employees]
context:
[globals]
JOHN=Zap/1
[employees]
exten => 124,1,Set(GLOBAL(GEORGE)=SIP/George)
Channel variables
A channel variable is a variable that is associated only with a particular call. Unlike
global variables, channel variables 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 available for use within the dialplan,
which are explained in the channelvariables.txt file in the doc subdirectory of the As-
terisk source. Channel variables are set via the Set() application:
exten => 125,1,Set(MAGICNUMBER=42)
We’ll cover many uses for channel variables in Chapter 6.
Environment variables
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. Envi-
ronment variables aren’t commonly used in Asterisk dialplans, but they are available
should you need them.
Adding variables to our dialplan
Now that we’ve learned about variables, let’s put them to work in our dialplan. We’ll
add global variables for two people, John and Jane:
† Don’t worry! We’ll cover dialplan functions in the “Dialplan Functions” section.
136 | Chapter 5: Dialplan Basics