1

I'm starting to integrate Twilio into our service. Is it possible to take an incoming SMS and routed between 2 (possibly more) HTTP URLs based on data from the SMS (From, Message, etc)?

I see this happening with Functions, but I couldn't find an example of a Function that takes an incoming SMS and using data from the SMS, the chooses between multiple HTTP URLs/Endpoints to forward the original SMS without modifying it.

So far I've got the following Twilio function:

exports.handler = function(context, event, callback) { 
const response = new Twilio.Response();
let message = event.body
if(PhoneNumberInTestCloud(event.From, context))
  {
    console.log("data content: " + response)
    console.log("forwarded to: " + context.TestCloudURL)
    response.appendHeader('Location', context.TestCloudURL);
    callback(null, response);
  }
else
  {
    console.log("data content: " + response)
    console.log("forwarded to: " + context.MATestCloudURL)
    response.appendHeader('Location', context.MATestCloudURL);
    callback(null, response);
  }
};

function PhoneNumberInTestCloud(phoneNumber, context) { 
if(phoneNumber == context.DevPhone1)
  {
    return true;
  }
else if (phoneNumber == context.QAPhone)
  {
    return true;
  }
return false;
}

Note that TestCloudURL, MATestCloudURL, DevPhone1, and QAPhone are Environmental Variables.

Thanks for your help.

C.Islas
  • 59
  • 7
  • We'll need some initial code from you as a starting point – gregnr Sep 19 '17 at 19:07
  • @gregnr I've updated my question with the simple code that I have. Basically what I want to achieve is that if an SMS sender is a known number I want to "forward" the SMS to one Server that can already handle an incoming Twilio SMS. If the SMS is from an unknown number, have a different server handle the SMS. – C.Islas Sep 19 '17 at 21:15
  • 1
    @AlexBaban He wants to forward the SMS to another HTTP endpoint – gregnr Sep 19 '17 at 21:26
  • @C.Islas You will basically need to mimic the HTTP request Twilio sent to your original server as a new HTTP request to your upstream server. Look into the [`request`](https://github.com/request/request) module. – gregnr Sep 19 '17 at 21:26
  • Hey, I'm a developer evangelist for Twilio and would love to help. Thanks for sharing your code, that's helpful. Can you tell me what's going wrong with that Function though? Are you getting the response you expect? Is there an error? – philnash Sep 27 '17 at 10:13

0 Answers0