Skip to content

Instantly share code, notes, and snippets.

@agallou

agallou/twilio_callback.php Secret

Created Apr 16, 2020
Embed
What would you like to do?
<?php
// Configuration start
$gdocUrl = 'https://docs.google.com/spreadsheets/d/IDDOCUMENT/export?format=csv';
$numbers = [
'Adrien' => '+33000000000',
'SebC' => '+33000000000',
];
$onDutyFallback = 'SebC';
// 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');
?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<?php if ($mode === 'sms'): ?>
<Response>
<Message to="<?php echo $number ?>"><?php echo htmlentities($_REQUEST['From'] . " : " . $_REQUEST['Body'], ENT_XML1) ?></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