Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Line breaks are removed calling telegram api

I want to send a message like a draft by calling it’s api for Telegram. I searched and learned that it can be done by calling

`https://t.me/share/url?url=${data}`

I did it with anchor tag as follows:

<a href={`https://t.me/share/url?url=${data}`}>share to telegram</a>

But I have problem when I want to add line break in data string.
I tried ‘\n’ and ‘\r\n’ but apparently it doesn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is this the right way to call telegram api?

If yes how can I separate lines of string like there is a line break between them

enter image description here

But I expected sth like this:

before
after

>Solution :

Try using this encodeURIComponent(data)instead of data in:

<a href={`https://t.me/share/url?url=${encodeURIComponent(data)}`}>share to telegram</a>

You basically need to call that function to, well, encode the data you want to send in a URL/URI as an argument to a post or get request

You can find a lot more info in the documentation here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

As for what exactly is a line break like \n getting encoded as. They get encoded as the '%0A'

Just as some general information, spaces are also invalid in URIs and they get encoded as '%20'

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading