Forwarding Messages from Twilio

In order to better help our users at Frontdoor, I implemented message forwarding from our Twilio phone number to my cellphone. This allows me to quickly jump on questions from users or landlords when they get an automated message from our system. These automated messages range from a simple authentication code to visit reminders or follow-ups. Despite the fact that it's an automated messages, some users like to send us questions by SMS so I have to make sure that I provide answers fairly quickly.

This article will cover two different ways of implementing message forwarding with Twilio.

textingStock-1

The Simple and Easy Way

If you simply need to forward messages without processing it or running logic on it, then this approach is ideal. It requires no server-side setup and the configuration is entirely done on Twilio's website, which makes it super easy and quick to get going.

1- Log in to Twilio's website and head over to the Phone Number section in the console. Click on the phone number you want to implement message forwarding for. This should take you to the Manage page for that phone number.

2- Follow this gif to configure message forwarding to your phone:
twilioForwardSms

Code:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Message to="+15141112222">
    New message from {{From}}: {{Body}}
  </Message>
</Response>

That's all there is. It's as simple as that!

The DIY Way

The Do it Yourself (DIY) way allows you to process the message and apply any logic you wish to it before forwarding it. It involves some server-side setup since you need to create an endpoint for Twilio to call when a message is received. The endpoint will be used as a webhook by Twilio, and here's what you need to implement the endpoint using Node.js and Express:

1- Define a POST endpoint:
app.post('/api/v1/twilio/forward', communicationController.twilioForwardSms);

2- Get the sender and message from the body, and return a response to Twilio. You should always return a response to Twilio in order to successfully close the loop for the webhook:

function twilioForwardSms(request, response) {
    var from = request.body.From;
    var message = request.body.Body;
    // We received the message, we can return a success
    response.setHeader('Content-Type', 'application/xml');
    response.end('<Response></Response>');
    // We can now handle the message ourselves
    communicationModule.twilioForwardSms('+15149662632', from, message);
}

3- Process the message and then forward it:

function twilioForwardSms(forwardTo, forwardFrom, body) {
    // Apply any logic here
    // Forward the message using the Twilio module
    twilio.sendMessage({
        to: forwardTo,
        from: twilioPhoneNumber,
        body: body += '\n\nFrom: ' + forwardFrom
    }, function (err, responseData) {
        // Apply more logic here if needed
        if (err) {
            logger.error('Failed to forward message from %s to %s: %s',
                forwardFrom,
                forwardTo,
                err.message);
        }
        else {
            logger.info('Successfully forwarded message from %s to %s. Message SID: %s, DateCreated: %s',
                forwardFrom,
                forwardTo,
                responseData.sid,
                responseData.dateCreated);
        }
    });
}

This approach requires a little more time but is not too complicated. It allows you to apply logic before forwarding the message as opposed to the first approach.

Also, I recommend using both methods together since you can setup a failover on Twilio: PRIMARY HANDLER FAILS. If the first method fails, it will try the second method, which is great if you want to make sure you get all the messages.

Bonus: Forwarding Voice Calls

You can easily forward voice calls by using the first approach seen above:
Forward Voice Calls

Code:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial>5141112222</Dial>
</Response>

I hope this helps you, and please do not hesitate to leave comments below. I'd love to hear from your experience with Twilio and how you implemented message or call forwarding.

Thanks to: Twilio for the TwiML inspiration

Wissam Abirached

Wissam Abirached