UPDATE: The code needed to be changed just a little, since the php function scandir did not work any more. Details below.

I had my friend Drew (@VVADrew) do some voice work with a collection of funny thoughts for a Twilio contest a while back. The call will loop indefinitely, each time choosing a random thought from a collection I saved to Amazon S3. Hit the button to have a listen.

The judges got a kick out of it.

The code itself is quite simple:

[php title="voice.php"]
$dir = "audio";
$bucket = "https://s3.amazonaws.com/twiliodeepthought";
$listing = simplexml_load_string(file_get_contents($bucket));
$files = $listing->Contents;
$random = rand(0, count($files));
$file = $files[$random]->Key;

echo <<<END
<Pause length="4" />
<Redirect method="GET">voice.php</Redirect>


Now, we pull an XML file listing the contents of the bucket, then pick one from our array Contents.

Here is an older version, made in python for Google App Engine
[python title="main.py"]
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
import twilio
from random import choice

SMS_MAX = 155

class MainHandler(webapp.RequestHandler):
def get(self):
f = open('deepthoughts.txt', 'r')
data = f.readlines()
deepThought = choice(data).replace("n", "")

r = twilio.Response()
for i in range(0, len(deepThought) / SMS_MAX + 1):
chunkStart = i * SMS_MAX
chunkEnd = chunkStart + SMS_MAX
messageNumber = ""
if(len(deepThought) > SMS_MAX): messageNumber = `i+1` + "/" + `len(deepThought)/SMS_MAX+1` + " "
r.addSms(messageNumber + deepThought[chunkStart:chunkEnd])

def main():
application = webapp.WSGIApplication([('/', MainHandler)],

if __name__ == '__main__':

For more information, watch my screencast for the same system in Python