Code

  • Advice,  Code

    Don’t Break the Chain

    Before I decided to take Referly full time it was just a side project for a couple years. For New Year’s 2012 I set a personal goal to code every day (my job at the time was head of marketing for a developer company), and in early February of this year I decided to get serious with a “don’t break the chain” mindset.

    Referly was the project I decided to rebuild from the ground up and 6 weeks into my routine I let Jeff know I would be leaving so we could start the transition plan.  Shortly after that conversation I had some beers with Joseph Walla from HelloFax and he convinced me to at least try to get into YC.  We all know how that turned out.

    So thank you Jerry Seinfeld for your advice on productivity – it worked for me.

    From the article:

    He said the way to be a better comic was to create better jokes and the way to create better jokes was to write every day. But his advice was better than that. He had a gem of a leverage technique he used on himself and you can use it to motivate yourself—even when you don’t feel like it.

    He revealed a unique calendar system he uses to pressure himself to write. Here’s how it works.

    He told me to get a big wall calendar that has a whole year on one page and hang it on a prominent wall. The next step was to get a big red magic marker.

    He said for each day that I do my task of writing, I get to put a big red X over that day. “After a few days you’ll have a chain. Just keep at it and the chain will grow longer every day. You’ll like seeing that chain, especially when you get a few weeks under your belt. Your only job next is to not break the chain.”

    “Don’t break the chain,” he said again for emphasis.

     

     

     

  • Code

    How I Built a Multi-User Door Buzzer for Our Apartment

    Full disclosure: a lot of people have talked about this idea, this is just my implementation of it for our apartment.  You can check out Buzzeromatic.com if you want someone else to administer yours or post to Elance/oDesk/HackerNews/Twitter to get someone to build it for you.  Look for something cool from @gregkoberger with all those features I left out (user management, UI, adding new roomies, adding more numbers etc.)

    Making a Present for My Roommates

    About a month ago, I moved to a 3-story loft in SOMA with my husband Kevin and our good friends Park and Kat.  My sister also graduated college and joined a local startup, so she’ll be moving in with us next week.  With 5 people living here and only 2 master keys to the front door, we had a bit of an access problem right away.  My roommates, knowing I haven’t had a chance to code since the move started, were nice enough to suggest a build an app with my Twilio skills (full disclosure: I work there) and we tested it out last night – it works, so I’d love to share.

    How A Call Box Works

    If you’ve lived in an apartment with a call box for buzzing people into your apartment before, this will sound pretty familiar.  There is a list of names and corresponding codes listed on the callbox display, you dial the number and it rings the person who lives there, they press a key and the door is unlocked.

    What happens inside the callbox is a little more interesting – because the sound the keypress makes, which is called a DTMF tone, is actually a pretty amazing little thing.  Tone dialing was arguably one of the earliest massive implementations of human-to-computer communication.

    Phreaking Out the Phone with DTMF Tones

    There is a long history of phreaking (the image to the right is of a bluebox built by Steve Jobs and Steve Wozniak, on display at the Computer History Museum) by playing DTMF tones and other tones (pulses, pins, etc), and the things telecommunications hackers achieved without any kind of API like Twilio is pretty amazing.  In my case, the call box just requires the person who receives the call to press 6 to open the door, and responds when the 6 DTMF tone is played.  This tone doesn’t have to come from pressing a key though,  I can just play the audio file into the phone to mimic the action of a keypress – and the doors opens!

    The simplest implementation of this is just to have the door automatically open when anyone dials your extension.  I don’t recommend setting your callbox up this way, because you might accidentally let in people who are messing with the box looking for a way in so they can do bad things.  My street has a lot of bums and other riff-raff on it, so I wanted something with a couple different types of security.  So here’s what I did.

    Setting Up My Call Box with Twilio

    If you haven’t heard of my company, Twilio, before the really quick elevator pitch is that we are the AWS of telecommunications, making it easy to send/receive calls and text messages programmatically and only paying for what you use.  It’s pretty sweet, its a startup, and I’d love your feedback on it.  So here we go:

    • Get a local Twilio number (or you can use the sandbox number for free)
    • Tell your building administrator to add your name/number to the box
    • Build a simple app that forwards the call to your cell phone so you can make sure the box can recognize DTMF tones passed from a forwarded call

    Doing this test before you build your full blown app is really important, it will save you from debugging an issue outside of your control.  You just need to create this little callbox.xml file, save it somewhere on the web (publicly accessible on Dropbox is a handy option, our try Twimlets)

    callbox.xml (replace 415-555-1212 with an actual phone number)

    1. <Response>
    2. <Dial>415-555-1212</Dial>

    3. </Response>

If you are using your Twilio trial account, make sure you use the Twilio sandbox number and remember your pin code for testing (or email me at danielle@twilio.com and I can manually remove this restriction from your account). If you run into problems just email help@twilio.com for 24/7 support, or tweet @twilio

Building the Full App

Now that your testing is done, it’s time to build the full app with just a little PHP.  This took me about 25 minutes to write from scratch in Emacs, probably will take you a whole lot less with all the sample code.  Here’s the spec for how the app should work:

Need to have:

  • visitor or roommate dials our extension in the call box and our Twilio number is called
  • menu is read to visitors, giving them the option of which roommate to contact
  • roommates have a secret code they can punch in to bypass the menu and open the door
  • if the visitor selects one of the roommates from the menu options, that roommate gets a call and presses a number on their cellphone dialpad to buzz in

Nice to have:

  • menu selections can be made at anytime, without waiting for the menu to finish, because that is just annoying
  • roommates can be simultaneously dialed on multiple numbers (cellphone, work, house phone) if they want
  • fun audio files can be played when someone is let in

callbox.php – this file controls what happens when the callbox is dialed

–>

  1. <?php
  2. header(“content-type: text/xml”);
  3. echo “<?xml version=\”1.0\” encoding=\”UTF-8\”?>\n;
  4. ?>
  5. <Response>
  6. <Gather action=”/doorbell_gather.php” method=”POST”>
  7. <Say voice=”woman”>If you are here for Katrina, press 2.</Say>
  8. <Say voice=”woman”>For Danielle, press 3.</Say>
  9. <Say voice=”woman”>For Park, press 4.</Say>
  10. <Say voice=”woman”>For Kevin, press 5.</Say>
  11. </Gather>
  12. </Response>

Two important things to point out here. First, notice that the menu options in the tag are nested within . This is awesome, because it means Twilio is listening for a keypress the entire time and you can interrupt the menu with your selection at anytime. Also, you’ll see if you check out the Twilio example code that usually includes the numDigits parameter, but we’re excluding it on purpose here because we want to accept secret pin codes in addition to single digit selections. You’ll see why in a moment.

doorbell_gather.php – this file determines what to do with the keypad data we just received

Note on this code: There are definitely more elegant ways to write this, but in my mission to convert all my roommates to geeks I’ve opted for something they can easily understand in case they want to change their secret codes without my help.  All pincodes have been changed for this example.

–>

  1. <?php
  2. if($_REQUEST[‘Digits’] == ‘2’) {
  3. header(“Location: katrina.xml”);
  4. }
  5. if($_REQUEST[‘Digits’] == ‘3’) {
  6. header(“Location: danielle.xml”);
  7. }
  8. if($_REQUEST[‘Digits’] == ‘4’) {
  9. header(“Location: park.xml”);
  10. }
  11. if($_REQUEST[‘Digits’] == ‘5’) {
  12. header(“Location: kevin.xml”);
  13. }
  14. if($_REQUEST[‘Digits’] == ‘1234’) {
  15. header(“Location: secret-danielle.xml”);
  16. }
  17. if($_REQUEST[‘Digits’] == ‘6677’) {
  18. header(“Location: secret-park.xml”);
  19. }
  20. if($_REQUEST[‘Digits’] == ‘9988’) {
  21. header(“Location: secret-kevin.xml”);
  22. }
  23. if($_REQUEST[‘Digits’] == ‘6786’) {
  24. header(“Location: secret-katrina.xml”);
  25. }
  26. header(“content-type: text/xml”);
  27. echo “<?xml version=\”1.0\” encoding=\”UTF-8\”?>\n;
  28. ?>
  29. <Response>
  30. <Say>I’m sorry, but the person you attempted to reach is unavailable.  Please try again later.</Say>
  31. </Response>

So Twilio is passing the keypress data as ‘Digits’ and we’re telling Twilio where to go depending on what was pressed. Pretty simple. The little bit of TwiML at the bottom only plays if the roommate called doesn’t answer their phone.

So now we need to create:

  • roommate.xml – which calls the selected roommate so they can buzz in their guest
  • secret-roommate.xml – which automatically opens the door when the code is entered

You might be wondering why everyone has their own secret-roommate.xml file, when they all do the same thing.  I decided it would be fun to prank my roommates with a funny theme song or movie quote before the door would unlock…

To complete this code, you’ll need to get an audio file (.wav or .mp3) of the DTMF tone you want to play back to the machine.  I used this awesome DTMF generator, and they host the audio file for you.

roommate.xml – the file that calls the selected roommate

  1. <Response>
  2. <Say>Connecting you to [Roommate’s Name] now</Say>
  3. <Dial>415-555-1212</Dial>
  4. </Response>

secret-roommate.xml – the file that opens the door is the roommate enters the correct pin code. The first <Play> contains a fun audio clip from Back to the Future, the second one plays the DTMF tone that will open the door (make sure to change this depending on which tone will open your specific door).

  1. <Response>
  2. <Play>http://moviewavs.com/0059305935/WAVS/Movies/Back_To_The_Future/seriousBLEEP.wav</Play>
  3. <Play>http://www.dialabc.com/i/cache/dtmfgen/wavpcm8.300/6.wav</Play>
  4. </Response>

So there you go, now you can manage a callbox for a bunch of roommates, add secret pin codes, and even give selective access to delivery people, cleaning staff, or whoever else is coming by to visit. Let me know if you find any bugs or have ideas for how to make this cooler in the comments.

Photo credits:

  • Call box: http://www.flickr.com/photos/carol329/300231462/
  • Touch Tone Telephones, 1966: http://www.flickr.com/photos/roadsidepictures/3601261522/
  • Bluebox: http://www.flickr.com/photos/awarnack/110798864/
  • Code

    Stuff I Built: Simple International Calling Card with Twilio

    Reposted from the Twilio Company Blog

    When I came downstairs this morning I was greeted by two bubbly and very sleep deprived Australians eager for some tea, and a chance to call Mum.  My first thought – there’s a Twilio app for that (or there will be soon)!

    Being in the Christmas sprint, I decided I’d quickly code up an application that would make it easy for them to call a U.S. number from the landline at our house or any local phone, and be forwarded to their mom’s, boyfriends, and other folks through a simple menu.  20 minutes later, we made our first call!

    Setting Up the International “Calling Card”

    Twilio doesn’t provide international phone numbers, but you can set up a U.S. number and have it forward to an international destination using the <Dial> verb.  You don’t even need to use the REST API to make the outbound calls, its so simple!

    Files to create:

    • * Handler for the incoming call, to greet the caller and read the menu, gather the menu selection keypress
    • * PHP handler for taking the keypress and directing the application to the right file to dial the number
    • * Files for each of the phone menu options, going to the different numbers to call

    Setting up incoming-call.php

    This first file is the one that I pointed the Twilio phone number to, to handle incoming calls.  It greets the caller and reads them a menu of people to call, and asks them to press a number to start.

    It looks something like this:

    <?php
     header("content-type: text/xml");
     echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    
    <Response>
    
    <Gather numDigits="1" action="make-call.php" method="POST">
    
     <Say voice="woman">Hey girls, ready to call someone? If you know your s\
    election, you may make it at any time.</Say>
     <Say>Press 1 to Call Laurens Mom</Say>
     <Say>Press 2 to Call Jace</Say>
     <Say>Press 3 to Call Eleesa's Home</Say>
     <Say>Press 4 to Call Duh lane ah's Cell Phone</Say>
     <Say>To get help, Press 5 to Call Danielle</Say>
    
    </Gather>
    
    <Say voice="woman">Thanks for using this Twil ee oh app, created by Danielle. \
    Happy holidays!</Say>
    
    </Response>
    

    Setting up make-call.php

    After the caller has pressed as key, the application posts the results to make-call.php, so we need to create a php file that understands what to do next with that information, and route the call.

    <?php 
     
            if($_REQUEST['Digits'] == '1') { 
                    header("Location: call-laurens-mom.php");
                    die;
            }
    
            if($_REQUEST['Digits'] == '2') {
                    header("Location: call-jace.php");
                    die;
            }
    
            if($_REQUEST['Digits'] == '3') {
                    header("Location: call-elisas-home.php");
                    die;
            }
    
            if($_REQUEST['Digits'] == '4') {
                    header("Location: call-dlaina-cell.php");
                    die;
            }
    
            if($_REQUEST['Digits'] == '5') {
                    header("Location: call-danielle.php");
                    die;
            }
    
    
            header("content-type: text/xml");
            echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    
    ?>
    

    Setting up TwiML to Connect the Call

    As you can see in the previous php script make-call.php, each selection directed the application to a different file.  This file is a very simple piece of TwiML that uses the <Dial> verb to connect the call.  Each one is pretty much the same, and looks like this:

    <?php
            header("content-type: text/xml");
            echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    
    <Response>
    
    <Say>Connecting you to Danielle, for help with this application..</Say>
    
    <Dial>4256987497</Dial>
    
    </Response>
    

    It’s Not Pre-paid, It’s Pay-As-You-Go

    The best part about this for Elisa and Lauren is that it isn’t a prepaid card where they spend $50 and and are stuck with the card, even if they don’t use it up.  I’m billing them for exactly the amount they use, and they don’t have to pay for it until after the fact.  I can imagine turning custom pay-as-you-go calling cards into a really interesting business.

    So there you have it.  If you have any international guests in your home this holiday season, or are interested in going into the calling card business, this might be a good place to start.  The app took less than 20 minutes to write, mostly because we were goofing around with the text to speech quite a bit, and is written with PHP.

    You do need an upgraded Twilio account to get a phone number and make international calls, so maybe some Twilio minutes would be a good thing to ask Santa to bring you.  Happy holidays!