Skip to content

Instantly share code, notes, and snippets.

@ratibus
Created April 28, 2017 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ratibus/b0530cc74a56da09f9887cdcae921efc to your computer and use it in GitHub Desktop.
Save ratibus/b0530cc74a56da09f9887cdcae921efc to your computer and use it in GitHub Desktop.
Twilio callback script
<?php
// Configuration start
$gdocUrl = 'https://docs.google.com/spreadsheets/d/DOCUMENT_ID_HERE/export?format=csv';
$numbers = [
'Name 1' => 'number 1',
'...' => '...',
'Name n' => 'number n',
];
$onDutyFallback = 'Name x';
// Configuration end
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : null;
if (!in_array($mode, ['sms', 'calls'], true)) {
http_response_code(400);
die("Invalid mode parameter");
}
// Load configuration from Google Spreadsheet document
$fp = fopen($gdocUrl, 'rb');
if (!$fp) {
http_response_code(503);
die("Unable to load Google Spreadsheet document");
}
$currentDate = date('Y-m-d');
$onDuty = null;
while (false !== ($row = fgetcsv($fp))) {
if (count($row) < 2 || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $row[0])) {
continue; // invalid line
}
list($date, $onDuty) = $row;
if ($date === $currentDate) {
break;
}
}
if (null === $onDuty || !isset($numbers[$onDuty])) {
$onDuty = $onDutyFallback;
}
$number = $numbers[$onDuty];
header('Content-Type: text/xml');
?>
<?xml version="1.0" encoding="UTF-8"?>
<?php if ($mode === 'sms'): ?>
<Response>
<Message to="<?php echo $number ?>"><?php echo htmlentities($_REQUEST['From'] . " : " . $_REQUEST['Body']) ?></Message>
</Response>
<?php elseif ($mode === 'calls'): ?>
<Response>
<Dial><?php echo $number ?></Dial>
</Response>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment