Forwarding SMS and voice calls to an alternate phone number
Tilde A. Thurium
Posted on April 13, 2020
Balancing privacy and convenience can be tricky. Today I’ll show you how to forward messages and phone calls with Twilio so that 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
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.”
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.
Configure the widget to connect the call to a single number and type your cell phone number in the box.
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.
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}}
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.
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.
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.
Posted on April 13, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.