1

Trying to replace my workplace's Twilio implementation with a temporary solution while we move from one platform to another, and I thought Webtask would be a great solution. I was very happy to see that there was a default Twilio template for webtask and thought I could get straight to work but for some reason, I can't get the template to work even with it's default functionality.

I'm worried that I'm not posting what's expected to the webtask. Here's the default code that sits on my webtask:

'use latest';
import twilio from 'twilio';
module.exports = (context, cb) => {
  // POST a json object with at least the following properties.
  const { body, to_number, from_number } = context.data;
  const { TWILIO_SID, TWILIO_AUTH_TOKEN } = context.secrets;
  
  var client = new twilio.RestClient(TWILIO_SID, TWILIO_AUTH_TOKEN);
  client.messages.create({
    body,
    to_number,
    from_number
  }, (err, message) => {
    message = message + " Goodbye World!";
    if (err) return cb(err);
    cb(null, message);
  });
};

And here's what my Postman looks like (with my Twilio phone number and personal phone number supplied to from_number and to_number in the actual code obv). Headers: postman-headers Body: postman-body

I've stored my Auth token and SID in the secrets area. When I make this post, I receive:

{
    "code": 400,
    "error": "Script returned an error.",
    "details": {
        "status": 400,
        "message": "A 'From' phone number is required.",
        "code": 21603,
        "moreInfo": "https://www.twilio.com/docs/errors/21603"
    },
    "message": "A 'From' phone number is required."
}

I've tried several things, like setting the from_number in the code like this const from_number = "+19999999999" except with a real number. Even so, I get the same 400 response (I also tried adding the SID as a username and the Auth Token as a password, as mentioned in this answer -- same result). I really want to get webtask working, if at all possible.

K. Rhoda
  • 181
  • 1
  • 15

1 Answers1

1

Twilio developer evangelist here.

When you're making the call to Twilio to send the message you are using the wrong attribute names. Instead of

client.messages.create({
    body,
    to_number,
    from_number
  }, (err, message) => { //... } );

it should be:

client.messages.create({
    body,
    to: to_number,
    from: from_number
  }, (err, message) => { //... } );

Twilio expects the attributes to be called to and from not to_number and from_number.

Let me know if this helps.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • This definitely did help. I ended up not using webtask, but this was a problem that would have persisted either way. Thanks! – K. Rhoda Aug 08 '17 at 15:21
  • Glad you got it all sorted! For the future, if you need a standalone solution for hosting an endpoint that does this kind of work, check out [Twilio Functions](http://twilio.com/functions) too. – philnash Aug 09 '17 at 00:15
  • That might be exactly what I need, thank you for the link. I sincerely appreciate it. – K. Rhoda Aug 09 '17 at 15:59
  • No problems! Just let me know if you have any questions at all about it! – philnash Aug 10 '17 at 10:25