17

Using Java/Scala. I am sending this string (an sms message) to a user mobile number by a Twilio Account.

var body = "Hello from Govind Singh Nagarkoti! Your verification code is " + code

This goes out to the user mobile, in 1 line.

I want a newline after the first sentence. I want the user to receive:

"Hello from Govind Singh Nagarkoti!
 Your verification code is 240190" 

How can I enter a line break?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Govind Singh
  • 15,282
  • 14
  • 72
  • 106

7 Answers7

32

If you are using an official Twilio library, the proper syntax is \n.

In your case, this should work:

var body = "Hello from Govind Singh Nagarkoti! \n Your verification code is " + code
alphaleonis
  • 1,329
  • 11
  • 18
12

Just a tip to anyone else who tries to use \n to take the content to a new line: the "\n" has to be within double quotes; if it's contained within single quotes it will print out as '\n'.

user7291698
  • 1,972
  • 2
  • 15
  • 30
Samar Gurjar
  • 221
  • 4
  • 10
8

Use %0a , it might be helpful.

Reference :http://www.twilio.com/help/faq/sms/how-do-i-add-a-line-break-in-my-sms-message

Amar
  • 955
  • 3
  • 11
  • 28
  • user getting as it is message with %0a – Govind Singh Jun 14 '14 at 10:45
  • As far I understood curl -X is a command line client tool for URL manipulations,it meant making a call to the REST API given with the sets of data.U might try with \n once – Amar Jun 14 '14 at 10:54
3

You can either use \n with single quotes

val body = s"Hello from Govind Singh Nagarkoti!\nYour verification code is $code"

or a new line with triple quotes:

var body = s"""Hello from Govind Singh Nagarkoti! 
Your verification code is $code"""

Note that I also used string interpolation here by prefixing the variable with $ and prefixing the string with s.

jdprc06
  • 360
  • 3
  • 7
2

Make sure your IDE doesn't automatcally add another \ if you copy pasted "\n". This is what tripped me. I pasted "\n" and it got inserted as "\\n" so naturaly the first "\" made the second "\" the special character and resulted in the "n" being treated as a simple "n" character. Hope this helps.

1

\n works , and i was trying for, %0a

var body = "Hello from Govind Singh Nagarkoti!\n Your verification code is " + code
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
1

If anyone is struggling with this in Node.js specifically maybe this will help.

I tried all of the above and nothing worked. In my case the notification.msgText was coming from a database and the \n were in the string returned from the db. Nothing I tried worked, until I did the following. I used the | as a placeholder in the database, and then replaced it on the call to twillio. Hopefully this might save you from pulling out a hair or two.

const rsp = await twilioClient.messages
    .create({
        body: notification.msgText.replace(/\|/g, "\n"),
        from: twilioFromNum,
        to: `+1${notification.cellPhone}`
});
Dan Harris
  • 11
  • 1