Monday, May 17, 2010

Asterisk CallerID look up script for NZ.

It works with by scraping results from yellow pages, where you can feed it a number and if there is a listing that matchs then it returns the company details.


FreePBX Setup

Go to CallerID Lookup Sources and define your own source as following:
Source type: HTTP
cache results: yes ( populates PhoneBook automatically and saves bandwidth)
Host: localhost
Port: 8071
Path: /
Query: [NUMBER]&${CALLERID(name)}

Go to Inboud Routes:
Set the CID Lookup Source to your own source in all Inbound Routes
Internet Services Setup

Since we are using xinetd, put the following in your /etc/services:
asterisklookup 8071/tcp # asterisk lookup
and put the following in xinetd.conf:
service asterisklookup
{
socket_type = stream
protocol = tcp
wait = no
user = asterisk
group = asterisk
server = /usr/local/bin/callerid.sh
disable = no
}

Then restart the xinetd daemon with...
/etc/init.d/xinetd -restart

Create and install script:
#!/bin/sh

read THE_REQUEST

NUMBER=`echo -n $THE_REQUEST | cut -d "?" -f 2`
NUMBER=`echo -n $NUMBER | cut -d "&" -f 1`
INNAME=`echo -n $THE_REQUEST | cut -d "&" -f 2`
INNAME=${INNAME// HTTP\/?.?/}
INNAME=`echo -n ${INNAME} | /usr/bin/strings`
NUMBER=`echo -n ${NUMBER} | /usr/bin/strings`

echo

if [ ${#NUMBER} == 0 ]
then
NUMBER="Unknown"
else
#national number add leading zero
if [ ${NUMBER:0:1} != "0" ]
then
NUMBER="0"${NUMBER}
fi

#international number two leading zero
if [ ${#NUMBER} = 12 ]
then
NUMBER="0"${NUMBER}
fi

NAME=`cat /root/caller-id.txt | grep -i "${NUMBER}" | cut -d "," -f 2 | cut -b 2-`

#not found in local lookup check unknown file
if [ "$NAME" == "" ]
then
NAME=`cat /root/caller-id-unknown.txt | grep -i "${NUMBER}" | cut -d "," -f 2 | cut -b 2-`
fi

#name not found in unknown list check yellow pages
if [ "$NAME" == "" ]
then
NAME=`lynx -connect_timeout=3 -dump http://yellow.co.nz/search/new+zealand/${NUMBER}-1.html 2>/dev/null | grep -i "tick to compare" -A 3 | grep -v -F "[_]" | cut -s -d "]" -f 2`

#result from yellow pages add to local lookup list
if [ "$NAME" != "" ]
then
echo ${NUMBER}, ${NAME} >> /root/caller-id.txt
else
echo ${NUMBER}, Unknown >> /root/caller-id-unknown.txt
fi
fi

NAME=`echo -n ${NAME} | /usr/bin/strings`

#test result returned from local lookup or yellow pages
if [ "$NAME" == "" ]
then
if [ "$INNAME" != "" ]
then
echo $INNAME
else
echo ${NUMBER}
fi
else
echo ${NAME}
fi

Save as callerid.sh, copy into /usr/local/bin and make it executable...
chmod a+x /usr/local/bin/callerid.sh
Make two blank files for the retrieved and unknown callers numbers to be saved, and set the file permissions, and owner to the asterisk user.
echo > caller-id.txt
echo Unknown, Unknown > caller-id-unknown.txt
sudo chown asterisk:asterisk caller-id*
sudo chmod a+rw caller-id*

Done.

2 comments:

  1. Hi,

    Does this script still work? I've followed the instructions to the letter, and it fails every time.

    Asterisk is def calling the script via xinetd, however I get errors coming back:

    "CALLERID(name)=/usr/local/bin/callerid.sh: line 66: syntax error: unexpected end of file") in new stack

    The script is copied exactly as the above.

    Thanks!

    ReplyDelete
  2. Got it working. Looks like the sites changed a little.

    Changed the looukup part of the script to the below which seems to work (It can probably be tidied up)

    NAME=`/usr/bin/lynx -connect_timeout=3 -dump http://yellow.co.nz/search/new+zealand/${NUMBER}-1.html 2>/dev/null | grep -i "compare" -A 10 | grep -v -F "[_]" | cut -s -d "]" -f 2 |tail -n1|sed '$s/..........$//'`

    ReplyDelete