Reverse Hold (Callback Requests) with Asterisk and PHP

Ever wonder how the “Request Callback” buttons work when they call you as requested after filling in form? Or need to write one?

Simply, I created a script that creates a .call file in the correct format in a location that Asterisk monitors for outbound calls.

Here is how I did it with PHP and Asterisk.

Pre-requisites

  1. Your Asterisk server has to accept SSH logins with a username that is part of the asterisk group.
  2. Your Asterisk server needs to have autoload=yes in modules.conf
  3. You must be able to write to /var/spool/asterisk/outgoing/ on Asterisk
  4. Obviously your web server needs communication to Asterisk.
  5. Ability to create a cron on the Asterisk server.

Part 1- Create your form to handle the user request.

I actually used the cForms plugin for WordPress to create my form. I used a nice button to open up a modal window containing the form.

 

** Your form processor would ideally validate the data. I relied on the cForms data to validate the information.

Part 2 – Process the data and send to Asterisk

I had a lot of problems using the SSH PECL Extension, (so did this guy), so I ended up using phpseclib.
My script requires phpseclib.

Once your data is validated, you need to call a script like this.

  1. I am calling the script via GET with a query string variable of Phone
  2. My ‘teeny bit of validation’ does a few things;
    1.  Checks the Phone number exists (again).
    2. Checks the phone number length is 11 characters (UK only).
    3. Checks the referrer is the web server we expect.
  3. Connects to the Asterisk server via SSH
  4. Writes the file with a random name and sets the permission.
The file we write is initialised as $callData, you need to change;
  1. <<SIP TRUNK>>> should match the trunk you would like to use
  2. CallerID works two fold, firstly the number is the outgoing Caller ID, also the name and number show up on the incoming phone(s) on your Asterisk.
  3. <<EXT OR RING GROUP>> should match the extension or ring group you would like to ring.
&lt;?php
include('phpseclib/Net/SFTP.php');
// teeny bit of validation
if (isset($_GET['Phone']) AND preg_match('/^[0-9]{11}+$/', $_GET['Phone'])   AND preg_match('/&lt;&lt;WEB SERVER NAME&gt;&gt;/',$_SERVER['HTTP_REFERER']) == '') :
	// init the ssh object	 
	$sftp = new Net_SFTP('&lt;&lt;YOUR ASTERISK SERVER&gt;&gt;');
	if (!$sftp-&gt;login('&lt;&lt;ASTERISK SSH LOGIN&gt;&gt;', '&lt;&lt;ASTERISK SSH PASSWORD&gt;&gt;')) {
	    exit('Login Failed');
	}
	// make a random file
	$file =  substr(str_shuffle(MD5(microtime())), 0, 10);
	/ asterisk call file data
	$callData = '
Channel:&lt;&lt;SIP TRUNK&gt;&gt;'.$_GET['Phone'].'
CallerID: "Call Back Request" &lt;08000 1234 5678&gt;
MaxRetries: 2
RetryTime: 60
WaitTime: 30
Extension: &lt;&lt;EXT OR RING GROUP&gt;&gt;
Priority: 2
	';
 
	$sftp-&gt;put('/var/spool/asterisk/outgoing/'.$file.'.call', $callData);
	$sftp-&gt;chmod(0777, '/var/spool/asterisk/outgoing/'.$file.'.call');
 
endif;
?&gt;

Part 3 – House Keeping on Asterisk

This isn’t strictly required, but sometimes Asterisk may not delete the .call files you create or get confused, so I created a cron that ran every minute to make sure the call files are cleaned up.

[codesyntax lang=”bash”]
* * * * * /bin/rm -rf /var/spool/asterisk/outgoing/* && logger Asterisk deleted outgoing queue files

19 Oct – Update

Someone has accused me of “stealing” their code. This isn’t true. There is only one way of making Asterisk ring out automatically, so the functionality will be similar  I wrote this code from scratch using the following references;

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *