SMS Forwarding and Responding Using Twilio and JavaScript
Tags: twilio, sms, javascript, node
Published: 2019-03-20 20:07:22

---
cover_image: https://thepracticaldev.s3.amazonaws.com/i/mdivld2mpw9uvybye53p.png
title: SMS Forwarding and Responding Using Twilio and JavaScript
published: true
tags: twilio, sms, javascript, nodejs
canonical_url: https://www.twilio.com/blog/sms-forwarding-and-responding-using-twilio-and-javascript
description: Learn how you can set up a phone number to forward incoming SMS to your actual number and how to reply using JavaScript and Twilio Functions
---

There are a variety of reasons why you sometimes don't want to give out your phone number to people but still want to receive SMS and be able to reply. Maybe you are trying to sell something on the internet, maybe you want to set up a way for people to report code of conduct violations at an event, or maybe you just don't trust the newly met person that asked for your number. In the past I showed how you can quickly [set up a message forwarding with practically no coding using TwiML Bins](https://www.twilio.com/blog/2016/11/set-up-an-sms-forwarding-number-in-5-minutes-with-twilio.html). However, it doesn't let you respond using that number. Let's take a look at how we can change that using Twilio Functions.

## Creating a Twilio Function

Before we get started, make sure you have a Twilio account. Sign up here for free: [www.twilio.com/try-twilio](http://www.twilio.com/try-twilio)

Once you are logged in, head to the [Functions](https://www.twilio.com/console/runtime/functions/manage)[section](https://www.twilio.com/console/runtime/functions/manage) of the Runtime part of the [Twilio Console](https://www.twilio.com/console/runtime/functions/manage). Create a new Twilio Function and choose the "Hello SMS" template.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/D7AfdHsSmoJ_FAxu1YJVET4OKtqdHe328GnRKNrURT7jB_.width-500.png)

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/oOCOOTJbt2EdvsgCH7Dmxf1JJtI-S-uANATB7PpKyU8IdM.width-500.png)

Change the name of the Twilio Function to something that gives you a hint on what it does. I'll name mine "My SMS Forwarder" and give it the path `/forward-sms`.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/mKUKLx3oj0Nk4Mfu677oO2mkxoiHCE7hkVPVY61ep2UL9q.width-500.png)

Right now our Function is just an auto-responder returning "Hello World". In order to actually forward SMS similar to our previous blog post, update the code the following way:

```js
exports.handler = function(context, event, callback) {
    const MY_NUMBER = 'YOUR_NUMBER_HERE';
         
    let twiml = new Twilio.twiml.MessagingResponse();
    twiml.message({ to: MY_NUMBER }, `${event.From}: ${event.Body}`);
    callback(null, twiml);
};
```

Make sure to replace `YOUR_NUMBER_HERE` with the phone number you want the incoming SMS forwarded to.

In Twilio Functions we can access webhook data such as the `From` number or the message `Body` using `event.`. The new `twiml.message` line will now return the TwiML needed to forward an SMS to the specified number, containing the number of the person who sent it and the original message body.

Save the code by clicking the "Save" button. Head to the [Phone Numbers section in the Twilio Console](https://www.twilio.com/console/phone-numbers/incoming) and pick the number that you want to use for SMS forwarding or buy a new one.

Once you are in the configuration screen, scroll to the bottom of the page, and under 'A message comes in' select the value "Function", and then the name of your Function. In my case that's "My SMS Forwarder". ![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/j85CydNoaP664NQ3_0AkcxFApBps6nOe-iVE6IN8jYqEHQ.width-500.png)

Afterwards click "Save" and grab your phone to text any message to it. You should see a reply with your phone number and the message you sent:

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/ewrJdtyy9UnwlQZZKGv4FSnvH6AgYkQMYXicdNs6bk1AxK.width-500.png)

If you want to see if it properly works, ask a friend or colleague to text your Twilio number and you should see their message. Alternatively get a second Twilio number and use the [API Explorer's "Message Create" functionality](https://www.twilio.com/console/runtime/api-explorer/sms/messages/create) to send an SMS to your forwarding number.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/pjn4dO8oHpKWbKIeLhnTPv93_YyDhZR0tK8j8UnRbrCRy6.width-500.png) ![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/8V8C4I-MryInn4P5OJ-zOg7rRSRJIEmlnnlMiXj99Hbf-y.width-500.png)

### Responding to Messages

Now that we have the message forwarding solved, how do we reply to those messages? If you want to respond with your actual number, that's easy, copy the number from the SMS and write them from your phone. If you want to keep on using your masked number it's not quite that easy.

Right now if you reply to any SMS it will trigger the same SMS webhook and basically just echo to you. We'll modify this behavior by adding the following logic to this:

Check if the message came from our own number:

- Came from someone else -> Forward the SMS like we previously did
- Message is from us -> Parse message to receive intended recipient and forward the message

In order to find out the intended recipient, we'll establish a certain pattern that all of our responses have to follow. It will be the same way that we are currently forwarding messages:

`RECIPIENT_NUMBER: MESSAGE_BODY`.

Update your Twilio Function code to apply this logic:

```js
exports.handler = function(context, event, callback) {
    const MY_NUMBER = 'YOUR_NUMBER_HERE';
         
    let twiml = new Twilio.twiml.MessagingResponse();
    if (event.From === MY_NUMBER) {
        const separatorPosition = event.Body.indexOf(':');
        if (separatorPosition < 1) {
            twiml.message('You need to specify a recipient number and a ":" before the message.');
        } else {
            const recipientNumber = event.Body.substr(0, separatorPosition).trim();
            const messageBody = event.Body.substr(separatorPosition + 1).trim();
            twiml.message({ to: recipientNumber }, messageBody);
        }
    } else {
        twiml.message({ to: MY_NUMBER }, `${event.From}: ${event.Body}`);    
    }
    callback(null, twiml);
};
```

Click save and wait until the updated Function is deployed (there will be a green box saying the deployment was successful).

Grab your phone and try to send an SMS to a friend or your other Twilio number. The SMS has to be in the format `+RECEIPIENT_NUMBER: message`.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/avrBABDY2vzPM-vXHPyJ-PwiYxoHrrC9xvFg_M5JMQC2r1.width-500.png)

The other person should receive only the text you sent. If you sent it to another Twilio number, [check your messaging logs](https://www.twilio.com/console/sms/logs) instead.

You can also try sending a text that doesn't fit the format and you should receive a reply that the message is not appropriately formatted.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/Bls-Ek3S-PMS1FascYyTe3xRuCN1nxAQBPOm8C5bTSBg2S.width-500.png)

### Congratulations!

That's it. You are all set up with an SMS forwarding number that you can use whenever you don't want to give out your normal phone number. But this is really just the start. With [Twilio Functions](https://www.twilio.com/functions) you have access to the npm ecosystem. So why not hook up a [Google Spreadsheet](https://www.twilio.com/blog/2017/03/google-spreadsheets-and-javascriptnode-js.html) or the API of your preferred contacts host to look up names instead of having to write down the phone number to respond. Or create a list of blocked phone numbers. Or maybe you have a completely different idea, I would love to hear what you come up with.

Feel free to reach out to me:

- Email: [dkundel@twilio.com](mailto:dkundel@twilio.com)
- Twitter: [@dkundel](https://twitter.com/dkundel?lang=en)
- GitHub: [dkundel](https://github.com/dkundel)
- Web: [dkundel.com](https://dkundel.com/)