Forwarding SMS and voice calls to an alternate phone number

April 09, 2020
Written by

Forwarding SMS and voice calls to an alternate phone number

Balancing privacy and convenience can be tricky. Today I’ll show you one way to forward messages and phone calls with Twilio using Twilio Studio, so you can give out a phone number where people can reach you, while keeping your real phone number a secret.

There are lots of reasons you might want to hide your real phone number. For example:

  • Transacting with internet strangers such as buying/selling on Craigslist
  • Giving delivery folks a number to reach you
  • Putting a phone number on your business card

Editor: this article shows one way to forward calls using Twilio, using Twilio Studio. Depending on your use case you might prefer to use another method:

  • Using a TwiML Bin
  • Using Twilio Functions
  • Using a custom Twilio Webhook
  • Using functionality from a Twilio partner

For an overview of these options, see this post.

Prerequisites to create a forwarding number

  • A Twilio account - sign up for a free one here
  • A Twilio phone number with SMS and voice capabilities
  • A phone number you want to forward calls and messages to. For the rest of this post, we’ll call it your cell phone number to simplify things but it can be any number with SMS and voice capabilities.
  • A friend with a SMS-enabled phone number to help you test things out

Forwarding phone calls with Twilio Studio

Twilio Studio is a visual application builder that allows you to write conditional logic for handling messaging flows without writing any code.

Head to the Studio Dashboard. Click the “+” button to create a new Studio Flow starting from scratch. Let’s call it “message forwarding.”

Screenshot of the "New Flow" dialog box in Twilio Studio. The "Flow Name" input box has the text "message forwarding".

From the Widget Library on the right-hand side, drag a “Connect Call To” widget on to the canvas. Connect it to the Incoming Call trigger.

Screenshot of Twilio Studio flow. A "Connect Call To" widget is connected to the "Incoming Call" trigger.

Configure the widget to connect the call to a single number and type your cell phone number in the box.

Screenshot of "Connect Call To" widget configuration. The "Connect Call To" dropdown has "Single Number" selected, and the input box below contains a fake cell phone number.

Save the widget configuration, and then click Publish at the top.

Go to the Phone Numbers dashboard. Under “Voice & Fax”, configure your Twilio phone number to use the message forwarding Studio Flow you’ve just created.

Screenshot of Twilio phone number configuration. Under "Voice & Fax", the "Configure With" dropdown has "Webhooks, TwiML Bins, Functions, Studio, or Proxy." Under "A Call Comes In," "Studio Flow" and "message forwarding" are selected.

Grab a friend. Ask them to call your Twilio phone number to test things out. ☎️ 🎉

One-way SMS forwarding with Twilio Studio

There are a few ways of doing one-way SMS forwarding with Twilio. Since we’ve already got a Studio Flow going, let’s build on top of that.

Drag a Send Message widget onto the canvas. Connect it with the Incoming Message trigger.

Configure the widget so that it sends a message to your cell phone number, from your Twilio phone number. The message body should be:

From: {{trigger.message.Body}} : {{widgets.send_message_1.inbound.Body}} 

Screenshot of configuration for Twilio Studio "Send Message" widget. Send Message From has a fake Twilio number in the input box, and "Send Message To" has a fake cell phone number in the input box.
Screenshot of Twilio Studio flow. The "Send Message" widget is connected to the "Incoming Message" trigger. In the "Send Message" widget configuration, the Message Body is "from {{trigger.message.Body}

Don’t forget to publish the changes to your Flow. Ask your friend to help you test by sending a text message to your Twilio number.

Two-way SMS forwarding with Twilio Functions

Let’s say you want to be able to reply to incoming messages from your cell phone. Using Twilio Functions, we’ll write some code to accomplish that.

In order to run this Function, you need 3 pieces of information:

  • Your cell phone number
  • The third-party phone number you want to exchange messages with
  • The Twilio phone number serving as the gateway in the middle

We’ll reconfigure the same Twilio number we were using with Studio earlier to call the Twilio Function whenever a new SMS comes in. Inside the Function, there’s some logic to determine which direction to forward the information.

A hand-drawn diagram. On the left, a pink anthrophomorphic cell phone labeled "Your Cell Phone." In the middle, a smiling blue circle labeled "Twilio Number." Underneath that, a blue box labeled "Twilio Function." On the right, a purple anthrophomorphic cell phone labeled "3rd Party." There are arrows flowing from Your Cell Phone to/from Twilio Number, from Twilio Number to/from Twilio Function, and from Twilio Number to/from 3rd Party.

How do you get the third-party phone number? You can leave the Studio Flow we created earlier hooked up. When a message comes in you want to reply to, copy the phone number out of the message body and drop it into the code below.

On the Functions dashboard, create a new Function called forward SMS. Copy the following code into it:

 exports.handler = function (context, event, callback) {
    const yourPhoneNumber = ''; // replace this
    const thirdPartyPhoneNumber = ''; // replace this
    const twilioPhoneNumber = event.To;

    let twiml = new Twilio.twiml.MessagingResponse();
    if (event.From === yourPhoneNumber) {
      twiml.message(event.Body, {
        to: thirdPartyPhoneNumber,
        from: twilioPhoneNumber
      });
    } else {
      twiml.message(`${event.From}: ${event.Body}`, {
        to: yourPhoneNumber,
        from: twilioPhoneNumber
      });
    }
    callback(null, twiml);
  }

Save your Function -- it’ll deploy automatically. Configure your Twilio phone number so that this Function is called when an incoming message arrives.

Screenshot of Twilio phone number configuration. Under "Messaging", the "Configure With" dropdown has "Webhooks, TwiML Bins, Functions, Studio, or Proxy" selected. Under "A Message Comes In," "Function" and "forward SMS" are selected.

Ask your friend to send you a SMS. Send them one back, and verify that they received it. Offer to buy them a beverage of their choice to say thanks for the help. ☕

One drawback of this approach is that you’re limited to one conversation at a time. Since the 3rd party phone number is hard coded, it needs to be changed each time you want to converse with a different person. If you’re a high roller who needs to juggle multiple simultaneous conversations with strangers, you could add some conditional logic to specify the number you want to send a message to.

Wrapping it up

In this blog post you’ve learned a few ways of forwarding messages using Twilio Studio and Functions. Go forth and distribute your phone number to the world, more safely.

If you have any questions or issues, feel free to send me a SMS at +1 970-238-3818. You can also reach me on Twitter at @annthurium.

Editor: for an overview of other ways to forward calls on Twilio, please see this post.