Callerid name

from google contacts

Alexcr2

A Great Headline

A good subtitle

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

пакеты
gdata - Google Data APIs Python Client Library https://code.google.com/p/gdata-python-client/downloads/list

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

установка

python setup.py install

проверяем

pip freeze |grep gdata

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

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

vim /etc/asterisk/contacts.py

содержимое

#!/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 = "alexcr.telecom@gmail.com"
        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()
                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()

скачать скрипт можно тут https://bitbucket.org/Alexcr/asterisk-google/overview

git clone https://Alexcr@bitbucket.org/Alexcr/asterisk-google.git

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

/etc/asterisk/contacts.py 79167193249

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

если выдал 

Traceback (most recent call last):
  File "/etc/asterisk/google-contacts.py", line 42, in <module>
    main()
  File "/etc/asterisk/google-contacts.py", line 34, in main
    feed = gd_client.GetContactsFeed(query.ToUri())
  File "/usr/local/lib/python2.7/dist-packages/gdata/contacts/service.py", line 104, in GetContactsFeed
    return self.Get(uri, converter=gdata.contacts.ContactsFeedFromString)
  File "/usr/local/lib/python2.7/dist-packages/gdata/service.py", 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="https://www.google.com/m8/feeds/contacts/default/full?max-results=1000">here</A>.\n</BODY>\n</HTML>\n', 'reas': 'Moved Permanently'}

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

Asterisk

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

[from-internal]
exten => _XXXXXX.,1,SET(CALLERID(name)=${SHELL(/etc/asterisk/contacts.py ${CALLERID(num)})})

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

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