Reverse caller lookup with asterisk and tel.search.ch

Reverse caller lookup with asterisk and tel.search.ch

To cut the fixed telephone line, I use guest-voip.ch as SIP provider, an asterisk open source PBX installed on my QNAP NAS and some Apple devices (Iphone, IPad and MBP) as phone clients.

Asterisk allows to execute external scripts, so called AGI scripts, to add additional functionality to the PBX. In my case, I use this interface to call a bash script, which lookups the incoming call number in a local text file (cache). If it doesn’t exist in the file, it calls a php script on the local webserver which opens a connection to tel.search.ch with the number as GET parameter and retrieves the search result as answer. The php script then parses the answer, removes any html tags and returns that value to the bash script. The bash script adds the value to the local cache file and returns it back to asterisk.

Additional asterisk configuration (extensions.conf):

exten = s,1,Set(CHANNEL(language)=de)
exten = s,2,AGI(lookup.agi, ${CALLERID(num)})
exten = s,3,Set(CALLERID(name)=${LONGNAME})

Bash script (stored in /var/lib/asterisk/agi-bin):

#!/bin/sh
#
#read agi_request
#read agi_language
#read agi_channel
#read agi_type
#read agi_uniqueid
#read agi_callerid
#read agi_dnid
#read agi_rdnis
#read agi_context
#read agi_extension
#read agi_priority
#read agi_enhanced
#read agi_accountcode
#read emptyline

#pfad zum cachefile
CACHE="/var/spool/asterisk/invsuche_cache"

#pfad um das tempfile anzulegen
TMPFILE="/tmp/tmpsuche.html"
TMPFILE2="/tmp/tmpclir"
LOG="/var/log/asterisk/anrufliste_log"

echo "$1-$2-$3" >/tmp/reverse.tmp

if [ "$1" = " " ] || [ -z "$1" ]; then
echo "Keine Nummer"
#echo | tail -n 10 /var/log/syslog | grep "RING (" >>$TMPFILE2
#if [ "`tail -c 10 $TMPFILE2`" = "z audio) " ]; then
NAME="analoger Anrufer"
DETAILS="Keine details"
#fi
#if [ "`tail -c 10 $TMPFILE2`" = "(Speech) " ]; then
# NAME="aktiv unterdrueckt"
# DETAILS="ISDN anrufer ohne Nummer"
#fi
else
NUMMER=`echo $1 | sed -e "s/\ //g" -e "s/+41/0/"`
echo "Suche nach $NUMMER im cache"
NAME=`awk -F '---' '{ if ($1 == "'$NUMMER'") print $2 }' $CACHE`
if [ -z "$NAME" ]; then
wget -q --tries=3 --timeout=5 -O $TMPFILE "http://gnas/asterisk/telsearch.php?tel=$NUMMER"
NAME=`cat $TMPFILE`
if [ ! -z "$NAME" ]; then
printf "$NUMMER---$NAME\n" >> $CACHE
fi

fi

if [ -z "$NAME" ]; then
NAME="$NUMMER"
fi
fi

###
### Here you can add "additional alert code"
###

# directly source an external scriptlet, for better separation with this publicly updated script
#. /usr/local/asterisk/reverse.agi_notifier_sh

printf "`date +%Y-%m-%d\ %H:%M` $NAME\t$NUMMER\n" >>$LOG
echo 'SET VARIABLE LONGNAME '"\"$NAME\"" >/dev/stdout
read in

exit 0

Php file (uses snoopy library):

<?php
$number = $_GET["tel"];

$url = "http://tel.search.ch/?tel=".$number; // + evtl. Übergabeparameter
include "Snoopy.class.php";
$snoopy = new Snoopy;

$snoopy->fetch("$url");

$GrabStart = '<h5>';
$GrabEnd = '</h5>';

$GrabData = eregi("$GrabStart(.*)$GrabEnd", $snoopy->results, $output1);
$output1[1] = str_replace("ö", "oe", $output1[1]);
$output1[1] = str_replace("Ö", "Oe", $output1[1]);
$output1[1] = str_replace("ä", "ae", $output1[1]);
$output1[1] = str_replace("Ä", "Ae", $output1[1]);
$output1[1] = str_replace("ü", "ue", $output1[1]);
$output1[1] = str_replace("Ü", "Ue", $output1[1]);
$output1[1] = str_replace(",", "", $output1[1]);

# HTML Code entfernen und Zeilenumbruch einfügen
$output1[1] =preg_replace('/(\\s+)/', ' ',$output1[1] );
$output1[1] = strip_tags($output1[1]);
$output1[1] = str_replace("ZZZ", "\n", $output1[1]);
$name= explode("\n", wordwrap($output1[1], 20));

echo $name[0];

?>

The following sources helped me bringing this up in a really short time:
Reverse lookup configuration in Germany
Asterisk Gateway Interface
trixbox mit Anzeige des Anrufenden via tel.search.ch

3 Replies to “Reverse caller lookup with asterisk and tel.search.ch”

  1. Hallo David,
    ich habe dein Script ein wenig auf mein Astlinux Asterisk angepasst da ich Asterisk auf eine CF Card installiert habe nun funktioniert alles bis auf das eigentlich was das Script machen soll Anrufername wird nicht angezeigt.
    ich habe einige Pfade abgeändert ins /tmp da ich keine Schreibzugriffe auf die CF karten machen sollte.

    im CLI bekomme ich folgendes:
    — Called 15@15
    — Executing [15@15:1] Set(“Local/15@15-6c26;2”, “CHANNEL(language)=de”) in new stack
    — Executing [15@15:2] AGI(“Local/15@15-6c26;2”, “lookup.agi, 0796003185”) in new stack
    — Launched AGI Script /var/lib/asterisk/agi-bin/lookup.agi
    — AGI Script lookup.agi completed, returning 0
    — Executing [15@15:3] Set(“Local/15@15-6c26;2”, “CALLERID(name)=”) in new stack
    — Executing [15@15:4] Dial(“Local/15@15-6c26;2”, “SIP/15,10,r”) in new stack

    mein lookup.agi

    #!/bin/sh
    #
    #read agi_request
    #read agi_language
    #read agi_channel
    #read agi_type
    #read agi_uniqueid
    #read agi_callerid
    #read agi_dnid
    #read agi_rdnis
    #read agi_context
    #read agi_extension
    #read agi_priority
    #read agi_enhanced
    #read agi_accountcode
    #read emptyline

    #pfad zum cachefile
    CACHE=”/tmp/invsuche_cache”

    #pfad um das tempfile anzulegen
    TMPFILE=”/tmp/tmpsuche.html”
    TMPFILE2=”/tmp/tmpclir”
    LOG=”/tmp/anrufliste_log”
    echo “$1-$2-$3” >/tmp/reverse.tmp
    if [ “$1″ = ” ” ] || [ -z “$1” ]; then
    echo “Keine Nummer”
    #echo | tail -n 10 /var/log/syslog | grep “RING (” >>$TMPFILE2
    #if [ “`tail -c 10 $TMPFILE2`” = “z audio) ” ]; then
    NAME=”analoger Anrufer”
    DETAILS=”Keine details”
    #fi
    #if [ “`tail -c 10 $TMPFILE2`” = “(Speech) ” ]; then
    # NAME=”aktiv unterdrueckt”
    # DETAILS=”ISDN anrufer ohne Nummer”
    #fi
    else
    NUMMER=`echo $1 | sed -e “s/\ //g” -e “s/+41/0/”`
    echo “Suche nach $NUMMER im cache”
    NAME=`awk -F ‘—‘ ‘{ if ($1 == “‘$NUMMER'”) print $2 }’ $CACHE`
    if [ -z “$NAME” ]; then
    wget -O $TMPFILE “http://allversal.ch/telsearch.php?tel=$NUMMER”
    NAME=`cat $TMPFILE`
    if [ ! -z “$NAME” ]; then
    printf “$NUMMER—$NAME\n” >> $CACHE
    fi
    fi
    if [ -z “$NAME” ]; then
    NAME=”$NUMMER”
    fi
    fi
    ###
    ### Here you can add “additional alert code”
    ###
    # directly source an external scriptlet, for better separation with this publicly updated script
    #. /usr/local/asterisk/reverse.agi_notifier_sh
    printf “`date +%Y-%m-%d\ %H:%M` $NAME\t$NUMMER\n” >>$LOG
    echo ‘SET VARIABLE LONGNAME ‘”\”$NAME\”” >/tmp/stdout
    read in
    exit 0

    mein extensions.conf schaut so aus :

    exten =>15,1,Set(CHANNEL(language)=de)
    exten =>15,n,AGI(lookup.agi, ${CALLERID(num)})
    exten =>15,n,Set(CALLERID(name)=${LONGNAME})
    exten =>15,n,Dial(SIP/15,10,r)
    exten =>15,n,Answer
    exten =>15,n,Playback(vm-nobodyavail)
    exten =>15,n,Voicemail(15)
    exten =>15,n,Hangup

    nach einem Anruf erhalte ich im /tmp 5 files

    1.) file anrufliste_log

    2011-11-17 03:56 Grgic Nedeljko 0796003185

    2.) file invsuche_cache

    0796003185—Grgic Nedeljko

    3.) file reverse.tmp with

    0796003185–

    4.) file stdout

    SET VARIABLE LONGNAME “Grgic Nedeljko”

    5.) file tmpsuche.html

    wo habe ich was falsch gemacht ?

    Beste Grüsse

    Grgic Nedeljko

  2. Sorry für die späte Reaktion, irgendwie funktioniert die Mailbenachrichtigung des Blogs nicht mehr. Dann funktionierts nun bei dir?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.