Asterisk PHP/HTML Call File Generator

Asterisk is open source PBX telephony software that runs on many platforms. It can do anything from making automated calls, to acting as an answering machine, to switching an entire call centre.

For the first case (making automated calls), asterisk reads "call files" from a certain directory, parses them, and then sends the appropriate information to the telephony hardware.

Here's what I did to make an Asterisk call file generator web interface. This will create the call file and give it to Asterisk for processing. In my example I am using a Zap (pstn) line to make the outgoing calls. I am running Asterisk on Debian. To follow this tutorial, you need a working Asterisk installation (I'm using Asterisk 1.4) with an FXO card that's connected to your regular phone jack (I use a clone X100P), and a php-enabled web server running on the same machine as Asterisk. This would work just as well on Ubuntu, and probably any other Debian-based Linux. It will not work on Microsoft Windows.

First, create an html file for inputing your parameters for the call file, containing this:

<html>
<head><title></title></head>
<body>
<form action="phone.php" method="POST">
<table>
<tr><td>Phone Number</td><td><input type="text" name="phonenumber"></td></tr>
<tr><td>Max Retries</td><td><input type="text" name="maxretries" value="2"></td></tr>
<tr><td>Retry Time</td><td><input type="text" name="retrytime" value="60"></td></tr>
<tr><td>Wait Time</td><td><input type="text" name="waittime" value="30"></td></tr>
<tr><td>Context</td><td><input type="text" name="context" value="pointless"></td></tr>
<tr><td>Extension</td><td><input type="text" name="extension" value="s"></td></tr>
<tr><td>Priority</td><td><input type="text" name="priority" value="1"></td></tr>
</table>
<table>
<tr><td>Time to Call: 
</td><td>Hour</td><td>Minute</td><td>Second</td></tr>
<tr><td></td><td><input type="text" name="hour"></td>
<td><input type="text" name="minute"></td><td><input type="text" name="second"></td></tr>
<tr><td></td><td>Year</td><td>Month</td><td>Day</td></tr>
<tr><td></td><td><input type="text" name="year"></td>
<td><input type="text" name="month"></td><td><input type="text" name="day"></td></tr>
<tr><td><input type="submit" name"submit" value="Call"></td></tr>
</table>
</form>
</body>
</html>

This file contains default values for some of the fields. It will fill in some of the text inputs so that, if unchanged, your call file will point to context "pointless", extension "s", priority "1". You can change the defaults simply by changing value="" in the input tags.

Next, create a php file that will take your input from the html form and generate the call file. This file assumes that you will be making the outgoing call through Zap channel 1. Save the following code as phone.php in the same directory as your html file:

<?php
$counterfile="counter.txt";
incrementCounter($counterfile);

function incrementCounter($counterfile)
{
        $current=readCounter($counterfile);
        $current++;

        if(!$handle = fopen($counterfile, "w"))
        {
                return false;
        }
        else
        {
                if(fwrite($handle, $current) === FALSE)
                {
                        return false;
                }
        }
        fclose($handle);
        return true;
}

function readCounter($counterfile)
{
        $contents = @file_get_contents($counterfile);
        if(is_numeric((int)$contents))
        {
                return ((int)$contents);
        }
        else
        {
                return 0;
        }
}

$number = @file_get_contents($counterfile);
$callfile = "call$number.call";
$fh = fopen($callfile, 'w') or die("can't open file");
$phonenumber = $_POST['phonenumber'];
$maxretries = $_POST['maxretries'];
$retrytime = $_POST['retrytime'];
$waittime = $_POST['waittime'];
$context = $_POST['context'];
$extension = $_POST['extension'];
$priority = $_POST['priority'];
$hour = $_POST['hour'];
$minute = $_POST['minute'];
$second = $_POST['second'];
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$calltime = @mktime($hour, $minute, $second, $month, $day, $year);
$array = "Channel: Zap/1/$phonenumber\nMaxRetries: 
$maxretries\nRetryTime: $retrytime\nWaitTime: $waittime\nContext: 
$context\nExtension: $extension\nPriority: $priority";
fwrite($fh, $array);
fclose($fh);
touch($callfile, $calltime);
echo "<html><head><title></title></head><body>
Phone number <b>$phonenumber</b> will be called at <b>$hour:$minute:$second</b> on <b>$year/$month/$day</b>.<br>";
echo "The server will redial <b>$maxretries</b> times if the line is busy.<br>";
echo "If the call fails, the server will redial after <b>$retrytime</b> seconds.<br>";
echo "The server will wait <b>$waittime</b> seconds for the call to be answered before it will stop calling.<br>";
echo "The context <b>$context</b> will be used and will start on extension <b>$extension</b> at priority <b>$priority</b>.<br>";
echo "Please wait for the server to process this information and make the call.<br>";
echo "If you would like to cancel this call, click this button immediately:
<form action=\"cancel.php\" method=\"POST\"><input 
type=\"submit\" name=\"cancel\" value=\"Cancel Call\"></form></body></html>";
?>

Then, create a script that will allow you to cancel your call if you're really quick and manage to click the cancel button before cron takes over. Save the following as cancel.php in the same folder:

<?php
$counterfile="counter.txt";
$number=file_get_contents($counterfile);
unlink("call$number.call");
echo "<html><head><title></title></head><body>
If you clicked the button quick enough, your call has now been cancelled. 
If you see a warning above this message, then you weren't quick enough and your call will go through as scheduled.
</body></html>";
?>

Now you need to make your working folder world writable so PHP can create new files in it. Make sure to type in the real path of your folder. In a root terminal type:

chmod 777 /path/to/yourfolder

Next you need to create a bourne shell script that will be run by cron to change the owner of the call file to asterisk, and move it into the /var/spool/asterisk/outgoing folder. As root, create a file named callfiles.sh with the following code, and place it in /usr/bin . Make sure to put in the real path of your folder:

#!/bin/sh
chown asterisk:asterisk /path/to/yourfolder/*.call
mv /path/to/yourfolder/*.call /var/spool/asterisk/outgoing

Now you need to make your bourne shell script executable. In a terminal, as root type:

chmod +x /usr/bin/callfiles.sh

And finally, create the cron job to run callfiles.sh once every minute. As root type the following in a terminal:

crontab -e

This will open your crontab with a text editor. Add the following line to the bottom of the file:

0-59 * * * * /usr/bin/callfiles.sh

Save the file and exit, and it will automatically install the task as a cron job.

Well there you have it. Thats how to make an HTML/PHP call file generator for Asterisk. If you leave the "time to call" field empty, your call will happen within about 1 minute of clicking the call button. If you are scheduling a call in the future, make sure you fill in all the fields, including the date, or it won't work properly. Configuring a dial plan to use is up to you. Please note that I am in no way a professional php programmer so this may not be very secure or efficient code. Also, you should DEFINITELY protect this folder with a password if you are going to put it online. You don't want some random person to stumble upon your page and be able to use your phone line.

Was this tutorial helpful? Toss me a few bucks if you're feeling generous.

Go back to Tutorials


This tutorial is copyright © 2009 Defcronyke Webmaster, The Eternal Void Network. It may only be linked to with permission from the original author. If you provide reference to it anywhere, please give credit to the author.



Number of Unique Hits: 00006767
Privacy Policy