TwiML Bins: A Serverless and Codeless Way to Try Twilio

TwiML Bins: A Serverless and Codeless Way to Try Twilio
November 26, 2017
Written by
Paul Kamp
Twilion

In May, Twilio announced Twilio Functions, a serverless Node.js environment for spinning up functions without having to find hosting. Functions is an easy server replacement – but did you know we have other ways to rapidly build and iterate on your product serverless-ly? (Including a new visual communications tool, Twilio Studio, in developer preview).

Today I’m thrilled to shine a light on TwiML Bins, an incredibly simple way to start building or prototyping with Twilio. TwiML Bins are all you need to enable very desirable – and previously quite expensive – communications features like Cloud-based Voicemail or SMS masking and forwarding with Twilio.

Oh, and did I mention you won’t need to write a single line of code? Onward!

Get Yourself a Twilio Number or Three

The magic of TwiML Bins starts in the Twilio Console. Log into your Twilio account (or create a new one – try us free!), then surf over to the Twilio Phone Numbers Console.

If you just started a new account (or don’t have a spare number), hit the big red plus (‘+’) button to start your search. For SMS Forwarding, click ‘SMS’ and for Cloud Voicemail click ‘Voice’, then hit the search button.

Buy a phone number online with Twilio

Find a suitable number – for some countries SMS and Voice capable numbers are split requiring two numbers (run two searches) – then hit the ‘Buy’ box. You’ll have to confirm your purchase, but then you’re ready to go!

Seriously simple stuff – and I solemnly swear the rest of this is just as easy. Let’s do it!

Set Up an SMS-Forwarder… and Learn Some TwiML

TwiML, or the Twilio Markup Language, is a set of simple verbs and nouns that allow you to tell us over here at Twilio what to do with all the messages and calls your number receives. Suffice to say, our TwiML documentation is extensive – but you only need to understand a small subset to get going today.

Now that you know what TwiML is, note that TwiML Bins are a convenient place to park said TwiML! Click here and visit the TwiML Bin section of the console, then create a new TwiML Bin with the red plus (‘ ’) button.

What next? Glad you asked! Paste the following markup into the box, substituting Jenny’s number +18888675309 with your actual, real, about-to-be-masked cell phone number:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message to="+18888675309">{{From}}: {{Body}}</Message>
</Response>

Name it something amazing, then create the TwiML App with the big red ‘Create’ button on the bottom of the box.

Serverless TwiML Bin function creation

Now, back to the Phone Number Console. Click the number you recently purchased, and scroll down to the ‘Messaging’ Section.

In ‘A Message Comes In’ you’ll see two menus. On the left, you need to pick the type of ‘handler’ for events on this number; select the menu option ‘TwiML’. On the right, you need to pick the specific TwiML Bin – direct it to the function you just saved. Here’s how it might look:

Save it and….. just kidding – that’s all, folks!

Text the Twilio purchased number now with some sort of uncomfortable message such as “Where’s the spaghetti steamer I bought you for your birthday?”. Your TwiML Bin will instantly spring into action and forward the text over to your cell phone, with the sender being none the wiser.

(Why did you regift that steamer, anyway?)

Here’s what happened: you wrote TwiML to tell Twilio to Message your cell phone (using ‘To’). The ‘{{}}’ mustache templates informed the TwiML Bin to automatically add the ‘From’ number and the complete message body.

Building a Cloud-Based Voicemail System in a TwiML Bin

Voicemail in the cloud? That’s something you could use –  right?

With a Twilio number you choose and which isn’t directly linked to your cell phone, you’ll wonder why you didn’t build this years ago. Let’s rectify that situation right away using a two-part process: defining a success Action, and writing some TwiML to handle incoming calls.

Defining Success… at Least for Voicemails

Click here to return to the TwiML Bin console. Again, click the red plus (‘+’) button to create a new bin, then paste this code into the Bin:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>Nice message. I will be in touch.</Say>
</Response>

Name this bin ‘Voicemail Success’ then hit ‘Create’. Instead of surfing away, you’ll need to copy one thing from the resulting success screen:

TwiML Bin function path

Paste the Callback URL in the ‘URL’ field somewhere safe (like a text document), then return to the TwiML Bin Console.

Set Up Your Cloud Voicemail Prompt

Create a new TwiML Bin by again hitting the plus (‘ ’) button. Paste in the following TwiML, substituting the URL from the last function into the Action attribute:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Please leave a message at the tone and press the star key when done.</Say>
<Record
	action="PASTE_THAT_URL_HERE"
	method="GET"
	maxLength="30"
	finishOnKey="*"
/>
<Say>I didn't hear anything, did you record?</Say>
</Response>

Name your function ‘Cloud Voicemail’ and Save.

Send Those Callers to Voicemail

Return to the Phone Number Console where you purchased those numbers… oh, 14 minutes ago? Go back there now, we’re going to direct your calls to that TwiML Bin.

Follow the same procedure as in the SMS Forwarding example above, except this time select ‘Cloud Voicemail’ inside the field ‘A Call Comes In’.

Setting a callback serverless function

You know what to do next – save it, then give the Twilio Number a call! Leave yourself a beautiful sub-30 second message, then hit the ‘*’ on your phone to lock it in. You’ll hear your success message play… great work.

Reviewing Your Cloud Voicemails

To listen to your recordings, visit the Recording Logs console. Right there on top you’ll see your fresh voicemail. Give it a listen at your leisure!

Online cloud voicemail recordings list with Twilio

Cloud Voicemail, SMS Forwarding, No Code: Check

Pretty incredible, right? You implemented SMS Forwarding and Cloud Voicemail in under 20 minutes without writing a single line of code. Those were, at least a few years ago, nearly unobtainable (at least monetarily) communications features that you can build through Twilio with just a few minutes investment.

If only Seinfeld had voicemail...

Ever count how many of Jerry’s problems would
have been solved with cell phones and voicemail?

All that in mind, we’d love to show you even more! How about turning those recordings into text transcriptions? Yes, Twilio can transcribe your voicemails. And that’s just a start – dig into our Docs for even more features you can implement without writing any code or purchasing any servers.

We can’t wait to see what you build – make sure you tell us how quickly you did it, okay? You can find me at pkamp@twilio.com and I’m very excited to see what you can create with TwiML Bins.