How Twilio’s Programmable Voice Saved My Vacation
Time to read: 4 minutes
After booking some train journeys with Deutsche Bahn (the German rail network) for my summer vacation this year, I found I needed to call their reservations desk to change something important about my tickets. Unfortunately the number they provide is a specially-priced Premium Rate number in Germany, and international calls to these are blocked from my phone. Twilio’s Programmable Voice products are perfect for solving this kind of problem. Read on to find out how I did it and how you could do the same.
Step 1: Getting A Local Number
The first thing to do is to get a number which you can call. Twilio’s phone numbers console allows us to buy phone numbers in over a hundred countries so once you have signed up for an account click the “+” button to buy a new number that’s local to you:
Step 2: Forwarding Incoming Calls
After buying a number, you will be taken to the number setup page, where you can configure what Twilio will do in case of an incoming call. Change the configuration for “A Call Comes In” to use a TwiML Bin. TwiML is the markup language that is used to tell Twilio how to behave, and by hosting our TwiML in a TwiML Bin, we don’t need to run any server ourselves.
After selecting “TwiML Bin” from the drop-down menu on the left, click the "+" button on the right to create a new TwiML Bin. In this case we want Twilio to call a different number for us, so the <Dial>
verb is what we want:
The +49 number shown is the Deutsche Bahn number which is blocked from my phone. After clicking “Create” on the TwiML Bin dialog, we are back on the number setup page. Click “Save” on that, and we’re nearly done!
At this point we have a local phone number set up in Twilio to forward calls to the German number for Deutsche Bahn. In most cases that’s everything needed to use Twilio for forwarding calls internationally.
Step 3: Debugging
I smiled to myself with the satisfaction of someone who has saved themselves some time and money, called the number and…. Nothing happened. I was not connected to Deutsche Bahn and I could not rescue my vacation plans. What had gone wrong?
Twilio keeps detailed logs of all events for phone numbers managed on the platform, and so this is the first place to check. Across the top of the phone number setup page there is a link to check the Calls Log:
By changing the drop-down menu to show “Incoming” calls I could see a link to the details of the call I had just made, which had a clear error message and instructions on how to fix it:
Ah ha! This is a “High Risk” number, which means that it costs more than a regular call and I need to explicitly say that I want to be able to call this kind of number by following the link provided:
It makes a lot of sense to have to opt into calling these kinds of numbers. In my case I knew this was right because Deutsche Bahn had included the details of the calling costs in their email. After saving this page I smiled to myself again, dialled my UK Twilio number and… I was connected to Deutsche Bahn!
For thirty seconds.
Then I was disconnected. So I checked the logs again:
The call was marked with a status of “no-answer”. Aw snap! Something else to fix, but in fixing it I learned something new!
Step 4: A Little Sip of SIP
The call to Deutsche Bahn started with a pre-recorded message about how the call might be recorded, and how I might find the answer to my question on their website. The thirty seconds that I was connected was not quite long enough to make it to the end of that message.
In order to explain what problem this causes and how to fix it, I need to talk a little about SIP. SIP is to phone calls what HTTP is to websites: it’s an industry standard protocol for managing real-time communications like voice and video. In fact, SIP and HTTP are remarkably similar.
When you make a phone call, the destination telephony network will probably send back SIP response code 180 to indicate that the destination phone is ringing, and then a SIP code 200 when the call is answered (rather like the HTTP/200 response sent by a web server when a request is successful). Twilio does not consider the call to be “answered” (and doesn’t start charging you) until it has received the SIP 200 response from the destination network, and the <Dial>
verb has a default timeout of 30 seconds before an unanswered call will be marked as “no-answer” and disconnected.
However, it is possible for the destination network to send some media content back before this, which is known as “early media” and indicated by SIP code 183. This can be used to send informational messages without the caller incurring any cost, and in Deutsche Bahn’s case this is exactly what had happened.
Step 5: Being Patient with Early Media
The fix was to set a longer timeout on my <Dial>
. Because I didn’t know the exact duration of the message I just set it to the maximum (600 seconds), by changing the TwiML to read:
Now, I was able to call Deutsche Bahn, hear the message, be kept on hold for a couple of minutes and finally speak to an operator who solved my train booking problems on the spot. All for about the cost of a cup of coffee - it would easily be covered by the credits included with a new trial account.
Vacation Rescued
This is just a quick example of how I was able to use Twilio’s Programmable Voice features for solving a specific problem I had making a phone call. If you have any similar stories of problems you’ve solved using Twilio I’d love to hear about them, so let me know
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.