Quantcast
Channel: The Michigan Telephone blog » Programming
Viewing all articles
Browse latest Browse all 4

BETA Perl script for Caller ID popups when using Obihai devices

$
0
0

Quite some time ago, I published a BETA Perl script for Caller ID popups when using Linksys/Sipura devices. Time marches on, and now the new Obihai devices (the OBi100 and OBi110) are on the market, and my earlier script won’t work with the Obihai devices. I had kind of thrown “everything but the kitchen sink” into that original script but in the end all I really wanted was the notification popups, so when I set out to write a similar script for use with the Obihai devices, I decided to keep it simple.

So, if you have an Obihai adapter on your local subnet or home network and receive calls over it, and you would like to see popups on your computer when a call comes in, showing the caller’s name and number, along with an identification of the line/extension/device that the call came in on and the time and date the call arrived (in case you are out when the call comes in), AND you have previously run Perl scripts on your computer, OR are reasonably good at following instructions and problem-solving, AND you are willing to run a script that comes with NO WARRANTY whatsoever (if it breaks, you can keep all the pieces), then here is a script for you. Note that this has already been published (by me) in the OBiTALK forum, so if you have already installed this script using the instructions there, then there is probably nothing new for you here (unless I add something after original publication, as I have sometimes been know to do). Also note that this is still very much experimental code, and may be buggy.

One new feature this script offers is that if there is an Asterisk box on your local network that is running the CallerID Superfecta module, you can use that to possibly get a name for your incoming Google Voice calls.  If you don’t have CallerID Superfecta available, the script will still work just fine, you simply won’t get the name lookups. One caveat about this script is that in order for it to work, you must set your Obihai device to use a fixed IP address on your local network.  In my previous script I wasted a lot of code trying to find the device on the local network, but the Obihai devices are a different type of device and in the end it just makes things a lot easier if you set the Obihai device to use a fixed IP address.

You will need to make some changes to the script.  At the top of the script, change these values:

  • Change 2345 to some designation that describes the receiving phone.  It could be an extension number, the full Google Voice number, the name of the device… it’s just to identify which device the call is coming in on in case you have more than one
  • Change 192.168.0.123 to the fixed IP of your OBi device
  • Change password to the admin password of your OBi device
  • Change admin@OBi110 to admin@OBi100 if  (and only if) you have an OBi100 (this is important!)
  • Uncomment the “my $superfecta” line and change the ip address from 192.168.0.234 to the address of the Asterisk/FreePBX server running the CallerID Superfecta module, if you have one available. Don’t uncomment the line if you don’t have access to such a server.

During initial testing the script will run for 20 seconds and quit — this allows you to run the script from a terminal window and see whether Perl complains about a missing module, etc. (it will complain if you don’t have the LWP::UserAgent module installed, so install that module if you don’t already have it). After you are finished testing change while ($count < 20) { to while (1) { to eliminate the 20 second timeout.

And, there is one more change you must make, near the end of the script.  Change the line print”$name\n$num calling $ext at $fulldate\n” to a system call to the notification system on your machine.  For example, these two lines should work on a Mac or Windows machine, provided you have Growl (or Growl for Windows) and growlnotify installed (tested on a Mac, but not on Windows — let me know if you had to change anything to make it work):

(Note that in all of the following code examples, some lines may overflow the column width of this blog, so you’ll want to use cut-and-paste to move these into an editor so that you can view the full, untruncated lines:)

eval { system("growlnotify -s -p 1 -a Telephone -m \"$num calling $ext at $fulldate\" \"$name\"") };
warn() if $@;

Leave out the “-a Telephone” if you’re running Windows or don’t have the Telephone app installed, I’m just using its icon in the notifications.  Or on a Ubuntu system (untested, but it should work if notify-send is installed):

eval { system("notify-send –urgency=\"critical\" –icon=\"phone\" \"$name\" \"$num calling $ext at $fulldate\"") };
warn() if $@;

Here’s the Perl script:

#!/usr/bin/perl
use LWP::UserAgent;
my $ext='2345';
my $ip='192.168.0.123';
my $pw='password';
my $obi='admin@OBi110';
# my $superfecta="http://192.168.0.234/admin/modules/superfecta/bin/callerid.php?thenumber=";

my $url = "http://$ip//PI_FXS_1_Stats.xml";
my $regex1 = qr/current="(.*?)" >/;
my $regex2 = qr/type="input" default=" " current="'(.*?)>/;
my $regex3 = qr/^(.*?)'/;
my $regex4 = qr/' (.*?)"/;

my @months = (
	"January", "February", "March", "April", "May", "June",
	"July", "August", "September", "October", "November", "December"
);
my @weekdays = (
	"Sunday", "Monday", "Tuesday", "Wednesday",
	"Thursday", "Friday", "Saturday"
);

my $ua = new LWP::UserAgent;
print "starting\n";
$ua->timeout(10);
$ua->credentials("$ip:80", $obi, 'admin', $pw);

while ($count < 20) {
	$count++;
	sleep 1;
	my $request = HTTP::Request->new('GET');
	$request->url($url);
	$request = $ua->request($request)->content;
	my @status = $request =~ /$regex1/g;
	if ( $status[0] eq "Ringing" ) {
		if ( $flag== 0 ) {
			$flag = 1;
			my @cid  = $request =~ /$regex2/g;
			my @cidname = $cid[0]  =~ /$regex3/g;
			my $name = $cidname[0];
			my @cidnum = $cid[0]  =~ /$regex4/g;
			my $num = $cidnum[0];
			if  (defined $superfecta) {
				if ( $name =~ /^\+1(\d{10})$/ ) {
					$name = HTTP::Request->new('GET');
					$name->url("$superfecta$num");
					$name = $ua->request($name)->content;
				}
			}
			$num =~ s/^([2-9])(\d{2})([2-9])(\d{2})(\d{4})$/$1$2-$3$4-$5/;
			$num =~ s/^(1)([2-9])(\d{2})([2-9])(\d{2})(\d{4})$/$1-$2$3-$4$5-$6/;
			$num =~ s/^([2-9])(\d{2})(\d{3})(\d{3})$/$1$2 $3 $4/;
			my (
				$sec,  $min,  $hour, $mday, $mon,
				$year, $wday, $yday, $isdst
			) = localtime(time);
			my $ampm = "AM";
			if ( $hour > 12 ) {
				$ampm = "PM";
				$hour = ( $hour - 12 );
			}
			elsif ( $hour eq 12 ) { $ampm = "PM"; }
			elsif ( $hour eq 0 )  { $hour = "12"; }
			if ( $min < 10 ) { $min = "0" . $min; }
			$year += 1900;
			my $fulldate =
			"$hour:$min $ampm on $weekdays[$wday], $months[$mon] $mday, $year";
			print"$name\n$num calling $ext at $fulldate\n";
		}
	}elsif ( $status[0] ne ""){
	            if ( $flag == 1 ) {
			$flag = 0;
		}
	}
}

One note:  As the script is currently written, the CallerID Superfecta module is only called if the Caller ID name matches the pattern +1 followed by ten digits, which is what you get on incoming Google Voice calls on the Obihai devices.  And no, I would have no clue whatsoever as to how to make the CallerID Superfecta module run independently of Asterisk and FreePBX — it’s written in PHP and I don’t know PHP (other than occasionally I can hack someone else’s PHP program to make a minor change, but that’s about it.  This would not be a minor change!).  It appears as if someone is trying to make a standalone version of CallerID Superfecta, however, I know nothing about this projects beyond what’s posted in this message: CallerID web service

Once this is running the way you like, with no errors, you can set it up to run as a startup task so you don’t need to keep a terminal window open.  And, since it’s a Perl script, it lends itself to custom modifications.  For example, see the original article for a suggestion on how to send notifications to a home theater PC running XBMC or Boxee.  If you make any significant changes to the script (or if you add any of the extended functionality back from the original script), please feel free to post your modifications in a comment.

EDIT: If you are running Windows, you might prefer to use GrowlerID.



Viewing all articles
Browse latest Browse all 4

Trending Articles