Asterisk Dialplan Replace (Remove hash or pound sign from dialled numbers).

Asterisk is a great VoIP server, incredibly stable, scalable and free. If you pay Digium then the support is also prompt, polite and easy to speak to – even free to call over SIP.

I have a problem where the users of the phone systems have learned that you can dial a number and press # and it will dial quicker. Most desk phones treat the # (hash or pound button) as a “submit” command and instantly dial the number instead of waiting for the dial timeout.

Traditional phones remove the # and send the dial request to Asterisk.

This works well except for users who have a Gigaset cordless DECT phone. Gigaset SIP DECT phones don’t have the notion of “off-hook dialling” so the user dials the number, adds a # and then presses the green dial / button.
The Gigaset submits the dial request to Asterisk including the # on the end! If someone dialled an internal extension the call fails. If someone dialled an external number, mostly, the calls work. In some cases when calling a large company or call center (probably due to the equipment at the far end being passed the # too) calls fail.

I’ve been searching for a way to get Asterisk to replace or substitute the # when dialling..

One way Asterisk seriously fails is the documentation. Mostly it has been left to a wiki called voip-info to document stuff and they generally do a great job.

One place the documentation is lacking (even even the voip-info wiki page admits their information is lacking) is information on regex or string replacement. There is a page on the REPLACE function in Asterisk. However I’ve tried many times and never managed to get it working. I remember finding a page a while ago stating that the function was broken at some version of Asterisk and was never fixed. I’ve certainly never had any luck with it.

The next page that might have been useful is the regex page. However this is the page where even the authors admit the documentation is poor.

In the end I combined the regex documentation with this answer on stack overflow to create the answer!
My dialplan now reads the following. This allows only digits 0 through 9 to be sent to the SIP provider.

exten => _9.,1,Set(toDial=$["${EXTEN}" : "([0-9]+)"])
exten => _9.,n,Dial(SIP/voipprovider-out/${toDial:1})
exten => _9.,n,Congestion

Also to fix dialling of internal extensions with a # on the end (e.g. 202#) I changed the extension code to read _202! which means match 202 or 202 with any digits behind it. Sadly this does mean that 20222222 or 202004, 202###*** etc.. will also dial that extension but I prefered a one config line solution with that bug to a multi-line solution that might be more strict.

exten => _202!,1,SIPAddHeader(Alert-Info:Pulse)
exten => _202!,n,Set(__PICKUPMARK=202)
exten => _202!,n,Dial(SIP/202&SIP/202cordless)
exten => _202!,n,Hangup()

As I’ve commented, I think the only way to make it as strict as just 202 and 202# being accepted is to have two lines for each exten entry… one => 202,1, and one => 202#,1, which makes for a double-the-size dialplan for all extensions compared to my above changes which just modifies existing lines.

Hope this helps someone.

This entry was posted in Uncategorized. Bookmark the permalink.

Comment on this topic

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s