Building a Happy New Years Voice Broadcasting App

December 31, 2009
Written by
Danielle Morrill
Contributor
Opinions expressed by Twilio contributors are their own

Twilio Bug Logo

Over lunch today we were talking about how we wanted to make a Twilio Voice app to ring in the New Year, and I wanted to make it something really social where people would feel more connected to the entire world after using it.  Ambitious?  Yeah.  Impossible?  Nope.  So I built a Twilio app.  Here’s how it works…

TO TRY IT OUT*

415-483-1336

*you will get a phone call around 11:30pm PST

Step 1 – Call In and Leave Your Message for the World

The first step is to set up the file to handle incoming calls to my Twilio number.

Creating incoming.php

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <Response>
  3. <Say>Happy New Years from Twil ee oh! Record your News Years Eve wishes for the world after the beep, and press star when you are done.</Say>
  4. <Record action=”new-recording.php” method=”POST” maxLength=”180″ finishOnKey=”*” />
  5. </Response>

Step 2 – Gather the Recording URLs and Phone Numbers from Inbound Calls

The Twilio REST API makes it very simple to get a list of recordings by account SID using the Recordings Resource, so the next step is to gather up the recordings from incoming callers so we can put them all into a single TwiML file for the outbound call that will take place later tonight.

When you use the two URL examples below, an authentication window will pop up and you will need to use either your Twilio username and password or account SID and token.

Getting a List of Recordings

https://api.twilio.com/2008-08-01/Accounts/{Account SID}/Recordings?DateCreated=2009-12-31

Getting a Single Recording from the List

https://api.twilio.com/2008-08-01/Accounts/{Account SID}/Recordings/{Recording SID}

Creating new-recording.php

  1. <?php
  2. header(“content-type: text/xml”);
  3. echo “<?xml version=1.0 encoding=UTF-8?>n;
  4. ?>
  5. <Response>
  6. <Say>Thank you, your recording has been saved.  You will receive a call at midnight tonight, where you will hear the New
  7. Years wishes of the world.  Have a wonderful two thousand ten, from everyone at twil ee oh.</Say>
  8. </Response>
  9. <?php
  10. $phonenumbers = ‘phonenumbers.txt’;
  11. $recordings = ‘allrecordings.txt’;
  12. $caller_number = $_REQUEST[‘Caller’];
  13. $recording_URL = $_REQUEST[‘RecordingUrl’];
  14. /* Append the caller’s phone number to the list to call later */
  15. file_put_contents($phonenumbers, $caller_number.n, FILE_APPEND);
  16. /* Append the recording URL to the list of recordings to play during the call */
  17. file_put_contents($recordings, $recording_URL.n, FILE_APPEND);
  18. ?>

Step 3 – Initiate a Massive Outbound Call with the REST API

Since
the phone lines are pretty jammed at midnight each New Years Eve, I’m
going to initiate the call at 11:30pm Pacific Time instead, mostly to
make sure people with cell phones will actually get it.  The idea is
that every person who contributed to the massive New Years Eve message
will get to hear the entire thing.

I’ve got to get home before the traffic gets crazy, so I’m going to go ahead and post this and write the rest of the code when I get home.  The last piece to add will enable me to initiate calls to all the people who have called in with the REST API.

In the meantime, you can learn about initiating calls with the Twilio REST API, or watch this video.

More Functionality… If I Have Time

I was thinking about how to add a truly “viral” feature to this, where each person who used it would be able to include more people in the experience.

What if the caller had the option to input a friend’s phone number, so that they would receive a call asking them to contribute their New Years wishes for 2010?  How hard would that be?