Twilio Polls

Announcements

Design Notebooks

Let’s take a few minutes to review some of your Design Notebook entries from last week.

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 response.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="./response.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 American Sniper.</Say>
 <Say>Press 2 for Birdman.</Say>
 <Say>Press 3 for Boyhood.</Say>
 <Say>Press 4 for The Grand Budapest Hotel.</Say>
 <Say>Press 5 for The Imitation Game.</Say>
 <Say>Press 6 for Selma.</Say>
 <Say>Press 7 for The Theory of Everything.</Say>
 <Say>Press 8 for Whiplash.</Say>
 <Say>Or press 9 to repeat these choices.</Say>
 </Gather>
</Response>
// response.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 American Sniper.</Say>";
break;

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

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

case 4:
echo "<Say>Thanks for your response. You chose The Grand Budapest Hotel.</Say>";
break;

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

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

case 7:
echo "<Say>Thanks for your response. You chose The Theory of Everything.</Say>";
break;

case 8:
echo "<Say>Thanks for your response. You chose Whiplash.</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 American Sniper.</Say>
 <Say>Press 2 for Birdman.</Say>
 <Say>Press 3 for Boyhood.</Say>
 <Say>Press 4 for The Grand Budapest Hotel.</Say>
 <Say>Press 5 for The Imatation Game.</Say>
 <Say>Press 6 for Selma.</Say>
 <Say>Press 7 for The Theory of Everything.</Say>
 <Say>Press 8 for Whiplash.</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,American Sniper,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose American Sniper.</Say>";
 break;
 
 case 2:
 // write the callers phone number, selection, a timestamp of the record & respond
 fwrite($fp, $from . ',Best Picture,Birdman,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose Birdman.</Say>";
 break;
 
 case 3:
 // write the callers phone number, selection, a timestamp of the record
 fwrite($fp, $from . ',Best Picture,Boyhood,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose Boyhood.</Say>";
 break;
 
 case 4:
 // write the callers phone number, selection, a timestamp of the record & respond
 fwrite($fp, $from . ',Best Picture,The Grand Budapest Hotel,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose The Grand Budapest Hotel.</Say>";
 break;
 
 case 5:
 // write the callers phone number, selection, a timestamp of the record
 fwrite($fp, $from . ',Best Picture,The Imitation Game,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose The Imatation Game.</Say>";
 break;
 
 case 6:
 // write the callers phone number, selection, a timestamp of the record 
 fwrite($fp, $from . ',Best Picture,Selma,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose Selma.</Say>";
 break;
 
 case 7:
 // write the callers phone number, selection, a timestamp of the record
 fwrite($fp, $from . ',Best Picture,The Theory of Everything,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose The Theory of Everything.</Say>";
 break;
 
 case 8:
 // write the callers phone number, selection, a timestamp of the record
 fwrite($fp, $from . ',Best Picture,Whiplash,' . date("c", $t) . "\n");
 echo "<Say>Thanks for your response. You chose Whiplash.</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>
<?php
 header("content-type: text/xml");
 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 
 // collect the cller'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. 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?
<?php
 // we can get the number of the sender using the 'Fromn' 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":
 $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>

Challenge

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

Assignment

Partner Organization Interview and Follow-up Survey

  1. Meet with your team and combine all of your questions from last week’s assignment.
  2. Discuss your questions and determine if there are any other questions that need to be added.
  3. Refine your questions to fit into either the closed- or open-ended question type forms and organize them into 2 groups: initial interview questions, and secondary & followup questions
  4. Create a one-sheet list of your initial interview questions in preparation for a call with your Partner Organization (call time to be determined).
  5. Using the secondary & followup questions list, create a TCNJ Qualtrics survey as a starting point for a followup survey to your initial interview call.

Design Notebook

  1. Create a new page in your Design Notebook and label it “Interview & Survey” and today’s date.
  2. List the initial interview questions you and your group came up with in preparation for your initial interviews.
  3. Include a link to your TCNJ Qualtrics Survey.

*Assignments are due before class begins on Mondays. Be prepared to present your work in class for discussion.

One thought on “Twilio Polls”

Leave a Reply

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