1
0
Fork 0

Add Multicall Support

Implement multicall support by using an sqlite database to store and
retrieve calls.  Two interfaces have been added:

1) add-call.php.  Used to add calls.
example: add-call.php 15555555555 '0 10-16 * * *' 'http://www.domain.com/xml.xml'

2) create-table.php.  Creates the database file

Created calls are retrieved in the caller.php file.  Each call is run
through the scheduler to verify if it is time for it to be made.
This commit is contained in:
Andrew Tomaka 2013-03-04 19:27:42 -05:00
parent aa2bb8624b
commit 4a3749883b
3 changed files with 65 additions and 13 deletions

34
add-call.php Normal file
View file

@ -0,0 +1,34 @@
<?php
if(count($argv) != 4) {
die("Usage: php ./add-call 15555555555 '* * * * *' http://domain.com/file.xml\n");
}
$phone = $argv[1];
$schedule = $argv[2];
$xml = $argv[3];
if(preg_match('/^\d{11}$/', $phone) != 1) {
die("Phone should be all numbers with no symbols. ex: 15555555555\n");
}
if(count(split(' ', $schedule)) != 5) {
die("Schedule requires a properly formed cron schedule.\n");
}
if(!filter_var($xml, FILTER_VALIDATE_URL)) {
die("XML must be a vaild url.\n");
}
try {
$db = new PDO('sqlite:db/songcaller.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("PDOException (connect): " . $e->getMessage() . "\n");
}
try {
$db->exec("INSERT INTO calls (phone, schedule, xml) VALUES($phone, '$schedule', '$xml')");
} catch(PDOException $e) {
die("PDOException (query): " . $e->getMessage() . "\n");
}
echo "Successfully added call.\n";

View file

@ -3,26 +3,34 @@ require 'vendor/autoload.php';
require 'vendor/twilio/sdk/Services/Twilio.php';
use Symfony\Component\Yaml\Yaml;
$crontab = new \HybridLogic\Cron\Crontab;
$settings = Yaml::parse('conf/settings.yaml');
$db = new PDO('sqlite:db/songcaller.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$settings = Yaml::parse('conf/settings.yml');
$client = new Services_Twilio(
$settings['twilio']['sid'],
$settings['twilio']['token']
);
$result = $db->query('SELECT * FROM calls');
foreach($result as $row) {
$crontab->add_job(
\HybridLogic\Cron\Job::factory('test')
->on('0 10-16 * * *')
->trigger(function() use($settings, $client) {
->on($row['schedule'])
->trigger(function() use($settings, $client, $row) {
$call = $client->account->calls->create(
$settings['application']['from'],
$settings['application']['to'],
$settings['application']['xml']
$row['phone'],
$row['xml']
);
})
);
}
$crontab->run();
$db = null;

10
create-table.php Normal file
View file

@ -0,0 +1,10 @@
<?php
$db = new PDO('sqlite:db/songcaller.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('create table calls (
id integer PRIMARY KEY,
phone integer,
schedule varchar(50),
xml varchar(255)
);');