Asterisk block incoming call from matching number prefix

1.5k views Asked by At

I am trying to make a dial plan to block incoming calls from any number starting with 88

Here is my dial plan

exten => _X.,1,NoOp(${CALLERID(num)})
same => n,Set(regx=^(88)[0-9]$)
same => n,GotoIf($[${REGEX("${regx}" ${CALLERID(num)})} = 1]?blocked,s,1)
same => n,Dial(SIP/8.8.8.8/${EXTEN}

[blocked]
exten => s,1,Wait(9)

At the moment if I test call with Caller ID 88 it blocks the call successfully but if the call comes from 88XXXXXXX it does not.

3

There are 3 answers

0
arheops On

Asterisk dialplan is regexp itself. Why you use other regexp?

exten => _X.,1,NoOp(${CALLERID(num)})
same => n,Gosub(cid-blacklist,${CALLERID(num)},1)
same => n,Dial(SIP/8.8.8.8/${EXTEN}
[cid_blacklist]
exten => _88.,1,Noop(bangladesh)
same => n,Wait(100)
; this works only when not found match in context.
include=> cid_blacklist_not_found
[cid_blacklist_not_found]
exten => _X.,1,Return;not found in cid blacklist
0
J B On

Why do you use the complex REGEX function? Just do something like that:

exten => _X.,1,NoOp(CallerID is: ${CALLERID(num)})
exten => _X.,n,Set(number=${CALLERID(num)})
exten => _X.,n,ExecIf($[${number:2} = 88],Hangup())
exten => _X.,n,NoOp(Call is being continued)

This will save the callerid num into the "number" variable. Then it will check if the first 2 characters of the variable are equal to 88. If so, the call will be hung up. You can also use GoToIf instead of ExecIf if you want to send the blacklisted call to a specific context where you want to do something else with it.

0
iuri On

tl;dr

Remove first three lines from your dialplan and add the following line to your dialplan to block all callers with numbers beginning with "88":

exten => _X./_88.,1,Goto(blacklisted,s,1)

, so your dialplan looks like this:

[default]
exten => _X.,1,Dial(SIP/8.8.8.8/${EXTEN})
exten => _X./_88.,1,Goto(blacklisted,s,1)
[blacklisted]
exten => s,1,Wait(9)

More details

Approach

According to Asterisk Pattern Matching you can define your exten pattern considering the Caller ID.

Write your dialplan

Since your requirement is to blacklist particular Caller ID numbers (in your case numbers starting with "88"), you could add the following line to your context:

exten => _X./_88.,1,Goto(blacklisted,s,1)

, so your extensions.conf looks as shown above.

Reload your dialplan

Type dialplan reload on your asterisk cli:

asthost*CLI> dialplan reload

Check your dialplan

Type dialplan show on your asterisk cli:

asthost*CLI> dialplan show
[ Context 'default' created by 'pbx_config' ]
  '_X.' (CID match '_88.') =>  1. Goto(blacklisted,s,1)                      [extensions.conf:3]
  '_X.' =>          1. Dial(SIP/8.8.8.8/${EXTEN})                 [extensions.conf:2]

[ Context 'blacklisted' created by 'pbx_config' ]
  's' =>            1. Wait(9)                                    [extensions.conf:5]

Test your dialplan

For testing, you should be able to manipulate the Caller ID for incoming calls. Start a call from your SIP endpoint or originate a call via callfiles and local channels. As a shortcut, you can generate the contents of the callfile and send it to asterisk's outgoing spool directory by running the echo linux shell command from the asterisk cli. Don't forget to set verbose level to 3, so you can see the dialplan output.

asthost*CLI> core set verbose 3
Console verbose was OFF and is now 3.
asthost*CLI> !echo 'Channel: Local/12345@default\nCallerid: "someone" <880123456>\nWaitTime: 20\nApplication: Hangup\n' > /var/spool/asterisk/outgoing/call.file
    -- Attempting call on Local/12345@default for application Hangup() (Retry 1)
    -- Called 12345@default
    -- Executing [12345@default:1] Goto("Local/12345@default-00000023;2", "blacklisted,s,1") in new stack
    -- Goto (blacklisted,s,1)
    -- Executing [s@blacklisted:1] Wait("Local/12345@default-00000023;2", "9") in new stack

Considerations

Keep in mind that the caller can provide his or her number in different number formats like "+88...", so your Caller ID filter "_88." will not match.