Google Contacts to Asterisk Phonebook

The Project

I wanted to have my Google Contacts be used to identify Caller ID for numbers coming into my Asterisk PBX. All the existing scripts I found were outdated and broken or required many hoops to jump through to get it working. So I put together a Google Script and a PHP script to get this all working. So let’s get to it.

Stage One: Google Script

The first stage to this working is a Google Script. It essentially grabs your Google Contacts and saves them to a data file on your Google Drive to be imported by our PHP script later on.

So let’s create this Google Script.

  1. Go to Google Script.
  2. Click Start Scripting
  3. Select create script for Blank Project
  4. Rename the project by clicking “Untitled project” in the top left hand corner and naming “Contacts-for-Asterisk-Phonebook” or whatever you would like.
    Rename
  5. Replace the contents of the Code.gs file with this code:
  6. Save script
    Save
  7. Run > AsteriskPhoneBookMain
    Screenshot 2015-10-04 01.41.17
  8. Authorize by clicking “Continue”
    Authorize
  9. Allow access by clicking “Allow”
    Authorize 2

Stage Two: Automate Google Script (optional)

We can automate the script to run automatically to generate our contact data file.

  1. Go to Resources > Current project’s triggers
    Screenshot 2015-10-04 01.49.41
  2. Click to add new trigger.
    Trigger 1
  3. Setup trigger to run AsteriskPhoneBookMain when/how often you want and click Save.
    Trigger 2

Stage Three: Make Google Drive File Accessible to Asterisk

Now we are going to change the sharing on our created Google Drive file so that it can be downloaded by our Asterisk box.

  1. Open the Google Drive webpage.
  2. Go to the “Google Contacts” folder (or whatever you may have modified the script to)
  3. Right click your phone list file and select Share…
    Share
  4. Go to Advanced
    Advanced
  5. Select Change…
    Change
  6. Select “On – Anyone with the link”
    Public
  7. Hit Save
    Save
  8. Copy the file ID and save it for later (File ID is located between “/file/d/” and “/view?usp=sharing”)
    File ID
  9. Click Done

Stage Four: Asterisk PHP Script

Now the PHP script that will download the contact list from our Google Drive and then replace the Asterisk Phonebook with our Google Contacts. (Notice: this script deletes all phonebook records on every run.)

  1. Open a SSH session to your Asterisk box
  2. Create a directory to store the script
    # mkdir GoogleContacts
  3. Change to directory you created
    # cd GoogleContacts
  4. Create PHP file
    # nano GoogleAsteriskPhonebook.php
  5. Paste this script into the PHP file
  6. Modify line 13 of the PHP script replacing [FILE ID] with the File ID you got from Stage Three, Step 8
  7. Save PHP file.

Stage Five: Run PHP Script and Verify

Now you’ve got the two parts setup and ready you can execute the PHP script and see your contacts added to Asterisk. (Notice: this script deletes all phonebook records on every run.)

  1. From SSH, execute your PHP script
    # php ~/GoogleContacts/GoogleAsteriskPhonebook.php
  2. Verify contacts were added
    # asterisk -rx "database show cidname"

Stage Six: Automate PHP Script (optional)

If we automated the Google Script, we might as well automate the PHP script to run automatically to keep our Asterisk Phonebook up to date. We will do this through cron.

  1. Open SSH session to Asterisk box
  2. Start editing crontab
    # crontab -e
  3. Add line to run script (This will run the script on the 10th minute of every hour of every day) [Hit I to Insert; to exit & save: hit Esc, Shift + Z, Shift + Z]
    10 * * * * /usr/bin/php /root/GoogleContacts/GoogleAsteriskPhonebook.php >/dev/null 2>&1

Wrap-up

Now with the Google Script gathering contact data and saving it to your Google Drive you can avoid the issue with OAUTH beingneeded for scripts to authenticate to your Google Account or scripts broken due to changes in APIs. The PHP script just downloads and interprets all that data and adds it to the Asterisk Phonebook. If you automate both of them as outlined then you can just set it up and just forget about it.

The only thing to make sure you have set now is that your Inbound Routes use Asterisk Phonebook for their Caller ID lookups either directly or add it to your CID Superfecta plan and have that set as your CID source.

Enjoy!

Share This:

4 thoughts on “Google Contacts to Asterisk Phonebook

  1. Hi

    I have contacts that have a ‘ in their name like “ros’nbrg”
    The php script doesn’t like it he complaint about it and he doesn’t import them

    Any idea how to fix it?

  2. Hi
    thank you for this post its working well. I am having 1 issue that any name that has a ” ‘ ” in the name like rose’brg It will not import the contact

  3. Hello, I am trying your script.
    I followed step by step procedure.

    when I run the

    php ~ / GoogleContacts / GoogleAsteriskPhonebook.php

    It is notified of the error

    “PHP Warning: stream_get_contents (): SSL: fatal protocol error in /root/GoogleContacts/GoogleAsteriskPhonebook.php on line 21”.

    Can you help me?
    Thank you

  4. Dear MrCaseyJames, really nice and very helpful tutorial. i am using it for over 3 months and it rocks. I am trying to alter your script to add conversion of greek characters to latin but i am lost. i have a few installations in friends that they do have their google contacts in with greek characters. i have the following without success

    //just to be sure that i will get only latin letters and numbers at the end. all spaces get a –
    $str = str_replace(” “,”-“,$original_string);
    $str = preg_replace(“/[^a-zA-Z0-9]/”, “”, $str);

    $str = str_replace(array(“?”,”?”,”?”,”?”),’a’,$str);
    $str = str_replace(array(“?”,”?”),’b’,$str);
    $str = str_replace(array(“?”,”?”),’g’,$str);
    $str = str_replace(array(“?”,”?”),’d’,$str);
    $str = str_replace(array(“?”,”?”,”?”,”?”),’e’,$str);
    $str = str_replace(array(“?”,”?”),’z’,$str);
    $str = str_replace(array(“?”,”?”,”?”,”?”),’i’,$str);
    $str = str_replace(array(“?”,”?”),’th’,$str);
    $str = str_replace(array(“?”,”?”,”?”,”?”,”?”,”?”,”?”),’i’,$str);
    $str = str_replace(array(“?”,”?”),’k’,$str);
    $str = str_replace(array(“?”,”?”),’l’,$str);
    $str = str_replace(array(“?”,”?”),’m’,$str);
    $str = str_replace(array(“?”,”?”),’n’,$str);
    $str = str_replace(array(“?”,”?”),’ks’,$str);
    $str = str_replace(array(“?”,”?”,”?”,”?”),’o’,$str);
    $str = str_replace(array(“?”,”?”),’p’,$str);
    $str = str_replace(array(“?”,”?”),’r’,$str);
    $str = str_replace(array(“?”,”?”,”?”),’s’,$str);
    $str = str_replace(array(“?”,”?”),’t’,$str);
    $str = str_replace(array(“?”,”?”,”?”,”?”,”?”,”?”,”?”),’u’,$str);
    $str = str_replace(array(“?”,”?”),’f’,$str);
    $str = str_replace(array(“?”,”?”),’x’,$str);
    $str = str_replace(array(“?”,”?”),’ps’,$str);
    $str = str_replace(array(“?”,”?”,”?”,”?”),’w’,$str);

Leave a Reply

Your email address will not be published. Required fields are marked *