So, now I’ve got this shiny new Milestone with Android 2.1 on board. And as a consequence of this fact, I have up2date contacts on google to sync the address book with. I also have a voicemail implementation on my own asterisk that sends me an email with wav attached in case of a missed call ( using voicemail “redirect” of the provider with my own number ). Why not to use both of these and get some fun like I was getting with Google Calendar and Asterisk?

So here we go, first of all I do it in python because there is a library ready to use and coding in python is fast. you gonna need to download and setup.py this.

The “program” is trivial when you know how it works and it is also very quick and dirty, “works for me” style:

#!/usr/bin/python
import atom,re,sys
import gdata.contacts
import gdata.contacts.service

def PhoneInFeed(feed,nphone):
  for i, entry in enumerate(feed.entry):
    for phone in entry.phone_number:
      if re.findall(nphone,phone.text):
        return entry.title.text
  return False

def main():
        email = "[email protected]"
        password = "and your pass"
        phone = sys.argv[1]
        name = None
        #here I check that the number is more than 4 digits
        #otherwise it is to simple and will match too much
        if phone.__len__() > 4:
                #and here i'd like to remove leading zeroes, pluses and country codes
                #so if you have two contacts with the same numbers but in different areas
                #then you will get what you ask for :)
                phone = re.sub('^[0+]+[0-9]{2}', '', phone)
                gd_client = gdata.contacts.service.ContactsService()
                gd_client.email = email
                gd_client.password = password
                gd_client.source = 'gcontact2ast'
                gd_client.ProgrammaticLogin()
                query = gdata.contacts.service.ContactsQuery()
                #oh, yeah I know, and don't care to get 1k of results (189 here and works)
                query.max_results = 1000

                feed = gd_client.GetContactsFeed(query.ToUri())
                name = PhoneInFeed(feed,phone)
        if not name:
                #here is how I call the unknown numbers calling in
                name = "Caller"
        sys.stdout.write(name)

if __name__ == "__main__":
        main()

and it can be used like this:

> ./mycontracts.py 333444555
A contact

At this point you should be able to find a contact name by a phone numer. So far so good. How would you use it with Asterisk? Well, there is more than one way to do it but I have app_backticks and I’m not afraid to use it, so my dialplan looks like this:

[somewhere-in-voicemail]
exten => s,2,BACKTICKS(CALLERID(name)|/usr/local/bin/mycontacts.py ${CALLERID(num)})
;the above sets callerid name to what was found
exten => s,3,Swift(Lawrence^Hello ${CALLERID(name)}, Please leave your message for Michael after the tone)
;and the above prompts the caller with his name (oh boy!) using cepstral TTS
exten => h,1,System(  sendfilevoicemail.pl  'New voicemail for Michael from ${CALLERID(all)}' /tmp/${CALLERID(num)}-${TIMESTAMP}.wav & )
;and at some point i send the file to where it belongs

And my callers get prompted by name if found and also I get email that has the name in subject.

Googluck!

  • Olli

    Sorry, I get this error:


    File “contacts.py”, line 20
    if phone.__len__() > 4:
    ^
    SyntaxError: invalid syntax

    Any ideas?

  • http://www.gfdsa.org gfdsa

    try this one:
    http://voip.gfdsa.org/mycontacts.py
    to avoid copy/paste or formatting problems

    python -V
    Python 2.4.3
    here

  • Max

    Take a look here, I liked that solution too, wonder when are they going to launch:

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

Post Navigation