0

The bot is already as administrator of the group and sending to google sheets all messages that are sent.

However, I don't want all members, I want only one specific member.

Let's say his id is 123456789, how do I get the bot to only send messages from that member?

My current script:

var token = "TOKEN BOT TELEGRAM";
var telegramUrl = "https://api.telegram.org/bot" + token;
var webAppUrl = "https://script.google.com/macros/s/ID TO WEB APP/exec";

function setWebhook() {
  var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
  var response = UrlFetchApp.fetch(url);
}

function doPost(e) {
  var contents = JSON.parse(e.postData.contents);
  var dateNow = new Date;
  var id = contents.message.from.id;
  var username = contents.message.from.username;
  var name = contents.message.from.first_name + " " + contents.message.from.last_name;
  var text = contents.message.text;
  var ssId = "ID TO SPREADSHEET";
  var sheet = SpreadsheetApp.openById(ssId).getSheetByName("Página1");

  sheet.appendRow([dateNow, id, username, name, text]);
}
Digital Farmer
  • 1,705
  • 5
  • 17
  • 67

1 Answers1

2

I believe your goal as follows.

  • You want to put the values from the user of the specific ID by modifying your script.

Modification point:

  • From your script, I understood that the ID can be retrieved by var id = contents.message.from.id. I thought that this might be used for achieving your goal.

When this point is reflected to your script, it becomes as follows.

Modified script:

function doPost(e) {
  var contents = JSON.parse(e.postData.contents);
  var dateNow = new Date;
  var id = contents.message.from.id;

  if (id != 123456789) return; // <--- Added. Or if (id.toSring() != "123456789") return;

  var username = contents.message.from.username;
  var name = contents.message.from.first_name + " " + contents.message.from.last_name;
  var text = contents.message.text;
  var ssId = "ID TO SPREADSHEET";
  var sheet = SpreadsheetApp.openById(ssId).getSheetByName("Página1");

  sheet.appendRow([dateNow, id, username, name, text]);
}

Note:

  • When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.

  • If you want to run the script for the multiple specific IDs, you can also use the following modified script.

      function doPost(e) {
        var contents = JSON.parse(e.postData.contents);
        var dateNow = new Date;
        var id = contents.message.from.id;
    
        var ids = [123456789,,,];  // Please set the specific IDs.
        if (!ids.includes(id)) return;
    
        var username = contents.message.from.username;
        var name = contents.message.from.first_name + " " + contents.message.from.last_name;
        var text = contents.message.text;
        var ssId = "ID TO SPREADSHEET";
        var sheet = SpreadsheetApp.openById(ssId).getSheetByName("Página1");
    
        sheet.appendRow([dateNow, id, username, name, text]);
      }
    
  • If above modification is not the result you expect, can you provide your sample value of e.postData.contents in your script? By this, I would like to confirm your current situation.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165