VoIP Cookbook: Defining Extension
Unlike the extensions in traditional PABX, where the extension is usually associated with a phone, interface or menu in, the extension in Asterisk is defined as a set of commands to run. These commands are usually executed according to their level of priority. Some commands, such as Dial or GotoIf, have the ability to follow other commands depending on a certain circumstance.
At the time when the extension is dialed, the command marked as 1 will be executed, followed by command number 2 and so on, until the phone is hung up.
In the syntax used in extensions.conf file, a step in a given extension is written using the following format:
exten = extension,priority,Command(parameter)
The sign “equal to” = can also be written using “=>”, just like the form often used in many examples.
In conclusion, a "context" has a name, such as "john". In every of them, we can define one or more "extension". In an extension, we can define a set of commands. How do we define these extensions and the commands required to handle them? To define both, we need to edit extensions.conf file using a text editor. There are several tools that allow us to edit them using graphic/ web.
The components that build the stages of extension command or the command line are as follows:
- Extension is the label of an extension, which can be a string (containing allowed numbers, letters and symbols). Extension is a pattern that must be evaluated dynamically ir order to match many possible phone numbers. Every command line that becomes part of a particular extension should have the same label.
- Priority usually is of integer number. It is the sequence of a command that must be run within a given extension. The first command that will be run must begin with priority 1. If there is no such thing as priority 1, then Asterisk will not execute the extension command. After running priority 1, Asterisk will then add another priority to the priority 2 and so on, of course, provided that there is no command that determines which subsequent priority which must be run. If the next command turns out to be undefined, Asterisk will stop the process running the command, notwithstanding there are still commands with higher priority.
- Command is the "application" to be run by Asterisk.
- Parameters are the parameters that must given to a command. Not all command requires parameter, as some of them can be executed without parameters.
For example:
exten => 123,1,Answer exten => 123,2,Playback(tt-weasels) exten => 123,3,Voicemail(44) exten => 123,4,Hangup
With these definitions, an extension is numbered "123". When a call is dialed to this extension, Asterisk will respond to the call, executing a sound file with the name “tt-weasels” and give the caller the chance to enter voicemail into mailbox 33, and will be ended up with a hangup.
Asterisk itself does not really care about the order of line placement in extensions.conf. So with random placement of lines, the command we want to execute will still be carried out according to the order we want.
exten => 123,4,Hangup exten => 123,1,Answer exten => 123,3,Voicemail(44) exten => 123,2,Playback(tt-weasels)
Another way in defining the command is to use Caller ID to match the caller.
exten => 123/100,1,Answer() exten => 123/100,2,Playback(tt-weasels) exten => 123/100,3,Voicemail(123) exten => 123/100,4,Hangup()
With such command, compability with extension 123 will be possible only when the Caller ID of the caller is 100. This can also be done through pattern matching process, such as the following:
exten => 1234/_256NXXXXXX,1,Answer() and so on
This way, the compability with extension 1234 will only possible if the Caller ID begins with just the code area number 256.
We can even do the following:
exten => s,1,Answer exten => s/9184238080,2,Set(CALLERID(name)=EVIL BASTARD) exten => s,2,Set(CALLERID(name)=Good Person) exten => s,3,Dial(SIP/goodperson)
In the second priority, it is shown that we can mark any person we dislike, while any person other than the one we dislike, after third priority, will return to the path specified.