Angely Fransiska
Angely Fransiska

Reputation: 21

How do I split long message, and send them in sequence?

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.)

Upvotes: 0

Views: 708

Answers (1)

TheWizEd
TheWizEd

Reputation: 8606

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

Upvotes: 1

Related Questions