LookupBlackList Alternative

In a previous post, I mentioned that I came up with a solution for the disadvantages to the LookupBlackList Application. This could also be viewed as an alternative solution, seeing how the LookupBlackList has been depreciated, and set to be removed for a future release of Asterisk. Let’s not waste any time. Let’s get right into it.

I previous mentioned a disadvantage being that you can’t block calls per destination DID or phone number. This was a problem for me. I wanted to be able to block callers from calling one of the phone numbers in my PBX, but not another number. So I came up with the following solution. You can modify the solution according to your requirements. I’ll give two ways of doing this, in addition to simple alternative for LookupBlackList – so it functions the same way.

First off, we can store our numbers to be blocked within a SQL database or AstDB. This will determine which method we’ll be using. We’ll start with AstDB.We also need to determine how we want to filter the callers: By caller id name, or caller id number? I personally like the number. This is generally the most unique caller id characteristic.



In this example, we have given each DID an unique id. For this example, I’m going to call it a ‘company’ (assuming each DID is a different company.)

Let’s add our callers to be blocked into AstDB:


database put <companyid> <numberblocked> 1
database put 100 4801115432 1
database put 150 6023321444 1

Now, let’s put together our dialplans:


exten => 18003337123,1,Set(COMPANY=100)
exten => 18003337123,2,Set(CID=${CALLERID(num)})
exten => 18003337123,3,Gotoif($["${DB(${COMPANY}/${CID})}" = "1"]?goaway,1,1)
exten => 18003337123,4,Queue(CSR1)

exten => 18004447123,1,Set(COMPANY=150)
exten => 18004447123,2,Set(CID=${CALLERID(num)})
exten => 18004447123,3,Gotoif($["${DB(${COMPANY}/${CID})}" = "1"]?goaway,1,1)
exten => 18004447123,4,Queue(CSR1)

[goaway]
exten => 1,1,Playback(not-taking-your-call)
exten => 1,2,Playback(vm-goodbye)
exten => 1,3,Hangup()

Now, to example the above dialplan example… We first create a variable ‘COMPANY’ – this is where we set a Company ID for this DID. Second priority, we set the CallerID Number. We are only taking the number from the caller id, because that’s what we’res blocked based on. If we were blocking based on the name, we would change that to show: CALLERID(nam)  – I have not successfully got it to work with CALLERID(all). Third priority, we check to see if the calling party is added to the AstDB for blocking. If so, it will jump to the ‘goaway’ context, to playback a message telling the caller they are blocked. Otherwise it will continue to the 4th priority, where it enters the caller in the queue.   The important thing is, that it’s only looking for blocked calls setup in AstDB for that Company ID. This gives you more control over the blocking per phone number (DID.) That’s it. Pretty simple.

Now, let’s assume that we want to use MySQL for doing the samething. We can do the same thing. I’m one of those that likes to use MySQL for everything. This allows me to create a PHP/MySQL GUI for easy management. I haven’t tested how resource intense this is, so use at your own risk.

Let’s create our database and table first:


CREATE DATABASE asterisk;
GRANT ALL PRIVILEGES ON asterisk.* TO 'asterisk'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;

CREATE TABLE IF NOT EXISTS `blacklist` (
`company_id` varchar(3) NOT NULL,
`phonenum` varchar(30) NOT NULL,
PRIMARY KEY  (`company_id`,`phonenum`),
FULLTEXT KEY `phonenum` (`phonenum`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Now, we can add our blocked numbers. Again, this relies on the company id for it to function.


INSERT INTO `asterisk`.`blacklist` (`company_id`, `phonenum`) VALUES ('100', '16021231141');
INSERT INTO `asterisk`.`blacklist` (`company_id`, `phonenum`) VALUES ('150', '14801231141');

Now let’s setup the dialplan:


[incoming]
exten => 18003337123,1,Set(DID=${EXTEN})
exten => 18003337123,2,Set(CID=100)
exten => 18003337123,3,Goto(cblock,1,1)
exten => 18003337123,5,Queue(CSR1)

exten => 18004447123,1,Set(DID=${EXTEN})
exten => 18004447123,2,Set(CID=150)
exten => 18004447123,3,Goto(cblock,1,1)
exten => 18004447123,5,Queue(CSR1)

[cblock]
exten => 1,1,MYSQL(Connect connid localhost asterisk yourpassword asterisk)
exten => 1,2,MYSQL(Query resultid ${connid} SELECT\ 'phonenum'\ FROM\ 'blacklist'\ WHERE\ 'company_id'\='${CID}'\ AND 'phonenum'\='${CALLERID(num)}'\)
exten => 1,3,MYSQL(Fetch fetchid ${resultid} BLACKLIST)
exten => 1,4,MYSQL(Clear ${resultid})
exten => 1,5,MYSQL(Disconnect ${connid})
exten => 1,6,GotoIf($["${CALLERID(num)}" = "${BLACKLIST}"]?goaway,1,1:cblock,1,8)
exten => 1,7,Goto(incoming,${DID},5)

[goaway]
exten => 1,1,Playback(not-taking-your-call)
exten => 1,2,Playback(vm-goodbye)
exten => 1,3,Hangup()

So in the above example we first set the company extension or DID in a variable, so we can redirect our dialplan back to it later. Then we set the company id as a variable. Next we forward to the call blocking context to see if the caller is allowed through. We query the MySQL database for the phone number. If it appears in the database for the company id, we forward them to the ‘go away’ message/context. If it doesn’t appear, we redirect them back to the next step in the DID’s dialplan.

What if you want to filter for all the phone numbers (DIDs) coming into Asterisk? Okay, so you can simply use the following:

Use AstDB – it will be quicker. Use the following to input the numbers into the blacklist:


database put blacklist <blockednumber> 1
database put blacklist 4801115432 1
database put blacklist 6023321444 1

And your dialplan will be as follows:


exten => 18003337123,1,Set(CID=${CALLERID(num)})
exten => 18003337123,2,Gotoif($["${DB(blacklist/${CID})}" = "1"]?goaway,1,1)
exten => 18003337123,3,Queue(CSR1)

exten => 18004447123,1,Set(CID=${CALLERID(num)})
exten => 18004447123,2,Gotoif($["${DB(blacklist/${CID})}" = "1"]?goaway,1,1)
exten => 18004447123,3,Queue(CSR1)

[goaway]
exten => 1,1,Playback(not-taking-your-call)
exten => 1,2,Playback(vm-goodbye)
exten => 1,3,Hangup()

I hope this is useful to someone. I would like to hear your comments. :-)


Leave a Reply