0

Disclaimer I am a newbie at programing and also not really good at english, so please pardon my bad explanation.

My bot had to send a message containing data retrieved from a spreadsheet, but found that the data was so many that it exceeded the character limit of the telegram message (4096 characters).

So I assume that I have to split the message, then send them in sequence. Please help...

here's my code :

function SearchbyProjectName(Prjct){
  var projectNON = GetAllProjectNON(); 
  var project = GetAllProject();
  var num = 1;
  const allLocation = [];

  var header = 
  'xxxheaderxxx';

  var footer =
  'xxxfooterxxx'

  for (var row=0; row<projectNON.length; row++) {
    if(projectNON[row][3]==Prjct){ 
      var resiNON = num++ + '. ) ' + '/d_' + projectNON[row][6] + '\n\n';
      allLocation.push(resiNON);
    }
  }
  for (var row=0; row<project.length; row++) {
    if(project[row][3]==Prjct){ 
      var resi = num++ + '. ) ' + '/d_' + project[row][6] + '\n\n';
      allLocation.push(resi);
    }
  }

  var stringg = allLocation.toString();
  var clearComma = stringg.replaceAll(",", "")
  var newData = header + clearComma + footer;
    
  return newData ;
}

here's the message :

ALL LOCATION Project: PTT2 : Tuesday, 24 Jan 2023 15:10:34

  1. ) /d_XXXXXXXXXXX

  2. ) /d_XXXXXXXXXXXXX

  3. ) /d_XXXX

  4. ) /d_DDDDDD

  5. ) /d_JJJJJJJJJJJJJ

  6. ) /d_XXXXXXX

...

  1. ) /d_BBBBBBBBBBBBB

I wish I could split the message at number 100.)

  • Does this answer your question? [How can I split a string into segments of n characters?](https://stackoverflow.com/questions/6259515/how-can-i-split-a-string-into-segments-of-n-characters) – Nikita Skrebets Jan 24 '23 at 07:21
  • unluckily, that doesn't answer my question. – Angely Fransiska Jan 24 '23 at 08:39
  • since it seems that your message is actually an encoded list where each item is expected to begin in a new line with an index like `#.) ` you are supposed to decode the list parsing your string to have back your list and later re-encode the specific items slices you are interested into. Or just find using a regex where the string hits `'100. )'`. In javascript there's String.split(separator) but I wouldn't bet that far this could work `msg.split('100. )')` – Diego D Jan 24 '23 at 08:48

1 Answers1

1

I'm not able to test this but I think it might solve your problem. For every 100 projects store the string in the array newData. Then in the receiver of newData loop throught the array of strings and send each individually.

Notice I don't push strings to the array allLocation but simply concatinate strings. Once I've reach a multiple of 100 push that concatinated string to the array newData.

function SearchbyProjectName(Prjct){
  var projectNON = GetAllProjectNON(); 
  var project = GetAllProject();
  var num = 1;
  var newData = [];
  var allLocation = "xxxheaderxxx";

  var footer = "xxxfooterxxx";
  var limit = 100;

  for (var row=0; row<projectNON.length; row++) {
    if(projectNON[row][3]==Prjct){ 
      var resiNON = num++ + '. ) ' + '/d_' + projectNON[row][6] + '\n\n';
      allLocation.concat(resiNON);
      if( num === limit ) {
        newData.push(allLocation);
        limit = limit+100;
        allLocation = "";
      }
    }
  }
  for (var row=0; row<project.length; row++) {
    if(project[row][3]==Prjct){ 
      var resi = num++ + '. ) ' + '/d_' + project[row][6] + '\n\n';
      allLocation.concat(resi);
      if( num === limit ) {
        newData.push(allLocation);
        limit = limit+100;
        allLocation = "";
      }
    }
  }

  allLocation = allLocation.concat(footer);
  newData.push(allLocation);

  return newData;
}

Reference

TheWizEd
  • 7,517
  • 2
  • 11
  • 19