I am new to APIs and I want to create a Telegram-Bot with JavaScript that sends notifications from an HTML form.
I am using the fetch command with the API this way:
var TOKEN = "";
var chatID = "";
var api = "https://api.telegram.org/bot" + TOKEN + "/sendMessage?chat_id="+chatID+"&text=";
// getting the message from the form
var message = document.getElementById('message').replace(/ /g,"%20");
// call fetch
fetch(api + "New%20Message:%20" + message);
The thing is that this only works on a message that is on one line. In case the message returns to the line, it creates weird text:
"Hello
World!"
becomes "HelloWorld!". Is there any way to make the API call with a message that goes back to lines within one Fetch call?
I want to get this output:
"New Message: Hello
World!"
without breaking the fetch method.
After some time I was able to fix this.
It just has to do with the ASCII coding. "%20" is a space, for a carriage return, which is the return to line is "%0D". This works just fine for the API.
message = (string Message).replace(/ /g, '%20').split('\n').join('%0A');
fetch(api + "New%20Message:%20" + message);
That way You can get both the spaces and the return to lines from the call.