Tag: asterisk

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