Predefined channel variables
Asterisk contains a number of predefined channel variables. Some of such variables are CONTEXT, EXTEN, and PRIORITY which hold the current context, extension, and priority. They are created automatically by Asterisk when an extension is dialed.
Here is a simple example. Write the following code at the end of the dialplan:
exten => _XXXX,1,Answer() same => n,SayDigits(${EXTEN})
The first line of the code will match any four number extension (will be discuss the pattern matching in the next section). The second line instructs Asterisk to run the SayDigits application, which will simply read back to you the four-digit extension that was dialed. So for example, if you dial 2005, you should hear these four numbers read back to you.
It is also possible to strip a certain number of digits off the front of the extension. For example, if you’ve assigned 0 for routing of external calls, you will need to remove the leading 0 before passing the call to your carrier. The syntax for doing this is ${EXTEN:n}, where n indicates where you want the returned number to start, from left to right. So in our last example, we can do this:
exten => _XXXX,1,Answer() same => n,SayDigits(${EXTEN:1})
Now, when you call a four-digit extension, the SayDigits application will start at the second digit and read only three digits, instead of four. So if we dial 2005, only the last three numbers will be read (005).