Skip to content

Instantly share code, notes, and snippets.

@BoldBigflank
Created March 23, 2012 21:11
Show Gist options
  • Save BoldBigflank/2175093 to your computer and use it in GitHub Desktop.
Save BoldBigflank/2175093 to your computer and use it in GitHub Desktop.
Cat Fact SMS
<?php
// Get a greeting based on the body
$Body = $_GET['Body'];
if(stripos($Body, "e4n3m23pd90vc4:9nfaDFADFD42baconP0o") !== FALSE){
$greeting = "&lt;Command not recognized&gt; ";
}
else if(stripos($Body, "stop") !== FALSE || strlen($Body)>30){
$greeting = "Hammer time. ";
}
else if(stripos($Body, "fuck") !== FALSE || stripos($Body, "shit") !== FALSE || stripos($Body, "asshole") !== FALSE){
$greeting = "Watch your language meow! ";
}
else{
$greeting = "Thank you for using Fun Feline Facts! ";
}
// Load the facts from the file
// https://gist.github.com/2175078
$handle = fopen("facts.txt", 'r');
$facts = array();
while($fact = fgets($handle))
array_push($facts, $fact);
$message = "";
// Prepare a cat fact message
$random = rand(0, count($facts));
$randomFact = trim($facts[$random]);
$message = htmlspecialchars("$greeting$randomFact Me-wow!");
// If it's a phone number, send the message to that number
// Otherwise, respond to the original sender
$toAttribute = (phoneUs($Body)!=0) ? "to=\"" . phoneUs($Body) . "\"" : "";
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
echo "<Response>";
if($count<8){
// Send the cat fact
while(strlen($message)>0){
echo "<Sms $toAttribute>" . substr($message, 0, 160) . "</Sms>";
$message = substr($message, 160);
}
// Give a response to the original sender
if($toAttribute != "" && phoneUS($Body) != phoneUs($_GET['From']))
echo "<Sms>(" . ++$count . " of 8 per day)A cat fact has been sent to your friend. Purr-fect! felinefacts@bold-it.com</Sms>";
}
echo "</Response>";
function phoneUS($number){
// Format the phone number to something Twilio friendly
$pattern = "/^\D*[1]?\D*(\d{3})\D*(\d{3})\D*(\d{4})(?:\D*[xX]\D*(\d*))?$/";
preg_match($pattern, $number, $numberArray);
if(!$numberArray){
return 0;
}
$extension = isset($numberArray[4]) ? $numberArray[4] : "";
return "+1".$numberArray[1].$numberArray[2].$numberArray[3];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment