Created
September 27, 2018 17:04
-
-
Save abiusx/dba9977a6ab4ce0c041bc72f4655e3ac to your computer and use it in GitHub Desktop.
Phone voice call captcha powered by Twilio and TwiML (also forwards SMS)
This file contains hidden or 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 | |
/** | |
* Call captcha to prevent spam calls | |
* Uses Twilio TwiML syntax | |
* @author abiusx | |
* @version 1.1 | |
*/ | |
$name="John Doe"; // Put your name here | |
$number="+14342904141"; // Put your real phone number here | |
session_start(); // Store the challenge in session | |
header("Content-type: text/xml"); | |
function logtext($text) | |
{ | |
@file_put_contents("call_log.txt",$text.PHP_EOL,FILE_APPEND); | |
} | |
$from=@$_POST['From']; // Caller number, e.g. +16175551212 | |
$to=@$_POST['To']; // Our bot number, useful for handling multiple numbers with this code | |
if (@$_POST['Direction']!="inbound" and !isset($_POST['SmsSid'])) | |
return false; // Only process inbound calls | |
/// Available: $_POST['FromCity'],$_POST['FromState'],$_POST['FromZip'],$_POST['FromCountry'] | |
$challenge_p1=rand(0,10); | |
$challenge_p2=rand(0,10); | |
$challenge_result=$challenge_p1+$challenge_p2; | |
$hints=["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", | |
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", | |
"seventeen", "eighteen", "nineteen", "tewnty"]; | |
$hint=$hints[$challenge_result]; | |
// Response processing: | |
$response=null; | |
if (isset($_POST['Digits'])) | |
{ | |
$response=$_POST['Digits']; | |
} | |
elseif (isset($_POST['SpeechResult'])) | |
{ | |
$response=$_POST['SpeechResult']; | |
// $_POST['Confidence'] is between 0.0 and 1.0 | |
if (!is_numeric($response)) | |
{ | |
if ( ($r=array_search($response, $hints)) !== false) | |
$response=$r; | |
} | |
} | |
echo '<?xml version="1.0" encoding="UTF-8"?>',PHP_EOL; | |
echo "<Response>",PHP_EOL; | |
if (isset($_POST['SmsSid']) or isset($_POST['Body'])) // SMS | |
{ | |
$body=$_POST['Body']; | |
logtext("SMS Forwading from '{$from}' to '{$number}' saying '{$body}'"); | |
echo "<Message to='{$number}'>{$from}: {$body}</Message>\n"; | |
} | |
else // Call | |
{ | |
if ($response!==null) // Response from caller | |
{ | |
logtext("From: {$from}, To: {$to}, Response: {$response}"); | |
if (isset($_SESSION['result']) and $response==$_SESSION['result']) | |
{ | |
echo "<Say voice='alice'>Thank you. Please wait while your call is connected.</Say>\n | |
<Dial>{$number}</Dial>\n"; | |
} | |
else | |
echo "<Say voice='alice'>Invalid response. Good bye.</Say>\n"; | |
} | |
else // Initial call | |
{ | |
$_SESSION['result']=$challenge_result; | |
?> | |
<Gather input='dtmf speech' hints='<?=$hint;?>' numDigits='2' timeout='5'> | |
<Say voice='alice'>You have called <?=$name;?>. | |
If this is an unsolicited call, please hang up now. | |
Otherwise, please respond to the following question for your call to be connected. | |
</Say> | |
<Pause /> | |
<Say voice='alice'> | |
What is <?=$challenge_p1;?>+<?=$challenge_p2;?>? | |
</Say> | |
<Pause length='5' /> | |
<Say voice='alice'> | |
I repeat, what is <?=$challenge_p1;?>+<?=$challenge_p2;?>? | |
</Say> | |
</Gather> | |
<Say voice='alice'>You did not respond to the challenge. Good bye.</Say> | |
<?php | |
} | |
} | |
echo "</Response>",PHP_EOL; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment