Twilio Polls

Announcements

Project Status

Let’s take a few minutes to get caught up on the status of each of your projects. Have you created your list of questions for your partner contacts?

Surveys

Why do we use surveys and questionnaires?

Survey Guide

Types of Survey Questions

  • Closed-ended Questions
  • Open-ended Questions

Types of Survey Questions on Explorable

Online Survey Tools

Here’s an example TCNJ Qualtrics Survey

Phone Polls

  1. Download the source code for this week’s examples from the class Github Organization.
  2. Unzip the  repository on your local machine and upload the contents to a new folder in the ‘www’ folder on your TCNJ web account.
  3. Let’s try out the examples one by one.

Basic Phone Interview Loop

flowchart_lg

Interview Loop with <Gather>

  1. After you’ve uploaded the poll_gather.xml and respond.php files to your TCNJ web account, log into Twilio, click into your number detail page, and edit the Voice request url for your number to point to ‘poll_gather.xml’.
  2. Call your Twilio number.
  3. What happened?
<!-- poll_gather.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<Response>
 <!-- ask poll question, default finish key is # -->
 <Gather action="./respond.php" method="get">
 <Say>Welcome to the 2016 Oscars phone poll.</Say>
 <Say>What film would you vote for for Best Picture?</Say>
 <Say>Press 1 for The Big Short.</Say>
 <Say>Press 2 for Bridge of Spies.</Say>
 <Say>Press 3 for Brooklyn.</Say>
 <Say>Press 4 for Mad Max Fury Road.</Say>
 <Say>Press 5 for The Martian.</Say>
 <Say>Press 6 for The Revenant.</Say>
 <Say>Press 7 for Room.</Say>
 <Say>Press 8 for Spotlight.</Say>
 <Say>Or press 9 to repeat these choices.</Say>
 </Gather>
</Response>
// respond.php
<?php
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';

// collect the selection number
$selection = (int) $_REQUEST['Digits'];

echo '<Response>';

switch($selection){
case 1:
echo "<Say>Thanks for your response. You chose The Big Short.</Say>";
break;

case 2:
echo "<Say>Thanks for your response. You chose Bridge of Spies.</Say>";
break;

case 3:
echo "<Say>Thanks for your response. You chose Brooklyn.</Say>";
break;

case 4:
echo "<Say>Thanks for your response. You chose Mad Max Fury Road.</Say>";
break;

case 5:
echo "<Say>Thanks for your response. You chose The Martian.</Say>";
break;

case 6:
echo "<Say>Thanks for your response. You chose The Revenant.</Say>";
break;

case 7:
echo "<Say>Thanks for your response. You chose Room.</Say>";
break;

case 8:
echo "<Say>Thanks for your response. You chose Spotlight.</Say>";
break;

case 9:
echo "<Say>O K. Redirecting back to the choices.</Say><Redirect>./poll_gather.xml</Redirect>";
break;

default:
echo "<Say>I didn't understand your selection.</Say><Redirect>./poll_gather.xml</Redirect>";
break;
}

// close out file pointer
fclose($fp);

echo '</Response>';
?>

Storing Responses to a File

  1. After you’ve uploaded the poll_gather_data.xml, record_data.php and data.txt files to your TCNJ web account, log into Twilio, click into your number detail page, and edit the Voice request url for your number to point to ‘poll_gather_data.xml’.
  2. Call your Twilio number.
  3. What happened?
  4. Now point your web browser to http://www.tcnj.edu/~your-user-name/your-folder/data.txt
  5. What do you see?

*A note on file permissions – these examples write the callers’ selections to a comma-separated file (csv). You’ll need to set the file permissions on your data file to ‘read & write’ with settings 666.

<!-- poll_gather_data.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<Response>
 <!-- ask poll question, default finish key is # -->
 <Gather action="./record_data.php" method="get">
 <Say>Welcome to the 2015 Oscars phone poll.</Say>
 <Say>What film would you vote for for Best Picture?</Say>
 <Say>Press 1 for The Big Short.</Say>
 <Say>Press 2 for Bridge of Spies.</Say>
 <Say>Press 3 for Brooklyn.</Say>
 <Say>Press 4 for Mad Max Fury Road.</Say>
 <Say>Press 5 for The Martian.</Say>
 <Say>Press 6 for The Revenant.</Say>
 <Say>Press 7 for Room.</Say>
 <Say>Press 8 for Spotlight.</Say>

<Say>Or press 9 to repeat these choices.</Say> </Gather> </Response>

// record_data.php
<?php
 header('Content-type: text/xml');
 echo '<?xml version="1.0" encoding="UTF-8"?>'; 
 
 // collect the selection number
 $selection = (int) $_REQUEST['Digits'];
 // collect the caller's phone number
 $from = $_REQUEST["From"];
 // get the exact time of the response
 $t = time();
 // set the timezone
 date_default_timezone_set('America/New_York');
 // open a file ponter to write our data
 $fp = fopen("data.txt", "a");
 
 echo '<Response>';
 
 switch($selection){
 case 1:
 // write the callers phone number, selection, a timestamp of the record
 fwrite($fp, $from . ',Best Picture,The Big Short,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose The Big Short.</Say>";
 break;
 
 case 2:
 // write the callers phone number, selection, a timestamp of the record & respond
 fwrite($fp, $from . ',Best Picture,Bridge of Spies,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose Bridge of Spies.</Say>";
 break;
 
 case 3:
 // write the callers phone number, selection, a timestamp of the record
 fwrite($fp, $from . ',Best Picture,Brooklyn,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose Brooklyn.</Say>";
 break;
 
 case 4:
 // write the callers phone number, selection, a timestamp of the record & respond
 fwrite($fp, $from . ',Best Picture,Mad Max Fury Road,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose Mad Max Fury Road.</Say>";
 break;
 
 case 5:
 // write the callers phone number, selection, a timestamp of the record
 fwrite($fp, $from . ',Best Picture,The Martian,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose The Martian.</Say>";
 break;
 
 case 6:
 // write the callers phone number, selection, a timestamp of the record 
 fwrite($fp, $from . ',Best Picture,The Revenant,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose The Revenant.</Say>";
 break;
 
 case 7:
 // write the callers phone number, selection, a timestamp of the record
 fwrite($fp, $from . ',Best Picture,Room,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose Room.</Say>";
 break;
 
 case 8:
 // write the callers phone number, selection, a timestamp of the record
 fwrite($fp, $from . ',Best Picture,Spotlight,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose Spotlight.</Say>";
 break;
 
 case 9:
 echo "<Say>O K. Redirecting back to the choices.</Say><Redirect>./poll_gather_data.xml</Redirect>";
 break; 
 
 default:
 echo "<Say>I didn't understand your selection.</Say><Redirect>./poll_gather_data.xml</Redirect>";
 break;
 }
 
 // close out file pointer
 fclose($fp);
 
 echo '</Response>';
?>

Interview Loop with <Record>

  1. After you’ve uploaded the poll_record_data.xml and recording.php files to your TCNJ web account, log into Twilio, click into your number detail page, and edit the Voice request url for your number to point to ‘poll_record_data.xml’.
  2. Call your Twilio number.
  3. What happened?
  4. Now point your web browser to http://www.tcnj.edu/~your-user-name/your-folder/data.txt
  5. What do you see?
<!-- poll_record_data.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<Response>
 <Say>Welcome to the 2015 Oscars Phone Poll</Say>
 <Say>What was your favorite movie last year? Record your answer at the beep. Press the pound key when you're done recording.</Say>
 <Record 
 action="http://www.tcnj.edu/~your-username/your-folder/recording.php"
 method="GET"
 maxLength="20"
 finishOnKey="#"
 />
</Response>
/* recording.php */

<?php
 header("content-type: text/xml");
 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 
 // collect the caller's phone number
 $from = $_REQUEST["From"];
 // get the exact time of the response
 $t = time();
 // set the timezone
 date_default_timezone_set('America/New_York');
 // open a file ponter to write our data
 $fp = fopen("data.txt", "a");
 
 // get the url to the voice recording
 $recording = $_REQUEST['RecordingUrl'];
 // get the duration of the voice recording
 $duration = $_REQUEST['RecordingDuration'];
 // write the data file
 fwrite($fp, $from . ',Favorite Movie this year,' . $recording . ',' . $duration . ',' . date("c", $t) . "\n");
?>
<Response>
 <Say>Thanks for taking our poll. Your favorite movie this year was</Say>
 <Play><?php echo $recording; ?></Play>
</Response>

An SMS Example

  1. After you’ve uploaded the sms_poll_data.php file to your TCNJ web account, log into Twilio, click into your number detail page, and edit the Messaging request url for your number to point to ‘sms_poll_data.php’.
  2. Text the word ‘poll’ to your number.
  3. What happened?
  4. Check for a text back from the system and follow the instructions.
  5. What happened this time?
  6. Now point your web browser to http://www.tcnj.edu/~your-user-name/your-folder/data.txt
  7. What do you see?
/* sms_poll_data.php */

<?php
 // we can get the number of the sender using the 'From' request value
 $from = $_REQUEST['From'];
 $msg = $_REQUEST['Body'];
 
 header("content-type: text/xml");
 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 // get the exact time of the response
 $t = time();
 // set the timezone
 date_default_timezone_set('America/New_York');
 // open a file ponter to write our data
 $fp = fopen("data.txt", "a");
 switch($msg){
 case "Poll":
 case "POLL":
 case "poll":
 $out = "Thanks for taking our poll! Text us back with the name of your favorite movie from last year!";
 break;
 
 default:
 // write the data file
 fwrite($fp, $from . ',sms,Favorite Movie this year,' . $msg . ',' . date("c", $t) . "\n");
 $out = "Really? Your favorite film last year was " . $msg;
 break;
 }
 
 // close out file pointer
 fclose($fp);
?>
<Response>
 <Message><?php echo $out; ?></Message>
</Response>

Challenges

Twilio

  • Break up into your groups and compare notes on your list of questions for your Partner Organization from last week’s assignment.
  • Using the Phone Poll design pattern, create a Phone Poll app that asks 3 closed-ended questions and one open-ended question.
  • Be sure to store the responses in a comma-separated (csv) file on your TCNJ web account or other hosting account.

Alexa

Using the Reindeer Games tutorial, set up your own example trivia game.

Cordova

  • Cordova SMS plugin – Install the plugin and try out the “Using the Plugin’ example.
  • Review the Cordova Plugin APIs page
  • What other plugins might you combine with an SMS interface to improve the basic SMS capabilities of your phone in a useful way?

Assignment

Reading

Design Notebooks

  • Create a new page in your Design Notebooks and title it “The Jack Principles”
  • Read The Jack Principles and write few paragraphs discussing how you might apply the three main principles for creating “interactive conversation interfaces” to your project

Projects

  • Make initial contact with your contacts
  • Arrange to meet with them to present your project concepts and to interview them using the questions you developed last week
  • Based upon the feedback from your partner contacts, begin diagraming your “interactive conversation interface” flows

*Submit the link to your Design Notebook to Canvas before we meet again next week

Leave a Reply

Your email address will not be published. Required fields are marked *