I’m building some interesting things for my campus using Twilio, who has a great set of APIs for SMS and voice applications. Through Twilio, I have a phone number that accepts incoming texts. During my development and testing, I realized, “what would happen if someone called the text number?”
The easy answer, just tell Twilio to respond with a simple message. Easy, right? Yes. Fun? Not yet.
One the ways I always learn a new system is to build something interesting, something fun that isn’t terribly useful but lets me poke around, try new things and break my code often and badly.
So I decided to build an app that gives you a random “bro” saying when you call. Things that, you know, a bro would say. After that genius idea, it was super easy in just a few lines of PHP to have a voice response app that read out a statement when you call a certain number.
Here’s the code. It’s messy, and there are things I’d do much better for production, but the idea was a quick and dirty sample.
<?php $bro = array(); $bro[] = "Sup, bro?"; $bro[] = "What do you think of my tap out Tee shirt?"; $bro[] = "This is Bro Hi Oh!"; $bro[] = "Want to watch U F C two fifty four tonight?"; $bro[] = "Bro. You're a bro."; $bro[] = "Bro, do you even lift?"; $bro[] = "Bro, check out my new affliction t-shirt"; $bro[] = "Bro, I just got floor seats for limp biscuit!"; $bro[] = "Bro, can you spot me some muscle milk?"; $bro[] = "Bro, I love that new Ed Hardy shirt."; $bro[] = "Bro, I can't wait for spring break!"; $bro[] = "Bros before hoes"; $bro[] = "Bro. shots. Now."; $bro[] = "Bro, Vin Diesel is an amaze bro."; $bro[] = "Hey Bro sef. You look great today."; $bro[] = "Hey Bro, you got any rillos?"; $bro[] = "cool story bro."; $bro[] = "Come at me, bro!"; $brosaying = $bro[array_rand($bro)]; header("content-type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; ?> <Response> <Say voice="man"><?php echo $brosaying;?></Say> </Response>
Basically, when a call is made, I told Twilio to look for a particular URL, where it would expect to receive an XML (or TwiML as they call it) with a set of actions to do, such as say something, play a recording, record a user’s input, or record keypresses. It’s a very powerful API.
We played around in the office and made a bunch of calls to the number, and laughed at what we’d get. Why not push it a little farther and give the user some stats about what caller they are. We’ll just store that in a database along with what bro saying is randomly chosen so we can do some data analysis later. That code looks like this:
<?php $mysqli = mysqli_connect("localhost", "dbuser", "dbpass", "brocalldatabase"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // Select the number of rows in the brocall database $query = "SELECT COUNT(*) from brocall"; $result = mysqli_query($mysqli,$query); $numcall = mysqli_fetch_row($result); // Build our array of bro sayings $bro = array(); $bro[] = "Sup, bro?"; $bro[] = "What do you think of my tap out Tee shirt?"; $bro[] = "This is Bro Hi Oh!"; $bro[] = "Want to watch U F C two fifty four tonight?"; $bro[] = "Bro. You're a bro."; $bro[] = "Bro, do you even lift?"; $bro[] = "Bro, check out my new affliction t-shirt"; $bro[] = "Bro, I just got floor seats for limp biscuit!"; $bro[] = "Bro, can you spot me some muscle milk?"; $bro[] = "Bro, I love that new Ed Hardy shirt."; $bro[] = "Bro, I can't wait for spring break!"; $bro[] = "Bros before hoes"; $bro[] = "Bro. shots. Now."; $bro[] = "Bro, Vin Diesel is an amaze bro."; $bro[] = "Hey Bro sef. You look great today."; $bro[] = "Hey Bro, you got any rillos?"; $bro[] = "cool story bro."; $bro[] = "Come at me, bro!"; // Select a random bro-ism $brosaying = $bro[array_rand($bro)]; // Save the particular pro-ism to the DB, along with a timestamp that MySQL puts in. $query2 = "INSERT INTO brocall VALUES (NULL,NULL,'".$brosaying."')"; mysqli_query($mysqli,$query2); mysqli_close($mysqli); // Now let's generate the TwiML for Twilio to read header("content-type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; ?> <Response> <Say voice="alice" language="en-US">Hello, welcome to Bro call. You are caller number <?php echo $numcall[0]; ?> to Bro call. Please stand by for your bro saying.</Say> <Pause /> <Say voice="man"><?php echo $brosaying;?></Say> <Pause /> <Say voice="alice" language="en-US">Thank you for calling Bro call. Goodbye.</Say> </Response>
That’s it. When you call, you get a recording tell you what caller number you are, then your bro saying and then a goodbye message. Super easy, and super fun.
I have about $11 USD in my Twilio account, so go ahead and call BroCall and try it out. Even though the call itself is about 15 seconds, it costs me a penny. Once the money is gone, it’ll shut off.
The number is 814-746-3204.
Do you want a fancy schmancy, affordable texting or other voice web app like this for your college or company? Contact me and we (Gas Mark 8) would love to build you something really cool.
Nice! I think the best way to learn is to play with new tech. I got “Bro’s Before Hoes”