Understand Extension and how to remove parts of incoming number and check database


I will need a bit help with my extension.conf. What I have is realtime asterisk with mysql. In database table sipfriends I’m saving numbers like:

  • 853322
  • 854653
  • 853424
    In this case 85 is prefix. The problem is that the number can come in international format like +85. So, when the caller dial 1234 to check his voicemail I need first to remove the + and/or anything else in front of the prefix 85.

My extension.conf has this so far.


switch => DUNDi/priv


exten => 1234,1,NoOp()
exten => _[+a-z0-9].,2,Set(SAFE_EXTEN=${FILTER(0-9,${CALLERID(num)})})
exten => 1234,2,Set(LANG=${ODBC_LANG(${CALLERID(num)})})

include => dundiextens

exten => _[+a-z0-9].,1,Set(SAFE_EXTEN=${FILTER(0-9,${CALLERID(num)})})
same => n,Voicemail(${SAFE_EXTEN}@VoiceMail)
same => n,Playback(Goodbye)
same => n,Hangup

include => lookupdundi

switch => Realtime


exten =>_[+a-z0-9].,1,Set(SAFE_EXTEN=${FILTER(0-9,${CALLERID(num)})})
same => n,Voicemail(${SAFE_EXTEN}@VoiceMail)
same => n,Playback(Goodbye)
same => n,Hangup

What I can’t understand is where to put this line exten =>_[+a-z0-9].,1,Set(SAFE_EXTEN=${FILTER(0-9,${CALLERID(num)})}) . Currently if I make call from “+8512345” I’ve got

Call from ‘’ (xxx.xxx.xxx.xxx:5060) to extension ‘+8512345’ rejected because extension not found in context ‘default’.

because in database the number is saved as “8512345”.

You put in in the default context, or change the context for that peer.

Are you sure you meant CALLERID(num) and not EXTEN?

But when I save number in database like +8512345 it’s working and vice versa. That’s what I want to accomplish. My number in database is 8512345 when call is received with + in front of number to remove it and look for the “clean” number without + in database.

Scenario 1:
Again currently number is saved like: 8512345
I simulate call from: +8512345
Receiving above error

Scenario 2:

Number is saved like: +8512345
I simulate call from: +8512345
Call is successful

So, I want to save numbers without +. In this case when number with + is trying to make a call I want to remove this + first.

I’ve never used ARA, so I suppose there could be a problem handling “+”. However, your error message would be a secondary error, in that case, the the key error would say not found in the context that was actually associated with the peer.

For example I have following in my extension


exten => _[a-z].,1,Set(SAFE_EXTEN=${FILTER(0-9,${EXTEN})})
same => n,Voicemail(${SAFE_EXTEN}@VoiceMail)
same => n,Playback(Goodbye)
same => n,Hangup

when sip user “8512345” call voicemail number “abc1234” the above SAFE_EXTEN function remove “abc” prefix. I want to do the same BEFORE database lookup if that sip user exists there.


  1. “+8512345” dial “1234”.
  2. function remove “+”
  3. another function ODBC_EXIST then check database.

Use GoTo to restart the dialplan with the unwanted parts of the extension removed.

This GoTo before hit 1234? eg. before this?

exten => 1234,1,NoOp()


This is the same issue as another thread, either a homework or a freelance tender job :wink:

Which other thread? I don’t understand.

(I hadn’t realised it was two different people; I’d assumed it was one person, inappropriately, starting multiple threads.)

They are similar issues but I’m not this man.