/twilio_callback.php Secret
Created
April 16, 2020 14:57
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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