Skip to content

Instantly share code, notes, and snippets.

@aaronpk
Created October 16, 2010 16:07
Show Gist options
  • Save aaronpk/629965 to your computer and use it in GitHub Desktop.
Save aaronpk/629965 to your computer and use it in GitHub Desktop.
CyborgCamp volunteer SMS notifications
<?php
include('include.php');
session_start();
$voice = 'allison';
$jsonInput = file_get_contents("php://input");
$input = json_decode($jsonInput);
$volunteers = array(
'19995551212' => 'Bob',
'18886662323' => 'Alice'
);
if(is_object($input))
{
// Incoming messages have a "session" parameter from Tropo
if(property_exists($input, 'session') && property_exists($input->session, 'from'))
{
$sessionID = $input->session->id;
$_SESSION['session'] = $input->session;
$_SESSION['callerID'] = $input->session->from->id;
$message = $input->session->initialText;
}
// A session was already started
elseif(array_key_exists('session', $_SESSION))
{
$sessionID = $_SESSION['session']->id;
}
else
$sessionID = FALSE;
}
else
$sessionID = FALSE;
// If one of the volunteers sends a message, broadcast the message to the rest of the volunteers
if(array_key_exists($_SESSION['callerID'], $volunteers))
{
$name = $volunteers[$_SESSION['callerID']];
ircdebug('[cyborgcamp] ' . $name . ': ' . $message);
foreach($volunteers as $k=>$v)
{
if($_SESSION['callerID'] != $k)
{
$tropo[] = array('message' => array(
'say' => array('value' => $name . ': ' . $message),
'to' => $k,
'network' => 'SMS'
));
}
}
}
else
{
// If an unknown user sends an SMS, assume they asked to be let in to the building so reply to them here
$tropo[] = array(
'say' => array(
'value' => "Ok! We'll be right there!"
)
);
// Notify all the volunteers of the user's message
foreach(getForwardingNumbers() as $n)
{
$tropo[] = array(
'message' => array(
'say' => array('value' => $_SESSION['callerID'] . ' says: ' . $message),
'to' => $n,
'network' => 'SMS'
)
);
}
ircdebug('[cyborgcamp] ' . $_SESSION['callerID'] . ' says: ' . $message);
}
echo json_encode(array('tropo'=>$tropo));
function getForwardingNumbers($prefix='')
{
global $volunteers;
$a = array();
foreach($volunteers as $k=>$v)
$a[] = $prefix . $k;
return $a;
}
function getForwardingName($number)
{
global $volunteers;
return array_key_exists($number, $volunteers) ? $volunteers[$number] : $number;
}
// Send a debug message to a mediawiki recentchanges IRC bot
function ircdebug($msg)
{
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($sock, $msg, strlen($msg), 0, 'example.com', 51111);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment