UPDATE: THIS METHOD NO LONGER WORKS. IF YOU ARE USING FREEPBX, CHECKOUT CID SUPERFECTA WITH GOOGLECONTACTS
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.
- Go to Google Script.
- Click Start Scripting
- Select create script for Blank Project
- Rename the project by clicking “Untitled project” in the top left hand corner and naming “Contacts-for-Asterisk-Phonebook” or whatever you would like.
- Replace the contents of the Code.gs file with this code:
- Save script
- Run > AsteriskPhoneBookMain
- Authorize by clicking “Continue”
- Allow access by clicking “Allow”
Stage Two: Automate Google Script (optional)
We can automate the script to run automatically to generate our contact data file.
- Go to Resources > Current project’s triggers
- Click to add new trigger.
- Setup trigger to run AsteriskPhoneBookMain when/how often you want and click Save.
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.
- Open the Google Drive webpage.
- Go to the “Google Contacts” folder (or whatever you may have modified the script to)
- Right click your phone list file and select Share…
- Go to Advanced
- Select Change…
- Select “On – Anyone with the link”
- Hit Save
- Copy the file ID and save it for later (File ID is located between “/file/d/” and “/view?usp=sharing”)
- 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.)
- Open a SSH session to your Asterisk box
- Create a directory to store the script
# mkdir GoogleContacts
- Change to directory you created
# cd GoogleContacts
- Create PHP file
# nano GoogleAsteriskPhonebook.php
- Paste this script into the PHP file
- Modify line 13 of the PHP script replacing [FILE ID] with the File ID you got from Stage Three, Step 8
- 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.)
- From SSH, execute your PHP script
# php ~/GoogleContacts/GoogleAsteriskPhonebook.php
- 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.
- Open SSH session to Asterisk box
- Start editing crontab
# crontab -e
- 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
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.