Asterisk Speed Dial

Wouldn’t it be great if all your contacts were readily available in a simple web page? And wouldn’t it be even better if you could simply click on their phone number, and let your phone server call your extension, then dial the contact automatically?

At our office, we have a page written in PHP that displays a list of names/ phone numbers matching whatever search string the user enters.  The phone numbers are displayed as a list of hyperlinks.  Those hyperlinks call a PHP page I’ll reference as dial4me.php (names have been changed to protect the innocent), passing the desired phone number in the query string.

Inside dial4me.php, the desired outbound phone number gets massaged (making sure the correct country code and area code are included), populating a variable called $num2dial.

The $sip_account variable is used to establish connectivity with the local extension somewhere inside our building.  It gets populated by looking up the appropriate value in a mysql table.  For example …

I also set up a special context inside extensions.conf to handle these types of calls.  There’s nothing magical about the 72x12231Nk32 extension—it’s just a random string of characters and digits that wouldn’t likely appear naturally.  (Also makes it easier to search for the relevant section in my excessively lengthy extensions.conf)

With that for an introduction, here’s the relevant snippet of code from dial4me.php:

// Asterisk eagerly searches for .call files.  We need to avoid file locking contention issues.
// So, fully populate the entire contents of the outbound .call file to a string before
// writing everything to the outbound .call file in one fell swoop.
// ——————————————————————————————–

define(‘CALL_FILE_TARGET_FOLDER’,’/var/spool/asterisk/outgoing/’);  // folder where call files
// should get dumped.  Asterisk will delete each file after processing.

define(‘CALL_FILE_TEMP_FOLDER’,’/var/www/html/tmp/’) ; // where to store call files temporarily,
// to avoid complaints from asterisk about inability to set utime or some such nonsense

        $lcOut = ‘Channel: ‘ . $sip_account . “\n”;
        $lcOut .= ‘WaitTime: 30’ . “\n”;
        $lcOut .= ‘Setvar: PHONENUMBER=’ . $num2dial . “\n”;
        $lcOut .= ‘Context: patch_me_thru’ . “\n”;
        $lcOut .= ‘Extension: 82x231Nk32 ‘ . “\n”;

       $cOutFile = ‘callnow_’ . time() . ‘.call’;
$cTmpOut = CALL_FILE_TEMP_FOLDER . $cOutFile ;
        $cFinal = CALL_FILE_TARGET_FOLDER . $cOutFile ;
        $nHand = fopen($cTmpOut,”w”) ;  // write output to tmp folder hanging off www/html

        fclose($nHand); // write the file out to the calls folder
        chmod($cTmpOut, 0777); // change from 644 to 0777 so that asterisk doesn’t freak
// out about not being able to update the file timestamp

       chown($cTmpOut,’asterisk’); // change file owner to asterisk so we don’t see
// a meaningless message about utime
        rename($cTmpOut,$cFinal) ;
        chmod($cFinal, 0777); // change from 644 to 0777 so that asterisk doesn’t freak
// out about not being able to update the file timestamp

And here’s the relevant context from Extensions.conf:

; This context uses .call files generated by  See dial4me.php
exten => 72x12231Nk32 ,1,Set(CDR(userfield)=${PHONENUMBER})
same => n,Set(CALLERID(num)=12624373239)
same => n,Dial(SIP/${PHONENUMBER}@trunk-VIout,120,mxX)
same => n,Hangup(); Clean up the local channel when we’re done with it.