3
votes

I would like to forward all incoming messages to some other number with the number to which I got the reply on Twilio,

For example: I have a Twilio number say "+14444444444" to which I got the reply from number say: '+15555555555' and I would like to forward all messages to number '+19999999999'. So I would like to forward all messages on number '+19999999999' which should be received from '+15555555555', not from '+14444444444', Will anyone please let us know if there is an API in Twilio that can do a trick using nodeJs.

Note: The SMS forwarding number can be dynamic and in that case, we cannot use Twiml, if we can use Twiml so please let us know how to set dynamic forwarding number.

Also got the following link that says how to forward SMS but is not relevant to approach we are trying to accomplish using nodeJS: https://support.twilio.com/hc/en-us/articles/223134287-Forwarding-SMS-messages-to-another-phone-number

Thanks, any help will be appreciated.

1

1 Answers

2
votes

Updated Answer

Thanks, @Alex and @Piyush for clarifying the question:

Really sorry about that! Thanks for clarifying. If I understand you correctly now, you want to forward a message to another number, but preservice the original number from the message. Unfortunately, there's not a way to do this. You can forward the message and include the original sender in the message body, but there's no way to replace the actual sender as the original.

Let me know if I understood that correctly this time and if there's anything else I can help with.

Old Answer (Message forwarding with own number)

You can use TwiML dynamically when using our helper libraries, so that should be something you can setup using Node. When your webhook sends your message to your Node application, you can check the body of the message, and make a conditional SMS request or conditionally point to different TwiML based on the content of the body. Here's an example of how to setup a conditional reply for your incoming messages based on the message body in Node:

https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-node-js

While this example is for replying to messages, it shows you the principles of how conditional TwiML can work.

You would just have add a "to" number you want to forward the message to in the message request.

Below is the example of conditional forward.

const http = require('http');
const express = require('express');
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));

app.post('/', (req, res) => {
  const twiml = new MessagingResponse();


  twiml.to = "+1234567890 // Your number here.


  if (req.body.Body == 'hello') {
    twiml.message('Hi!');
  } else if (req.body.Body == 'bye') {
    twiml.message('Goodbye');
  } else {
    twiml.message(
      'No Body param match, Twilio sends this in the request to your server.'
    );
  }

  res.writeHead(200, { 'Content-Type': 'text/xml' });
  res.end(twiml.toString());
});

http.createServer(app).listen(1337, () => {
  console.log('Express server listening on port 1337');
});

Let me know if that helps and if you have anymore questions on how to set this up.