Callerid name

from google contacts


A Great Headline

A good subtitle

Решение проблем с выводом имени звонящего на экран телефона

gdata - Google Data APIs Python Client Library

качаем и ставим
aptitude install mercurial -y 
hg clone
cd gdata-python-client/
в файле src/gdata/ поменять "elif server_response.status == 302:" на "elif server_response.status in (301, 302):" строка 1088
в новых версиях может решат проблему


python install


pip freeze |grep gdata

gdata==2.0.18  - долны увидеть (или выше)

создаем скрипт на питоне для вытаскивания имени контакта из гоогла

vim /etc/asterisk/


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 = ""
        password = "super-puper-password"
        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()
       = email
                gd_client.password = password
                gd_client.source = 'gcontact2ast'
                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"

if __name__ == "__main__":

скачать скрипт можно тут

git clone

проверяем работу скрипта

/etc/asterisk/ 79167193249

если выдал имя то все работает. 

если выдал 

Traceback (most recent call last):
  File "/etc/asterisk/", line 42, in <module>
  File "/etc/asterisk/", line 34, in main
    feed = gd_client.GetContactsFeed(query.ToUri())
  File "/usr/local/lib/python2.7/dist-packages/gdata/contacts/", line 104, in GetContactsFeed
    return self.Get(uri, converter=gdata.contacts.ContactsFeedFromString)
  File "/usr/local/lib/python2.7/dist-packages/gdata/", line 1108, in Get
    'reason': server_response.reason, 'body': result_body}
gdata.service.RequestError: {'status': 301, 'body': '<HTML>\n<HEAD>\n<TITLE>Moved Permanently</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>Mov Permanently</H1>\nThe document has moved <A HREF="">here</A>.\n</BODY>\n</HTML>\n', 'reas': 'Moved Permanently'}

надо искать почему не сработало "elif server_response.status in (301, 302):" 


При входящем звонке для того чтобы установить callerid(name) в астреиске нам надо добавить в диалплан наш следующею строку

exten => _XXXXXX.,1,SET(CALLERID(name)=${SHELL(/etc/asterisk/ ${CALLERID(num)})})

для freePBX добавить надо в extensions_custom.conf

exten => _X!,1,Set(CALLERID(name)=${SHELL(/etc/asterisk/ ${CALLERID(num)})})
exten => _X!,n,Goto(from-trunk,${EXTEN},1)