How to automatically reject calls from telemarketers and other "junk" callers
Note 1: This is a preliminary version of this page; it may be updated with additional information or to correct errors!
Note 2: The third-party Caller ID Superfecta module also provides a method for rejecting "junk" calls, that might be easier to implement than the method shown here.
This technique uses the service EveryCall.us to look up the numbers of incoming calls that are coming in via selected trunks. If you get a lot of calls from telemarketers and other "junk" callers, this may help you avoid having to deal with them. It's not foolproof, of course, but it will hopefully catch a good percentage of your "junk" calls.
We are using EveryCall.us for two reasons - first, you don't have to register just to do a phone number lookup, second, they have set up a special way to query their service that is very fast and that sends only a minimal amount of information (a "score" associated with a number, or -1 if it's not in their database at all). So we just look at the score and if it's over 10 (you can change that) we send it to the junk call treatment. Here's how to set it up.
Step 1: Make sure curl is installed on your system. From a Linux command prompt, type curl --help
If you get a list of curl options, you are good to go. If not, install curl in the usual manner (e.g. yum -y install curl may work on a CentOS system).
Step 2: Still at the Linux command prompt, enter touch /var/lib/asterisk/agi-bin/check-everycall-us.agi
Step 3: Use a text editor (nano, Midnight Commander's editor, or whatever you like) to edit the /var/lib/asterisk/agi-bin/check-everycall-us.agi file. Copy and paste the following code into it:
#!/bin/bash
BADCALLSCORE="0"
RETRIEVEDSCORE=`/usr/bin/curl -s -m 2 -A Mozilla/4.0 http://www.everycall.us/query\?${1}`
BADCALLSCORE=`echo ${RETRIEVEDSCORE} | grep score | sed -e 's/.*score=//' -e 's/ <br\/>//'`
if [ -z $BADCALLSCORE ]
then
BADCALLSCORE=0
fi
echo "SET VARIABLE badcallscore ${BADCALLSCORE}"
BE SURE NOT TO LOSE ANY BACKTICKS during the copy and paste operation! Be sure to save your changes.
Step 4: Change the permissions and ownership of /var/lib/asterisk/agi-bin/check-everycall-us.agi to be the same as the other .agi files in the /var/lib/asterisk/agi-bin directory. I always use Midnight Commander's "Advanced chown" (under the File menu) to view and change permissions, not being a Linux geek at heart. The main thing is to make sure that the owner and group are set to asterisk, and the owner has read/write/execute permissions.
Step 5. Now use your text editor again - this time you want to open /etc/asterisk/extensions_custom.conf and add the following to the existing file:
[custom-check-everycall-us]
exten => _X!,1,NoOp(Checking Everycall.us for bad caller)
exten => _X!,n,AGI(check-everycall-us.agi|${CALLERID(number)})
exten => _X!,n,NoOp(Everycall.us returned score of ${badcallscore})
exten => _X!,n,GotoIf($[0${badcallscore} < 10]?notbadcaller)
exten => _X!,n,NoOp(Bad caller score above threshold - changing DID to 9999999999)
exten => _X!,n,Goto(from-trunk,9999999999,1)
exten => _X!,n(notbadcaller),NoOp(Bad caller score below threshold - continuing)
exten => _X!,n,Goto(from-trunk,${EXTEN},1)
exten => h,1,Macro(hangupcall,)
Save your changes. Note that the number 10 on the 4th line is the score that determines a "bad" call - if a lot of bad calls are slipping through you can try making this lower. On the other hand, if good calls are getting caught, you may want to make it higher.
Step 6: In the FreePBX "Inbound Routes", create a new Inbound Route that will be used to dispose of the "bad" calls. Make the Description something like Blocked per Everycall.us, and set the DID Number to 9999999999 (leave the Caller ID Number blank). Then select whatever destination you like - I personally like the option "Terminate call: Play Ringtones to the caller until they hangup", but you can do whatever you like, including creating a special IVR just for those folks, if that's your bent. By using an inbound route in this manner, you have additional flexibility for disposing of the call, and you are terminating the call inside the FreePBX dialplan, which may have some implications for call detail recording, etc.
Step 7: In every trunk you want to monitor, you'll need to go into the trunk setup and change the statement context=from-trunk to context=custom-check-everycall-us - the good thing about this method is you can choose to check incoming calls on some trunks but not others. The bad part is you have to change the context= statement in every trunk that you want to check. NOTE: If the context statement is going to someplace other than from-trunk already, you'll need to find the custom dialplan referenced by that statement, and modify that to go to custom-check-everycall-us after it has done its work (instead of going to from-trunk). If that custom dialplan changes the DID of the call then make sure that the EXTEN variable contains the new DID before passing the call to custom-check-everycall-us.
Don't forget to apply your configuration changes when you are all finished!
Please note: The above information should be considered preliminary. The code shown above should still be considered experimental code. Use it at your own risk, and please let us know how it works for you, particularly if you actually receive any calls that score high enough to get "the treatment." Also please watch for any "false positives" - keep in mind that I am NOT a programmer, so don't rely on any of the above code without testing it for yourself!
- Printer-friendly version
- Login or register to post comments



Dead Service from everycall? everycall now always returning -1?
Maybe they stopped offering this service?
Any test numbers that I try which are clearly listed on the site seem to always return -1. Anyone else have this problem?
http://www.everycall.us/query?502-447-4497
Hello Everyone: We recently
Hello Everyone:
We recently started returning (-1) for queries as the system was being abused. Thank you to Chris for contacting us, we're in the process of re-working the API to incorporate some kind of simple API KEY and then we'll post instructions about how to request and API Key.
We're sorry for the inconvenience but are working quickly to get a better solution for the FreePBX community.
Ben