0

Happy Monday,

I'm building a very simple app with Meteor and Twilio (to send SMS) using TwiML.

However, I'm stuck: I don't know how to send a response with newline (kind of html <br>). Below my simple function :

function xyz(message) {
    response.setHeader('Content-Type', 'application/xml');
    response.statusCode = 200;
    var toSend = "<Response><Message>" + message + "</Message></Response>";
    response.end(toSend);}

toSend is thus:

<Response>
   <Message>The quick brown fox jumps over the lazy dog. Portez ce vieux whisky au juge blond qui fume</Message>
</Response>

Whereas I'd like to get (for example) :

<Response>
   <Message>The quick brown fox jumps over the lazy dog.

    Portez ce vieux whisky au juge blond qui fume
   </Message>
</Response>

And I can't use \n, or [CDATA] to use <br>.

I tried to use XMLbuilder (https://atmospherejs.com/meteor/xmlbuilder). But I didn't succeed in making it work..

Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
charnould
  • 2,480
  • 3
  • 19
  • 23

1 Answers1

1

Twilio developer evangelist here.

I have just done a small test with an Express application, check out the following function:

router.post("/twilio/messages", function(req, res, next) {
  res.set('Content-Type', 'application/xml');
  var message = "Hello\n\nPhil";
  res.send("<Response><Message>"+message+"</Message></Response>");
});

This returned a message to my phone that looked like:

Hello

Phil

I'm not sure how much ES2015 Meteor supports, but you could also achieve line breaks using a multi line string in JavaScript (note the backticks instead of quotes).

var message = `Hello

Phil`;

Let me know if this helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • But, I think you have been charged twice (2 sms), due to the \n I think... Could you please confirm me that.. I had this problem (but maybe my SMS was longer than the max size for a SMS...). And \n counts for many characters (UTF-16 something maybe)... I will try you method with backticks tonight.. It seems wonderful and Meteor supports ES2015 if I remember well... – charnould Mar 21 '16 at 11:25
  • I was only charged for one SMS, the line breaks do not make for new messages. Remember, the limit for SMS is 160 characters (or 70 characters if you use unicode characters, more info here: https://www.twilio.com/blog/2015/08/common-sms-problems-unicode-twilio.html). – philnash Mar 21 '16 at 11:29
  • Thank you very much. However, how many characters Twilio counts for your message? Say in another way, how many characters '\n' worth? If I understand well: '\n' is not a unicode character, then my SMS limit is 160... Thanks again. – charnould Mar 21 '16 at 12:17
  • A newline character is just one character. If you are worried about charges on your messages, drop me an email at philnash@twilio.com with a few message SIDs for which this happened for and I can take a look. – philnash Mar 21 '16 at 12:19