This application allows you to make a phone call from your browser using Twilio Client.
It also includes a couple other standard features you would expect a browser phone to have:
- Mute and unmute microphone input
- Send DTMF touch tones using the HTML key pad
This tutorial highlights the key bits of code that make this application work. Check out the project README on GitHub to see how to run the code yourself.
This application requires a TwiML application to make a phone call, in this step we'll show you how to create it.
Every TwiML application needs a friendly name (we called ours “Browser Dialer”) and either a Voice URL or a Messaging URL. Since we want our users to make phone calls, we used a URL to our application’s “/voice” route.
Note: When running the app locally, you can use a tool like ngrok to get a publicly accessible URL for your development environment.
You've seen how to create the TwiML application. Next, let's learn how to generate a capability token so that our clients can make outbound calls.
Before our users can make any calls in their browsers, we need to create a capability token for them.
Capability tokens are your way to control exactly what your users can and can't do with Twilio Client. In this case, our server will provide all users with tokens that allow them to make outbound phone calls.
The Twilio.js client needs credentials to be able to initiate calls as well as send messages, among other cool features. The Twilio.js client requests the token from the
/token route, at which point the client is initialized and ready to start using Twilio API's. Next, let's set up the Twilio Device in the browser.
In our client-side code, we start by including the Twilio.js library.
We then retrieve the capability token from the route /token with a POST request using jQuery.
Lastly, we pass our token to
Twilio.Device.setup() to finish the setup.
callback is used to notify us when the device is ready to make calls.
Setting up the Twilio Device was easy since the
/token route simplifies authorization. Now that the Twilio Client is initialized, let's see how to start an outbound call.
Now that Twilio Client is ready, our users can start making phone calls. They'll start by inputting the phone number they wish to call.
We massage that input before passing the number on to Twilio, adding a + sign, then a country code, and the actual number. This is called the E.164 format and is required by most parts of Twilio's API.
We then use
Twilio.Device.connect to start the call. Twilio will send a request to the URL you specified in your Twilio Application configuration, looking for instructions on how to handle the call.
In this case, we include the phone number the user wishes to dial in our
connect() call, and we then access it in our server-side code here.
As you have seen, connecting and disconnecting from a call is quite simple. Next, we'll enable the client to mute and unmute active calls.
Now you have an idea of how simple it is to use the Twilio Client in the browser. There are much more features to explore. Next, let's allow the user to send DTMF tones, in case they are calling an automated phone system.
If our user calls an automated phone system, they might need to navigate a menu system using our phone's keypad and DTMF tones.
DTMF stands for "Dual-tone multi-frequency signaling" and are the familiar sounds you hear when dialing a phone. DTMF have been standardized so they can be understood and decoded by machines.
To play DTMF tones with the Twilio.js client library we use the
sendDigits method, passing which digit the user pressed as our sole argument.
As you have seen, sending DTMF tones in response to button clicks is straightforward. Lastly our clients may want to hang up a call. Let's see how we can do that next.
That's it! We crafted an application that allows our users to make browser-to-phone calls using Twilio.js.
If you're a node developer working with Twilio, you might also enjoy these tutorials:
Easily route callers to the right people and information with an IVR (interactive voice response) system.
Click-to-call enables your company to convert web traffic into phone calls with the click of a button.
Thanks for checking this tutorial out! If you have any feedback to share with us please contact us on Twitter, we'd love to hear it.