Integrate Banana Phone scripts
We now come to the conclusion of our Banana Phone build. The scripts are written in Python and will be stored in some new directories we will create within the 'banana' home directory. We'll then add the needed media files for Asterisk to play when a call is received, and finish out with setting up the database for remembering validated caller IDs and some test calls to make sure everything works.
Banana Phone directory structure
We have a few Python scripts to copy into our 'banana' user's home directory, so we will make a few more to better organize things. Let's move into the 'banana' user's home directory and create some more.
Login to your Asterisk server as 'banana', move into the'banana_phone' directory in 'banana' user's home directory and create the following:
Now move into the 'scripts' directory you just created.
Copy Banana Phone scripts
There are eight python scripts we must copy into our directory so Banana Phone will do it's job. They require some database work with SQLAlchemy, so we'll install that now.
I'm gonna just list the python files for copying individually here. I won't go over what the scripts do in detail. That is left as an exercise for the reader.
This defines our database tables within sqlalchemy for a basic call log and white list.
Contains a helper function for creating a session in sqlalchemy for needed database operations.
We will run this script to create up the sqlite database needed for storing the CallLog and WhiteList. Don't run this now.
These are a collection of simple data validation functions for incoming caller ID numbers from Asterisk.
It is by no means extensive, but it covers that basics and is pretty simple to follow
This script validates that a caller ID looks legit enough to be put on the WhiteList. It's used after the call passes the human test.
This saves caller ID info and time it came in to the CallLog table. We don't use it directly in any of the Banana Phone dialplan, but I can't leave this obvious feature out. You should always keep logs.
This adds a caller ID string to the WhiteList table. This is called after the verify_CID script is run to ensure the CID being added is clean.
This does exactly what it sounds like: takes a caller ID number as input, says if it's in the whitelist or not.
Banana Phone generates audio clips with random 4-digit passwords embedded in them. The first part of that process starts with generating the 4-digit code audio and saving it to a place for later retrieval.
Once the auth code audio file is created, this script mixes that in with background music and drops it to the 'asterisk_resource' folder.
This isn't used in the actual Banana Phone logic, but it's a nice little utility script for checking that our CallLog table is being written to correctly.
With a few quick modifications, this script can be used to dump the whitelist entries as well. Just replace every reference to "CallLog" with "WhiteList"
In the same 'scripts' directory, run the setup_database script.
You should now have a database file in the 'banana_phone' directory that will contain our CallLog and WhiteList database records.
Database file and directory permissions
Banana Phone uses Sqlite as it's database for storing call logs and the whitelist. Sqlite requires that the parent folder of the database file also be writable.
I didn't wanna deal with making weird groups and all that bebop, I just set the 'banana_phone' directory and the database files' permissions to 777 (boo, hiss).
Audio files for Banana Phone
I don't think I can legally hand out copies of that song on my site, so we're just gonna use some royalty-free music courtesy of Bensound.com for the background music.
We also need some sound clips of the numbers 0 -9, and some explanatory intro speech and instructional speech, so I used Google's free Text-to-Speech service courtesy of soundoftext.com for those files.
Download and place the following files in the /home/banana/banana_phone/audio_clips/slns directory we made earlier:
Place the following files in the /home/banana/banana_phone/asterisk_resources/sounds
Make sure they're all owned by the 'banana' user.
The Banana Phone dialplan
This is where all Asterisk and our database scripts all come together to create our robocall blocker.
Here is a flow chart showing the logic flow for the Banana Phone dialplan.
I'm not doing any techno-sorcery here. It's really that simple.
Now actually getting Asterisk to do all those tricks is another story.
Asterisk has enough input flexibility within it's dialplan applications to where we can get programmatic call control without using AGI or AMI. And that's great, because the dialplan is the most stable programming option of the three.
* Please note: commands on the dialplan are meant to be on single lines, but the code highlighting generator I used put some of the longer ones on two lines. Just put them as single lines. Lines like 22/23, 25/26, 27/28, 49/50, etc. are supposed to be one line
Now here's the code for the Banana Phone dialplan. Open up /etc/asterisk/extensions.conf and replace our original, minimalist dialplan with this:
On line 18, replace "5555555555" with the 10-digit phone number of the phone line plugged into the Obi ATA.
There a couple subtle dialplan nuances used here, particularly when generating the audio files and when the caller is actually entering in the input from their phone. I will leave that for the reader to dissect if they feel so inclined. Google is your friend, and so is .
I also peppered in a bunch of NoOp() calls as debug messages you can see in the console.
Testing here is pretty straight forward.
- Call the phone line attached to your Obi ATA
- Follow the instructions
- Input the code
- See if it rings
- If it does, call again, you shouldn't have to test twice
I've tried to write an easy-to-follow tutorial, with little debug checks at the end of every section. However, it's easy to miss steps or forget stuff, so if your install doesn't work right the first time you try, go through these steps:
- Are you able to see calls coming into Asterisk?
- Try running the minimal dialplan from section 2 and see if calls come in. If not, try redoing the configurations on Asterisk and the Obi ATA until you can.
- If verified caller IDs keep getting tested, your database probably isn't getting written to.
- Are the permissions on the banana_phone.db file and it's parent directory /home/banana/banana_phone set correctly?
- If your Obi ATA isn't registering correctly with Asterisk,
- Make sure your IP address, usernames and passwords for our SIP endpoints are correct
- Restart Asterisk and check the status page on the Obi
- The Asterisk debug console is your friend. Use it to check how calls are being processed, where they mess up in the dialplan, and if your ATA is connected
- When editing extensions.conf, you must reload the dialplan from the Asterisk debug console for changes to take affect. Remember to use this command at the debug console:
That's it for making Banana Phone.
I'm a one-man army trying to fight phone scams. I'm also in the middle of commercializing my idea, so if this tutorial helped solve your robocall problem or you got some legitimate good information from this, please consider donating to the project. Beer money goes along way ;)