Integrating asterisk with the rest of the house; part 1. (OS X Address Book + Asterisk)

Last weekend I decided to try my own hand at using asterisk to create a click to call integration with the os x address book, and I am going to tell you how I got everything working. These instructions assume that you already have both an asterisk, and apache install up and running (on the same machine), and that you are using the asterisk install to power the phone system at your location.

1) I wrote a small program that takes 3 parameters; from extension, to extension, and callerid, and uses them to write an x.call file that asterisk uses to generate a call. I put this in my apache installs cgi-bin directory and set it to run as root.

#!/usr/bin/perl
$ENV{‘PATH’} = ‘/bin:/usr/bin’;

use CGI;
use Data::Uniqid qw (uniqid);

$query = new CGI;

$full_url = $query->url();
$uid = uniqid;
$fe = $query->url_param(‘fe’);
$te = $query->url_param(‘te’);
$cid = $query->url_param(‘cid’);

open $uid, “> /tmp/$uid” or die “Cannot create tempfile : $!”;
print $uid
“Channel: SIP/$fe
Set: ctdnum=$te
WaitTime: 30
Extension: $te
CallerID: $cid < $te>
Priority: 1
Context: c2c”;

close $uid;

system “mv /tmp/$uid /var/spool/asterisk/outgoing/$uid.call”;

This basically just takes the parameters you feed it in the url, writes a file, and moves it to the appropriate place so that asterisk will act on it.

2) I then had to make sure I created that “c2c” context in my extensions.conf and ran asterisk -rx ‘extensions reload’. Mine looks like this where “ST1” is my termination providers peer name.

[c2c]
exten => _1XX,1,Dial(${EXTEN},120,t)
exten => _NXXNXXXXXX,1,Dial(SIP/1${EXTEN}@${ST1},120,Tt)
exten => _1NXXNXXXXXX,1,Dial(SIP/${EXTEN}@${ST1},120,t)

3) I took an existing click2call script made for vonage and modified it to point to my server, and use the right parameters in the url.

(*
Put this script into your “Address Book Plugins” folder in your ~/Library folder.
Only works in the US with this version
*)

property fe : “101”

— The lines below are correct for the U.S.
property myCountryCode : “1”
property myLongDistanceCode : “1”
property myIntlAccessCode : “011”


using terms from application “Address Book”
on action property
return “phone”
end action property
on action title for pers with fone
return “Dial”
end action title

on should enable action for pers with fone
if label of fone contains “fax” then return false
return true
end should enable action

on perform action for pers with fone
set theNumber to (value of fone) as string
set cid to “somename”
— Add the long distance or international access code if it’s not already there.
set numToDial to InsertLDCodes(theNumber)


–Uncomment the following two lines to confirm the number
–display dialog “So you want me to call?” default answer numToDial buttons {“OK”}
–set numToDial to text returned of the result

–Erase everything that’s not a digit from the phone number
set cleanedNumber to CleanTheNumber(numToDial)
set theURL to “http://yourserver.com/cgi-bin/click2call.pl?”
set theURL to theURL & “&fe=” & fe
set theURL to theURL & “&te=” & cleanedNumber
set theURL to theURL & “&cid=” & cid

— Use curl to hit the URL and dial the number
set errorCode to do shell script “curl “” & theURL & “””

–If there was an error, return a message.
if (characters 1 thru 3 of errorCode) as string is not equal to “000” then
display dialog “Error: ” & errorCode buttons {“OK”}
end if

end perform action

end using terms from


on InsertLDCodes(theNumber)

if (characters 1 thru 2 of theNumber) as string = “+” & myCountryCode then
— The number was formatted correctly.
return theNumber
end if

if character 1 of theNumber = myLongDistanceCode then
— Domestic long distance with LD access code
return theNumber
end if

if character 1 of theNumber = “+” then
— international number, add prefix
return myIntlAccessCode & ” ” & theNumber
end if

— local number, must add the LD code for Vonage
return myLongDistanceCode & ” ” & theNumber

end InsertLDCodes

on CleanTheNumber(numToDial) — remove punctuation from a string, leaving just the number
set theDigits to {“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”}
set cleanedNumber to “”
repeat with i from 1 to length of numToDial
set j to (character i of numToDial)
if j is in theDigits then set cleanedNumber to cleanedNumber & j
end repeat
return cleanedNumber
end CleanTheNumber

This basically formats it correctly, creates the url based on the passed parameters, and visits that url using curl.

Now when you click on any number in your address book that is not a fax, you will see an option to dial. If you click that it visits the url you have specified with all the right options, which forces that perl code to run, writing the .call file, which makes asterisk generate the call. Simple eh? Holler @ me.
Next time I will explain how I got asterisk to notify me both in OS X, and on the mythtv box when an incoming call comes in.

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.