skip to Main Content

I have a form that sends a text to the Telegram robot via Ajax.
But the problem is that if the text is written in several lines, when the text is sent, the text is sent in one line,
The code I wrote is as follows:

$.ajax({
    type: "POST",
    dataType: "text",
    data: 'sendmessage=' + token + '&chat_id=' + lines[i] + '&text=' + document.getElementById("text").value,
    url: "https://ss.ir/apps/tele/aa.php",
    success: function(result) {
      document.getElementById("loading").style.display = 'none';
      var res = JSON.parse(result);
      if (res.ok == true) {
}}

Does anyone know what the solution to this problem is?

2

Answers


  1. First of all, I don’t recommend saving the token in the client code, just send a request with the essential data to your php file and from there do whatever you need to do, that’s how it might work:

    $.ajax({
                    type        : 'POST',
                    url         : 'https://ss.ir/apps/tele/aa.php', 
                    data        : {chat_id: lines[i], text: document.getElementById("text").value},
                    dataType    : 'json',
                    encode      : true
                    success     : function(result) {
      document.getElementById("loading").style.display = 'none';
      var res = JSON.parse(result);
      if (res.ok == true) {
                })
    

    And in your php code you must only get the text and the chat id ($_POST["text"]) and send the message (I recommend using a library or curl to send the request to telegram). You can use markdown or html to wrap see here, and you must specify the "parse_mode" when sending the message see here (if your text contains "<br>" tags use the HTML parse mode and it should work)

    Login or Signup to reply.
  2. I believe the line breaks are being lost on the other (PHP) side.

    The PHP side is supposed to replace the new-line characters that it receives with <br>, as shown here. Otherwise, the line breaks are lost.

    On your side, you can double-check what line-ending character(s) are being sent with your text, and maybe experiment with substituting, for example, CR/LR with just LF (if CR/LF is what you are sending)? Note that this would involve pre-processing the text before adding it to the ajax call:

    let ttxt = document.getElementById("text").value;
    ttxt = ttxt.replace(/rn/g, 'n');
    
    $.ajax({
        type: "POST",
        dataType: "text",
        data: 'sendmessage=' + token + '&chat_id=' + lines[i] + '&text=' + ttxt,
        url: "https://ss.ir/apps/tele/aa.php",
        success: function(result) {
          document.getElementById("loading").style.display = 'none';
          var res = JSON.parse(result);
          if (res.ok == true) {
    }}
    

    Everything depends on what you are sending, and on what Telegram expects to receive (and whether they added code to preserve line breaks, which they might not have done).

    You can always contact Telegram support and ask them how to resolve – they wrote their api, they should be able/willing to assist.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search