You are here

Add new comment

Sync Google Contacts with Asterisk

I posted a few weeks back about a SIP project I had been working on, this is the first in (hopefully) a series of posts about neat things I am doing with this setup. Spoiled by my Android phone, the Caller ID on my "house phone" wasn't up to par, so I set out to fix this.

For those of you unfamiliar with my Asterisk setup, after Mario showed me his setup I set out to join in the fun. I picked up the following:

Now onto the actual point of this post, I created a Python script which can be run on your Asterisk box to sync up your Google contacts. The script will retrieve all of your Google contacts, normalize their numbers and store them into the Asterisk Databse. Then you can then perform a lookup on the database when incoming calls occur.

First you will need to get the gdata-python-client in order to access your Google contacts. The install directions should handle everything you need to know. On my OpenWrt router I had to install the packages by hand to /usr/lib/python2.6, and needed to install the python-expat and python-openssl packages.

Now that you have the python client installed, download my script to a location of your choice and make it executable (chmod +x). Edit the script and insert your Google email address and password into the proper locations. Now execute the script and you should see feedback indicating that your contacts are being added to the Asterisk Database.

All of your Google contacts should now be in the Asterisk Database, lets verify that by running the following:
root@moonbaseone:~# asterisk -r
=========================================================================
Connected to Asterisk 1.6.2.6 currently running on moonbaseone (pid = 1790)
moonbaseone*CLI> database show cidname

Once you have verified your contacts are appearing in the database, lets move on and have Asterisk use these as Caller ID Names on incoming calls. Add the following to the incoming call portion of your /etc/asterisk/extensions.conf file:
exten => s,n,Set(CALLERID(name)=${DB(cidname/${CALLERID(num)})})

Then reload the extensions:
root@moonbaseone:~# asterisk -r
=========================================================================
Connected to Asterisk 1.6.2.6 currently running on moonbaseone (pid = 1790)
moonbaseone*CLI> dialplan reload
Dialplan reloaded.

Your incoming calls should now display the name located in your Google contacts if the phone numbers match. At this point I would recommend adding the script to a daily cronjob, so your changes continue to sync. If you are on an OpenWrt system you may want to move your Asterisk Database to a permanent location so you don't lose it if you ever reboot. To do this simply modify /etc/init.d/asterisk and replace:
[ -h $DEST/usr/lib/asterisk/astdb ] || ln -sf /var/spool/asterisk/astdb $DEST/usr/lib/asterisk/astdb
with:
[ -h $DEST/usr/lib/asterisk/astdb ] || rm /var/spool/asterisk/astdb

It should be noted that my phone number normalizing regex only handles numbers that I am familiar with, so US numbers. If it doesn't work with your numbers or you had to tweak something, leave a comment below.

UPDATE: If you recently upgraded to Asterisk 1.8 and started using the native Google Voice support then you will notice that this method no longer works with your calls. The Google Voice CallerID is passed in a different way, luckily it is an easy work around, head over to Mario Limonciello's blog and check out his solution.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.