Skip to content

Instantly share code, notes, and snippets.

@rahims
Created November 14, 2010 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahims/676035 to your computer and use it in GitHub Desktop.
Save rahims/676035 to your computer and use it in GitHub Desktop.
Code from Twilio talk given at the L.A. Hacker News Meetup on 11/13/2010
<?php
/*
* Code from Twilio talk given at the L.A. Hacker News Meetup on 11/13/2010
*
* During the talk, everyone was asked to vote (vote.php) whether they were
* awesome or not (text "1" for awesome, anything else for not awesome).
* Afterwards, the first person to vote was called up and told they were
* awesome. In this script, we call up the first person to vote that they're
* awesome and the first person to vote that they're not, in the hopes that
* the awesome person can help the not awesome person be awesome. Awesome,
* right?
*
* ---------------------------------------------------------------------------
*
* This script, along with group_therapy.xml, were the last set of scripts
* shown at the talk (after pat_back.php).
*
* ===========================================================================
*
* This script is meant to be run after the audience has voted (vote.php), so
* it assumes that the SQLite database has already been populated with votes.
* See vote.php for more information.
*
* You'll need to fill in the values for $ACCOUNT_SID, $AUTH_TOKEN, and
* $CALLER_ID. The first two values can be found on the main page of your
* Twilio dashboard. The $CALLER_ID variable should be set to the Twilio
* phone number you're using.
*
* You'll also need to fill in the value for $TWIML_URL, which is the full URL
* to the location of group_therapy.xml.
*
*/
// The Twilio helper library can be found at https://github.com/twilio/twilio-php
require_once('./twilio.php');
// You picked the API version when you set up your Twilio phone number.
$API_VERSION = '2010-04-01';
// --- Fill these in ---
$ACCOUNT_SID = ''; // Found on the main page of your Twilio dashboard
$AUTH_TOKEN = ''; // Found on the main page of your Twilio dashboard
$CALLER_ID = ''; // Your Twilio phone number
$TWIML_URL = ''; // http://[yourhost]/group_therapy.xml
$db = new SQLiteDatabase("twilio_talk.sqlite");
$client = new TwilioRestClient($ACCOUNT_SID, $AUTH_TOKEN);
// Get the phone number of the first person to vote that they're awesome.
$result = $db->query('SELECT DISTINCT * FROM votes WHERE vote=1 LIMIT 1');
$row = $result->current();
$phone_number = $row['phone_number'];
$parameters['From'] = $CALLER_ID;
$parameters['To'] = $phone_number;
$parameters['Url'] = $TWIML_URL;
// Call up the awesome person and put them in the conference room.
$client->request('/'.$API_VERSION.'/Accounts/'.$ACCOUNT_SID.'/Calls', 'POST', $parameters);
// Get the phone number of the first person to vote that they're not awesome.
$result = $db->query('SELECT DISTINCT * FROM votes WHERE vote=0 LIMIT 1');
$row = $result->current();
$phone_number = $row['phone_number'];
$parameters['To'] = $phone_number;
// Call up the not awesome person and put them in the conference room.
$client->request('/'.$API_VERSION.'/Accounts/'.$ACCOUNT_SID.'/Calls', 'POST', $parameters);
?>
<?xml version="1.0" encoding="UTF-8" ?>
<Response>
<Dial>
<!-- Create a simple conference room called "GroupTherapy" that starts as
soon as two people join and exits as soon as one party hangs up. -->
<Conference beep="false" waitUrl="" startConferenceOnJoin="true" endConferenceOnExit="true">
GroupTherapy
</Conference>
</Dial>
</Response>
<?php
/*
* Code from Twilio talk given at the L.A. Hacker News Meetup on 11/13/2010
*
* During the talk, everyone was asked to vote (vote.php) whether they were
* awesome or not (text "1" for awesome, anything else for not awesome). This
* script works with pat_back.xml to call up the first person that voted and
* pat them on the back for being awesome. (Dials the number and Twilio's
* text-to-speech engine says "you're the man now, dog!")
*
* ---------------------------------------------------------------------------
*
* This script, along with pat_back.xml, were the second set of scripts shown
* (after vote.php). After this, we moved on to the final script,
* group_therapy.php and group_therapy.xml.
*
* ===========================================================================
*
* This script is meant to be run after the audience has voted (vote.php), so
* it assumes that the SQLite database has already been populated with votes.
* See vote.php for more information.
*
* You'll need to fill in the values for $ACCOUNT_SID, $AUTH_TOKEN, and
* $CALLER_ID. The first two values can be found on the main page of your
* Twilio dashboard. The $CALLER_ID variable should be set to the Twilio
* phone number you're using.
*
* You'll also need to fill in the value for $TWIML_URL, which is the full URL
* to the location of pat_back.xml.
*
*/
// The Twilio helper library can be found at https://github.com/twilio/twilio-php
require_once('./twilio.php');
// You picked the API version when you set up your Twilio phone number.
$API_VERSION = '2010-04-01';
// --- Fill these in ---
$ACCOUNT_SID = ''; // Found on the main page of your Twilio dashboard
$AUTH_TOKEN = ''; // Found on the main page of your Twilio dashboard
$CALLER_ID = ''; // Your Twilio phone number
$TWIML_URL = ''; // http://[yourhost]/pat_back.xml
$db = new SQLiteDatabase("twilio_talk.sqlite");
$client = new TwilioRestClient($ACCOUNT_SID, $AUTH_TOKEN);
$result = $db->query('SELECT DISTINCT * FROM votes WHERE vote=1 LIMIT 1');
$row = $result->current();
$phone_number = $row['phone_number'];
$parameters['From'] = $CALLER_ID;
$parameters['To'] = $phone_number;
$parameters['Url'] = $TWIML_URL;
$client->request('/'.$API_VERSION.'/Accounts/'.$ACCOUNT_SID.'/Calls', 'POST', $parameters);
?>
<?xml version="1.0" encoding="UTF-8" ?>
<Response>
<!-- Twilio's text-to-speech engine will read this out -->
<Say>You're the man now, dog!</Say>
<Hangup />
</Response>
<?php
/*
* Code from Twilio talk given at the L.A. Hacker News Meetup on 11/13/2010
*
* This is a simple voting script. Text "1" to your Twilio number to vote
* "yes," and anything else to vote "no."
*
* You can purchase a Twilio phone number at http://www.twilio.com, or use
* the sandbox phone number that comes with the free trial.
*
* ---------------------------------------------------------------------------
*
* This was the first script coded in front of the audience. After texting to
* vote, we moved on to the code in pat_back.php and pat_back.xml.
*
* ===========================================================================
*
* Assumes a SQLite database called "twilio_talk.sqlite exists in the same
* directory as this script. Create the database by running:
*
* $db = new SQLiteDatabase("twilio_talk.sqlite");
* $db->query('BEGIN; CREATE TABLE votes (id INTEGER PRIMARY KEY, phone_number TEXT, vote INTEGER); COMMIT;');
*
*/
$db = new SQLiteDatabase("twilio_talk.sqlite");
// Twilio will call the script using either POST or GET, depending on how you
// set up your phone number in your Twilio account. (The default is POST.) In
// real life, you'd want to properly escape these, just in case.
$phone_number = $_REQUEST['From'];
$sms = (int) $_REQUEST['Body'];
if ($sms == 1)
{
$db->query('INSERT INTO votes (phone_number, vote) VALUES ('.$phone_number.', 1)');
}
else {
$db->query('INSERT INTO votes (phone_number, vote) VALUES ('.$phone_number.', 0)');
}
?>
@rahims
Copy link
Author

rahims commented Nov 14, 2010

GitHub arranged the source files in reverse order, go through the scripts from bottom to top on this page (vote.php, pat_back.php, group_therapy.php).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment