Ian Bradbury
Ian Bradbury

Reputation: 1475

Sending Twilio SMS with an Alphanumeric "From" returns the error message 'From' phone number is required

I am attempting to send SMS messages where I use a custom and human friendly "From" name.

I am able to send SMS fine - until I set the 'From' attribute to anything other than one of my valid Twilio numbers.

I have been testing this using a UK Twilio number sending to a UK mobile phone number, which Twilio list as a country capable of using alphanumeric from addresses.

Background : I am sending these SMS from Parse cloud code.

Here's my send code (Javascript).

function sendCodeSms(phoneNumber, code, prefix) {

// Define a human friendly 'from' address
var twilioFrom = 'Friendly';

// Use appropriate 'from' number - cover countries that don't allow alphanumerical from addresses
switch (prefix) {
    case "1":
        twilioFrom = twilioPhoneNumber_1;
        break;
}

// Create a promise
var promise = new Parse.Promise();

// Call the Twilio API
twilio.sendSms({
    to: prefix + phoneNumber.replace(/\D/g, ''),
    from: twilioFrom.replace(/\D/g, ''),
    body: 'Your login PIN is ' + code
}, function(err, responseData) {
    if (err) {
        console.log(err);
        promise.reject(err.message);
    } else {
        promise.resolve();
    }
});
return promise;

}

If I replace the twilioFrom value with one of my valid Twilit numbers then the SMS are sent fine.

Any ideas? Help?

(Please don't let it be a school boy error)

Upvotes: 0

Views: 972

Answers (3)

philnash
philnash

Reputation: 73027

Twilio developer evangelist here.

Apologies that the Twilio module is currently out of date on Parse. I am working with their team to get it up to date.

In the meantime, you don't need to write an entire module to use this functionality. Parse Cloud does supply a simple httpRequest module which you can use to send requests to the newer Twilio endpoints. Here is an example of a function to send an SMS message using the Messages endpoint:

var accountSid = 'AC123...'; // your account SID
var authToken  = 'xyzabc...'; // your auth token
var toNumber   = "+14515551234"; // the number you are sending a message to
var fromNumber = "HELLO"; // your alphanumeric string
var body       = "This is a message"; // your message to be sent

// Build up the URL
var url = 'https://';
url += accountSid + ':' + authToken + '@'; // add auth to URL
url += 'api.twilio.com/2010-04-01/Accounts/';
url += accountSid;
url += '/Messages.json';

Parse.Cloud.httpRequest({
  url: url,
  method: "POST",
  body: {
    "To": toNumber,
    "From": fromNumber,
    "Body": body
  }
}).then(function(httpResponse) {
  // success
  console.log(httpResponse.text);
  // httpResponse.data is the parsed JSON response
},function(httpResponse) {
  // error
  console.error('Request failed with response code ' + httpResponse.status);
});

Let me know if this helps at all.

Upvotes: 1

Ian Bradbury
Ian Bradbury

Reputation: 1475

So the answer is (as found by dmullings) that the Twilio library provided by Parse only provides a subset of the Twilio capabilities.

The built-in Twilio Cloud Module provides a subset of the functionality available to Twilio customers. If the feature you are interested in is not supported by this Cloud Module, you may want to consider writing your own Twilio JavaScript wrapper as a custom Cloud Module. We suggest looking at Twilio's node nodule for inspiration.

So the solution will be to create your own Twilio module.

Upvotes: 0

dmullings
dmullings

Reputation: 7200

It looks like you are removing all non numerical characters with .replace. So I think the from sent to twilio will be a blank string.

// Call the Twilio API
twilio.sendSms({
    to: prefix + phoneNumber.replace(/\D/g, ''),
    from: twilioFrom.replace(/\D/g, ''), // <--- Removing all non digit characters
    body: 'Your login PIN is ' + code
}, function(err, responseData) {
    if (err) {
        console.log(err);
        promise.reject(err.message);
    } else {
        promise.resolve();
    }
});

Upvotes: 1

Related Questions