Rate this page:

Route Leads with Twilio Studio

Route inbound sales leads to the right person with Twilio Studio + Twilio Functions. Collect inputs from your users and transform them into instant connections to the right representatives. In this tutorial, we’ll build a Studio flow that asks the user for the number of employees at their company, sends that number to a Function to fetch the correct representative’s contact information, then makes the call to that rep.

We’ll start with a fresh Twilio Studio flow; log into your Twilio account and navigate to the Studio Dashboard, then tap the + icon to create a new flow. You can name your flow anything you like; we’re calling ours Lead Routing.

Lead Routing Flow

You’ll notice that the canvas comes with a widget already in place -- that’s the Trigger widget, which kicks off our flow when the trigger we specify is fired. In this case, our trigger is going to be an Incoming Call.

Incoming Call Trigger

We’ll start with a Gather Input on Call widget -- drag this onto the canvas and connect it to the Incoming Call trigger by dragging the dot to the upper left corner of the new widget. Here, we’ll prompt the user for the number of employees in their company, so we can route their call to the correct rep. Type your question into the Text To Say field and select a voice to read your message.

Gather Input on Call - Get Company Size

Callers can respond to this in two ways -- pressing a key or saying a number. Let’s start with the voice input. Since it’s possible that someone might say something that isn’t a number, we’re going to use a Split Based On... widget to check the value of what the user said. Drag the Split widget onto the canvas and set the Variable To Test to the SpeechResult from your Gather widget -- in this case, it’s {{get_company_size.SpeechResult}}.

The condition match we’ll be checking for answers the question “is this a number?” so we’ll use a regex to check this. Create a new Transition, select Regex from the dropdown, and enter ^[0-9]*$ in the next field.

Split widget on speech result

First, we’ll handle the “not a number” case -- drag a new Say/Play widget onto the canvas and connect it to the No Condition Matches result from the Split widget. Enter the message of your choosing (we’re going with a gentle reminder that we’re looking for a number), and then connect the Audio Complete dot back to the Gather widget from earlier, so the user is re-prompted to say or type a number.

Not a number case - Split widget

The next step is to handle things in the happy case -- when our user says a valid number. In order to route the call to the rep, we’ll need to use some logic to determine which rep should take the call, and then pass the rep’s phone number to our flow. We’ll do this with a simple Twilio Function.

Navigate to the Functions section of the Twilio Console and create a new Function. We’ll call ours Lead Distribution and set a path that matches. Paste the following code into your new Function, modifying the result numbers and size criteria to fit your organization’s needs.

exports.handler = (context, event, callback) => {
    const size = event.size;
    let result = "";

    switch (true) {
        case (size < 50):
            result = '555-867-5310';
        case (size < 100):
            result = '555-867-5311';
        case (size < 1000):
            result = '555-867-5312';
        case (size > 1000):
            result = '555-867-5313';
            result = '555-867-5309';
    callback(null, result);

Your Function should look somewhat like this:

Lead routing function

Click Save to save and deploy your Function. Let’s go back to the Flow and connect this to our SpeechResult.

Drag a new Run Function widget onto the canvas, and connect it to the dangling regex dot from the Split widget. We’ll need to pass one parameter to our Function -- the size that we gathered from the SpeechResult. Click Add in the Function Parameters section of the right sidebar, and set the key to size and the value to the speech result -- in this sample, it’s {{widgets.get_company_size.SpeechResult}}. Save your new parameter.

Run function widget

Now it’s time to connect the call to the rep. Our Function is going to return a phone number for the correct rep, and we can use the Connect Call To widget to transfer the current call to that phone number. Drag the Success dot from the Run Function widget to the new widget to connect them.

Choose a Single Number from the dropdown, and set the next field to the result of your Function widget -- in this case, {{widgets.match_speech_to_rep.body}}. When the Function returns a phone number, this widget will then open up a call to that number, connecting the original caller to the most appropriate rep for their company size.

Function widget to Connect Call widget

Now, let’s handle keypress inputs! It’s going to work mostly the same way as voice input, but since we know the user is going to be typing numbers, we don’t need the Split widget, and can go directly to the Function call. Drag another Run Function and Connect Call To widget onto the canvas, this time setting the size parameter to the keypress result from your Gather widget -- in our case, that’s {{widgets.get_company_size.Digits}}.

Handle digit entry with functions widget

Your final canvas should look something like this:

Final canvas - lead routing

Time to connect the flow to a Twilio phone number. Navigate to the Phone Numbers section of the Twilio Console, and select the number you’d like to forward from. Scroll down to the Voice & Fax section, and select Webhooks, TwiML Bins, Functions, Studio, or Proxy from the first dropdown. Select Studio Flow next to the “A Call Comes In” dropdown, and choose the flow that you just created to attach it to the number. Click Save.

Set lead routing flow to a Twilio number

You’re ready to test out your flow! Give your Twilio number a call, follow the prompt to say or enter a number, then get routed to the rep that matches the company size you stated.

Ready to take things to the next level? Try modifying the Function further -- accept additional inputs beyond numbers, return more information, or try this routing flow with SMS!

Sarah Stringer Shawn Stern
Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.


        Thank you for your feedback!

        Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more