First up, if you're using a softphone and want to dial using that application I suggest downloading Telephone which comes with an Address Book plugin:
http://code.google.com/p/telephone/
I prefer to use a real phone when talking at home, and have a regular phone hooked up to the LinkSys SPA2102 ATA. To dial from Address Book with a real phone, Address Book needs to send a command to a php page on your PBX's webserver, which then dials the number passed from Address Book, and then dials your home extension.
I grabbed a few bits of scripts from the net; some of this code seems to be based on a few things Ward published a while back here
http://nerdvittles.com/index.php?p=168. I also found some AppleScript here
http://www.jaredwatkins.com.
With these as a base I ended up with two files that you'll need to put in two locations. Before telling you where to copy them, here is the code for both--You'll probably need to make a change here or there.
AppleScript scpt file
Be sure to enter a password for the line "set passwd to..."; it can be anything you want;
Be sure to enter the IP address of your PBX for the line "set command to..."; this assumes your PBX is on your local network but may work remotely as well;
Code:
using terms from application "Address Book"
on action property
return "phone"
end action property
on action title for p with e
return "Dial with Asterisk"
end action title
on should enable action for p with e
if value of e is missing value then
return false
else
return true
end if
end should enable action
on perform action for p with e
set telephone to value of e
-- The insecure option is for using self signed ssl cert
-- You should probably change the password =]
set passwd to "[your password here]"
set urlencoded to urlencode(telephone) of me
set command to "curl \"http://ip.address.of.your.pbx/asterisk-dial.php?key=" & passwd & "&telephone=" & urlencoded & "\""
do shell script command
return true
end perform action
end using terms from
on urlencode(theText)
set theTextEnc to ""
repeat with eachChar in characters of theText
set useChar to eachChar
set eachCharNum to ASCII number of eachChar
if eachCharNum = 32 then
set useChar to "+"
else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then
set firstDig to round (eachCharNum / 16) rounding down
set secondDig to eachCharNum mod 16
if firstDig > 9 then
set aNum to firstDig + 55
set firstDig to ASCII character aNum
end if
if secondDig > 9 then
set aNum to secondDig + 55
set secondDig to ASCII character aNum
end if
set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
set useChar to numHex
end if
set theTextEnc to theTextEnc & useChar as string
end repeat
return theTextEnc
end urlencode
Here's the code from the asterisk-dial.php file;
You'll need to enter the password you entered in the applescript above; see the line "$GETREQUEST_KEY...";
Also be sure to set the extension number you want to ring at the line "ASTERISK_EXTEN..."; I set this to the extension of my main "normal" phone connected via the SPA2102;
Code:
<?php
error_reporting(E_ERROR);
$USERNAME = "admin"; // Value from /etc/asterisk/manager.conf
$PASSWORD = "amp111"; // Value from /etc/asterisk/manager.conf
$ASTERISK_HOST = "127.0.0.1"; // The IP address of your local Asterisk server
$ASTERISK_PORT = 5038; // Value from /etc/asterisk/manager.conf
$ASTERISK_CONTEXT = "from-internal"; // The context, from /etc/asterisk/extensions.conf, to dial out.
$ASTERISK_EXTEN = "SIP/100"; // The local extension you want to ring before dialing out
$GETREQUEST_KEY = "[password from scpt file]"; // Set this to match the password in the applescript
// Bigtime security check
if ($_REQUEST["key"] != $GETREQUEST_KEY)
{
$fp = fopen("/tmp/dialedcalls.log","a");
fwrite($fp,strftime("%Y-%m-%d %H:%M:%S") . " $_SERVER[REMOTE_ADDR] $_REQUEST[telephone] KEY_CHECK_FAILED\n");
fclose($fp);
die;
}
Dial();
function Dial() {
global $USERNAME,$PASSWORD;
global $ASTERISK_HOST,$ASTERISK_PORT;
global $ASTERISK_CONTEXT,$ASTERISK_EXTEN;
$telephone = $_REQUEST["telephone"];
$telephone = str_replace("-","",$telephone);
$telephone = str_replace(" ","",$telephone);
$telephone = str_replace(")","",$telephone);
$telephone = str_replace("(","",$telephone);
$socket = fsockopen($ASTERISK_HOST,$ASTERISK_PORT, $errno, $errstr, $timeout);
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: $USERNAME\r\n");
fputs($socket, "Secret: $PASSWORD\r\n\r\n");
// Then we originate a call. The effect of this (at least in my world, is to
// ring my phone(s) ($ASTERISK_EXTEN) and then once I pick up, to
// dial the number I passed ($telephone).
fputs($socket, "Action: Originate\r\n");
fputs($socket, "Channel: $ASTERISK_EXTEN\r\n");
fputs($socket, "Context: $ASTERISK_CONTEXT\r\n");
fputs($socket, "Exten: $telephone\r\n");
fputs($socket, "Priority: 1\r\n");
fputs($socket, "Callerid: Address Book\r\n");
fputs($socket, "Action: Logoff\r\n\r\n");
// Get the first line returned by the Asterisk Manager
$wrets=fgets($socket,128);
// Asterisk Manager will return 'Asterisk Call Manager/1.0' when we
// connect to it. This could be a smarter routine here -- we could
// see if we actually authenticated and placed the call, etc. For
// now, we simply return an error if we didn't manage to connect
// to the Manager at all.
if ($wrets <> "Asterisk Call Manager/1.0")
{
echo "OK\n";
}
else
{
echo "Did not work!\n";
}
// I like to keep a log of the calls I've made this way.
$fp = fopen("/tmp/dialedcalls.log","a");
fwrite($fp,strftime("%Y-%m-%d %H:%M:%S") . " $_SERVER[REMOTE_ADDR] $telephone\n");
fclose($fp);
}
?>
Once you're done editing the files, save them locally and then copy them to the locations listed below.
* On OS X put the applescript file here:
~/Library/Address Book Plug-Ins/Asterisk Dial.scpt
FYI: The ~ tilde means your home/user folder; the absolute path would be:
/Users/[your username]/Library/Address Book Plug-Ins/Asterisk Dial.scpt
* On your PBX put the php file here:
/var/www/html/asterisk-dial.php
If you need to edit this file just do this:
cd /var/www/html/
sudo chmod u+w asterisk-dial.php //if it's not already, make the file writable
sudo nano -w asterisk-dial.php //edit the file with nano
After copying the php file to your PBX, chown the file to asterisk:asterisk and then chmod to 444. Chown will make asterisk own the file; chmod will make the file read only by all users.
The sum total of all this is that you can open address book, click the gray field name of a phone number, then choose "Dial with Asterisk" which will send a http request to the php page on your PBX...which then dials the number you chose...and then calls your home phone when the dialed number is connecting.
This worked so well I set it up on the wife's MacBook
PP