How to send message only once to every individual user who has engaged with my telegram bot?

So I used this source code to build my telegram bot in GAS, and also used message handler to grab message.message.chat.id in a Google spreadsheet

so now what I want is:

  1. remove the duplicated chat_id s, because it repeated itself in every time a user sends a message
  2. send a message to those chat_id s

What I have tried:

I used this function, it worked fine but the problem is it will be nightmare to change the chat_id value manually!

function SendTest() {
  var token = "xxx";
  var telegramUrl = "https://api.telegram.org/bot" + token;
  var url = telegramUrl + "/sendMessage?chat_id=xxx&text=Hello+World";
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

and here is a photo of how my spreadsheet looks like

spreedsheet

“as you see there is a duplicated chat_id s like in row 3,4,5 I just want one of them, so when I send them a message it doesn’t send several times - what I want number 1”

and this the message handler that I used

function saveMessage(message) {
  let file = SpreadsheetApp.openById(sheetLogId);
  // first tab of the file
  let sheet = file.getSheets()[0];
  // get last row
  let lastRow = sheet.getLastRow() + 1;
  
  sheet.setActiveSelection('A' + lastRow).setValue(Date(message.message.date)); // date
  sheet.setActiveSelection('B' + lastRow).setValue(message.message.chat.id); // chat id
  sheet.setActiveSelection('C' + lastRow).setValue(message.message.from.username); // username
  sheet.setActiveSelection('E' + lastRow).setValue(message.message.text); // message
  sheet.setActiveSelection('D' + lastRow).setValue(message.message.chat.first_name+ " " + message.message.chat.last_name); // message
  
}

Hi @alprens_hamo,
1)In the saveMessage function, you could check if the chat ID already exists in the sheet before appending the values.

Could you also share any link where I can get a explanation of how this works.and where it is hosted

i used this function to stop the duplicate of the chat id

function saveMessage(message) {
  let file = SpreadsheetApp.getActiveSpreadsheet();
  // first tab of the file
  let sheet = file.getSheets()[0];
  // get last row
  let lastRow = sheet.getLastRow() + 1;
  var newArr = [];
  //insert data into array. By using this, we can use setValues() and lessen the API calls
  newArr.push(Date(message.message.date));
  newArr.push(message.message.chat.id);
  newArr.push(message.message.from.username);
  newArr.push(message.message.text);
  newArr.push(message.message.chat.first_name+ " " + message.message.chat.last_name);
  //get all chat ids in sheets
  var ids = sheet.getRange('B1:B').getValues().filter(val => val != "");
  //convert chat ids array into 1 dimensional array so we can easily use Array.includes()
  var newArr2 = [].concat(...ids);
  //check if message.message.chat.id exists in the list
  if(!newArr2.includes(message.message.chat.id)){
    //update sheet and message user if chat id does not exists in sheet
    sheet.getRange(lastRow,1, 1, newArr.length).setValues([newArr]);
    var message = "Hello world";
    sendMessage(message, message.message.chat.id)
  }
}

after that used this one to send the msg to those chat id

function sendtext(){
 var rows = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
 var botSecret = "the bot token";
 rows.forEach(function(row, index) {
   Logger.log(row[1] + ":" + index)
 var id = row[1];
 UrlFetchApp.fetch("https://api.telegram.org/bot" + botSecret + "/sendMessage?text=" + "the text that you want "  + "&chat_id=" + id + "&parse_mode=HTML");
 });
}