Wish your users could get in touch as easily as they can surf? It's your lucky day!
Let's go over the step s necessary to implement click-to-call in a Ruby on Rails application.
- A website visitor submits a web form with a phone number.
- Your web application receives the submission and initiates an HTTP request to Twilio asking to initiate an outbound call.
- Twilio receives the request and initiates a call to the user's phone number.
- The user picks up the call.
- After the call connects, we provide TwiML instructions to connect the user to our sales or support teams.
Let's get started! Click the button below to move to the next step of the tutorial.
Before we create our click-to-call application, we need to set up our environment first.
Let's put our Twilio credentials in a place where our application can access them. For this tutorial, we'll store them in environment variables that our application can read.
# Find your Account SID and Auth Token at twilio.com/console export TWILIO_ACCOUNT_SID=AC2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX export TWILIO_AUTH_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX export API_HOST=https://example.herokuapp.com export TWILIO_NUMBER=+15551230987
Twilio_Number placeholder values with your unique values, which you can find in the Twilio Console. You can use an existing Twilio phone number or obtain a new number.
Next, let's look at making a friendly web form.
For our solution, we'll need a form where the user can enter a phone number.
No need to overthink this as the real goal is to
POST the user's and sales team's phone numbers to your controller.
So what does this form need?
- An input for the phone number
- Another for the sales team number
- A submit button.
Since the page doesn't need to render new content after clicking on submit, we've decided to implement the
POST action via AJAX using jQuery. Let's take a look at that next.
To make the click to call feature more seamless we used Ajax to send the form asynchronously.
This code shows one way you could implement this functionality using jQuery:
- Watch for the user "submitting" the form element.
- Submit that form data to our controller.
- Let the user know if the submission was successful or not.
This is a pretty common implementation of jQuery's $.ajax() method, but notice that we are returning the response message when the call has connected.
Now that we have the frontend done let's build a controller that will receive this data and call the user. We'll start our exploration in the next step.
First, we initiate a
@client object with our Account SID and Auth Token. This is essentially our Ruby REST API handler, which we could use to send SMS (or a myriad of other things).
In this example, we need it to get access to
account.calls. This is the object that we're going to use to create phone calls.
Next, we'll look at making phone calls.
We'll use the REST API to make an outgoing phone call which requires us to pass a
From number, a
To number and a URL Parameter that tells Twilio what to do after it connects the call to our user.
In this case, Twilio needs to DIAL in the Agent once the call has been placed. We'll discuss this more in future steps.
Next, let's look at the endpoint we'll expose to Twilio.
Twilio makes a request to our application when the call is created using the REST API. This means that we need to create an endpoint that is publicly available for Internet requests: we'll walk through a way to make your localhost accessible via ngrok a little later in this tutorial.
A publicly exposed URL has some drawbacks. Next, we'll look at making sure we don't leak any sensitive data.
TwiML sometimes can contain sensitive user information such as phone numbers. If you're revealing sensitive information at a route, you'll want to make sure you're talking to Twilio.
So, let's check that the request was made by Twilio before doing any operation on the
authenticate_twilio_request method provides a mechanism to confirm that the request your application is receiving is actually coming from Twilio.
Now that we know a request originates from Twilio let's move on and look at the TwiML response we'll send.
TwiML is a set of verbs and nouns written in XML that Twilio reads as instructions.
To make writing TwiML easy, many of the helper libraries have methods that generate TwiML for you. We use twilio-ruby to create a TwiML response that will instruct Twilio to SAY something.
And with that, you've helped us get a working click-to-call form, ready to be integrated into your own application.
Now you can run and test your Twilio app.
However, you probably want to test it using a publicly available endpoint without having to go "public" with your app. The best option is to use ngrok.
Note: For more information about running the application, see the readme file in the app github repository.
ngrok generates a secure URL that forwards traffic to a port, usually 5000, on your localhost server. It allows you to run applications locally but make them a publicly available endpoint via secure tunneling. This allows you to test your application and do all the things you want your app to do but in a secure public space.
ngrok is an executable (
ngrok.exe) that you run on the command line or terminal.
Head over to ngrok's website, download the
ngrok.zip file for your OS of choice, then unzip the file into an easily accessible location.
For example, on Windows, you can place it in your
\Users directory in
\AppData\Roaming\<ngrok-directory-name>\ngrok.exe. Then, update your PATH environment variables to the location of the ngrok executable. On Linux or OSX, it's much easier (see the instructions on the ngrok site).
And that's it.
So, now you are ready to use ngrok.
Before you start ngrok, it might be a good idea to have your Twilio Console open.
For Mac and Linux, open a terminal and run this command to start ngrok:
$ ./ngrok http 4040
On Windows, open a command prompt and run this command to start ngrok:
$ Path-to-ngrok> ngrok http 4040
Alternately, you can start ngrok using the following command:
$ ngrok http 4040 -host-header="localhost:4040"
The port number "4040" is arbitrary. If your local server is running on another port, replace "4040" in the command with the appropriate port number.
This will start ngrok. A running instance of ngrok will appear in the terminal showing the local web interface (in this case http://127.0.0.1:4040) and the public URL (in this case, https://28dd499b.ngrok.io).
The forwarding public URL needs to be set in the Twilio Console. In the Console, select the active phone number you set in the environment configuration file and enter the public URL from ngrok (shown above) in the "A Call Comes In" field under Voice & Fax. Be sure to select Webhook from the dropdown list.
Each time you restart ngrok a new public URL will be generated. You will need to update this URL in the Console any time you restart ngrok.
And that's it. You now have a publicly available endpoint for your app, so now you can take a look at your running app and enter the phone numbers using your ngrok address: https://xxxxxx.ngrok.io
- Check out how iAdvize uses Twilo click-to-call to connect to online shoppers with customer support representatives
- Automated Survey - Instantly collect structured data from your users with a survey conducted over a voice call or SMS text messages.
- SMS and MMS Notifications - Send SMS alerts to a list of system administrators when something goes wrong on your server.
Thanks for checking this tutorial out! Tweet to us @twilio and let us know what you're building!