who's calling?

May 24th, 2008 by rickbradley 0 comments »

This is an almost silly/trivial enough thing to not warrant a post, but since I didn’t see a trivial way to do this publicized on the internets, I should probably write it up.

So I have an asterisk box set up on my home phone line, primarily to hassle law-breaking telemarketers and debt collectors (I don’t have any debts that need collecting, but that doesn’t mean I don’t get the occasional debt collector calling the wrong person and refusing to accept the truth).

With the arrival of a sister-in-law and another house guest, the number of inbound phone calls has jumped noticeably. The unifying trait of these calls: in nearly all cases, they’re not for me. That doesn’t mean I don’t often find myself getting up to look at the caller ID, i.e., distracting myself from whatever it was that was doing when the phone rang.

So, I tried setting up app_notify which promised to be the simplest path to asterisk -> growl caller ID notification to my Mac laptop. 30 minutes, a few install errors, some hand attempts with getting the C happy on OS X, etc., I said “screw that.” I’ve written large-scale AGI apps (some have have been running continuously for upwards of 3 years now under heavy call load). Hopefully I can write an AGI that pings my laptop with a growl message when the phone rings.

So, I installed one of the various Ruby AGI libraries (batphone), installed ruby-growl, hacked around a bit and came up with this tiny script:

#!/usr/bin/env ruby

require 'rubygems'
require 'agi'
require 'ruby-growl'

thing = ''
  agi = AGI.new
  thing << [ agi.env['callerid'], agi.env['calleridname']].join("\n")
rescue Exception => e
  thing << "Error: #{e.inspect}" 

['',''].each do |host|
  g = Growl.new host, 'incoming phone call', ["incoming phone call"]
  g.notify "incoming phone call", "incoming phone call", thing


Granted, the list of IP addresses is hardwired, so it’s highly inelegant. This is also the first script I’ve written in a long while that wasn’t done test-first. My rule (excuse) for not writing tests first in an instance like this is, if it’s completely an exploratory spike I will sometimes not bother. If I want to package it, redistribute it, reuse it, put it into production, etc., then I rewrite it after the spike test-first from scratch. Publishing this script is serving as an example for anyone who wants to know how to do this, but recommending that if it’s useful you or I rewrite something better test-first. I also recommend looking into Adhearsion if you want to do more sophisticated Ruby + Asterisk work.

Anyway, to turn this on, save it in /var/lib/asterisk/agi-bin/announce_callerid.agi, make sure to make it executable (chmod +x), then put the following in your /etc/asterisk/extensions.conf:

exten => s,1,AGI(announce_callerid.agi)

Finally, “asterisk -r” and type “restart when convenient” to update everything. I had to update growl versions on one of the laptops and make sure that network announcements are enabled. I skyped our home phone and bing there’s the callerid notice on both laptops.

Leave a Reply