With many phone systems, a number such as “9” will need to be pressed in order to dial out and reach a phone number outside the Asterisk dialplan. In this lesson we will use the first digit 9 to indicate to Asterisk that the call will need to route through the SIP provider rather than it being a local extension.
We can use a regular expression, which in Asterisk will be used as a placeholder for an extension. Placeholders start with an “_” because it tells the system that a placeholder is going to be used. The “X” means any number 0-9. For example, _XXX is any three digit number. Because we may not know how many digits will be in the number, we can use the “.” to indicate to Asterisk that the previous digit is repeated any number of digits long. For example, _X. would allow any number, with any number of digits.
The EXTEN variable will hold the value of the phone extension that was last dialed. To use the value of this variable use the syntax ${EXTEN}. Because the phone system is now setup to dial “9” before the actual phone number, the EXTEN variable will also have “9” starting the number. Asterisk allows the variable to be split, which will allow us to remove that leading digit. If we use ${EXTEN:1} the number will start at index 1. Index values start counting with 0, so the “9” is located at index 0.
Up to this point, when the Dial() application has been used it has indicated to use SIP and then the account. We are going add another parameter, and that is the number to pass to the SIP account. We can use the SIP account from the online provider and pass the public phone number for it to call to.
Open the /etc/asterisk/extensions.conf file and add the following to the [phones] context:
exten => _9X.,1,NoOp(Calling Outside Line ${EXTEN:1})
same => n,Dial(SIP/LectureSnippets/${EXTEN:1})
The dialplan will need to be reloaded in the Asterisk console. After that, verify that you can call to outside lines by dialing “9” followed by the phone number.