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 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 = "" + token;
  var url = telegramUrl + "/sendMessage?chat_id=xxx&text=Hello+World";
  var response = UrlFetchApp.fetch(url);

and here is a photo of how my spreadsheet looks like


“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(; // date
  sheet.setActiveSelection('B' + lastRow).setValue(; // 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

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( " " +;
  //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 exists in the list
    //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";

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("" + botSecret + "/sendMessage?text=" + "the text that you want "  + "&chat_id=" + id + "&parse_mode=HTML");