You are here

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.

Comments

Awesome, I think I'll integrate this but with a small improvement, change the database table to be cidname_. That way I can use multiple gmail accounts, and also use it in conjunction with other cid lookup and caching scripts.

I've also been pondering using google documents to make a two way bridge between the asterisk database, and something that is easy to access from the web (blacklist, whitelist, distinctive ring, call logs, etc).

This worked great for me--thanks!

I made a couple tweaks to my setup because I'm using SIPGate and the caller ID isn't prefixed with a "+1", so I took the "+" out of the line that begins with "os.system(..." and changed the country_code variable to "".

I also set the Caller ID up by going into FreePBX and creating a lookup source called "Google Contacts" with Source Type internal and cache results on. Then I set up my inbound route to use that as a caller id lookup source.

David Merrick

"a small improvement, change the database table to be cidname_. "
Thanks for this hint! I tried to figure it by myself for 2 months! I see you know things about Drupal and I truly hope you will give more useful advice in comments or guest posts)
__________________
William

Hello! Thanks for the script!
I was looking for something like this for ages.
I have a problem tough. Can you explain the formating options in your script?
I have both belgian (+32) and us (+1) numbers and I can't get it work.
I don't get the
phone.text = re.sub('^\+1|^1|\(|\)|\s|-', '', phone.text)
os.system("asterisk -rx \'database put cidname +1%s \"%s\"\'" % (phone.text,entry.title.text))
part.
Could you develop a little bit? Thanks!

What I am doing there is stripping out the following characters: leading +1, leading 1, (, ), -, and any whitespace. Then in the next line I append a +1 to whatever number I am left with. This will result in numbers like: +15555555555, which match the phone numbers that my asterisk receives calls from.

I uploaded a new version of the script, with some changes that should make your multiple country code usage easier, and the regex modified to remove all non-digits. Hopefully it works on your non US numbers, I guessed on them. I added comments where applicable, let me know how it works.

very nice program, it works perfectly but what if I have my local cardDAV server??

I want to have a global address book editable from my network users. I used carddav.

It is the same as googles but it is locally. Can I somehow change the address of the server???

I'm not sure if its possible, you will have to check the gdata documentation, I didn't see anything when I did a quick search. Another option is to use a modified copy of the gdata cleint which points at your servers instead.

Good morning!

Nice, useful script, very comprehend HOWTO.

Everything worked fine for me, but one problem occured: As I'm working with a couple of russian clients, my gmail-adress-book stores names them in cyrillic letters. By importing these contacts to my *-DB cyrillic letters convert to "Ðлена Ðойнова", for example.

As I'm not familiar with python hope you can propose a workaround.

Best regards,
Sebastian, Germany

This has to be coming from one of two sources:

1. The gdata python client
2. The Asterisk Database

The script itself doesn't modify the names that are returned from google.

I am still getting asterisks as the callerid for all the calls, can you please give me some pointers.

when i run the following command:
database show cidname

OpenWrt*CLI> database show cidname
/cidname/+17407075678 : Testing Tester

Appreciate your help.
Thanks

That looks correct to me. Make sure you added the following to the correct incoming call section of your extensions.conf:
exten => s,n,Set(CALLERID(name)=${DB(cidname/${CALLERID(num)})})

Its also possible that you may need to use a different portion of the CALLERID for your phone, see the available options here: http://www.voip-info.org/wiki/view/Asterisk+func+callerid

Amazing post regarding Sync Google Contacts with Asterisk. Never have I thought it could be implemented. Thanks

Wow, I love this! Fantastic, elegant solution. Thank you!

Everything worked fine for me, but one problem occured: As I'm working with a couple of russian clients, my gmail-adress-book stores names them in cyrillic letters. By importing these contacts to my *-DB cyrillic letters convert to "Ðлена Ðойнова",

Thank you very much for sharing it... I was looking for something similar, and this one just did the job! :P

Thanks a million for this. I just moved ported my mobile number to google voice. I use an Obi110 with asterisk and the callerid comes in without the + symbol. I removed the + from the database put cidname line so all my contacts go in without the plus and callerid matching was perfect.

Great script. I made a little change because the incoming callerid format coming from my VOIP service is unpredictable (+XXY.., 00XXY.., 0Y..). Instead of trying to match the entire number, prefix and country code, I'm stripping the incoming number down to the last 8 digits and matching against that. It works very well.

In the script:
<snip>
phone.text = phone.text[-8:]
phone.text = re.sub('\D', '', phone.text)
os.system("asterisk -rx \'database put cidname %s%s \"%s\"\'" % (country_code,phone.text,entry.title.text))
<snip>

And on the Asterisk side:
exten => s,1,Set(CALLERID(name)=${DB(cidname/${CALLERID(num):-8,8})})

Does anyone knows how to get google voice to work oversea @ thriftywholesaler

Thanks for putting together a great script. This was a fantastic place to start in my quest to sync with Asteridex (for PBX in a Flash).

Here's my modified code (insert after line beginning with "feed = ", and reformat for Python). I don't pretend to be a programmer, but I enjoy learning new things, so this was a fun challenge for me.

# Create a connection to MySQL
# Change the parameters if you are not using PBX in a Flash
con = MySQLdb.connect(host='localhost', user='root', passwd='passw0rd', db='asteridex')
cur = con.cursor()

# delete all of our contacts before we refetch them, this will allow deletions to propogate
cur.execute("DELETE FROM user1")
cur.close()

# re-instantiate the cursor
cur = con.cursor()

# for each phone number in the contacts
for i, entry in enumerate(feed.entry):
for phone in entry.phone_number:
# Strip out any non numeric characters
phone.text = re.sub('\D', '', phone.text)

# Remove leading digit if it exists, we will add this again later for all numbers
# Only if a country code is defined.
if country_code != "":
phone.text = re.sub('^\+?%s' % country_code, '', phone.text)

# Insert the number into the cidname database, reinsert the country code if defined.
name = entry.title.text
num = phone.text
if str(name).strip() != "None":
cur.execute("INSERT INTO user1(`name`, `out`, `email`) VALUES (%s,%s,%s)",(name,num,"NULL"))
print str(name) + " was inserted into the database"

cur.close()
con.close()

Found these guys done it..... waiting for their launch though..

http://aptus.com/phonebook-contacts.html

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Plain text

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