如何仅向与我的电报机器人互动的每个用户发送一次消息

穆罕默德·阿巴斯

所以我使用这个源代码在 GAS 中构建了我的电报机器人,并且还使用了消息处理程序来抓取message.message.chat.id谷歌电子表格

所以现在我想要的是:

  1. 删除重复的chat_ids,因为每次用户发送消息时它都会重复自己
  2. 给那些chat_ids发消息

我尝试过的:

我使用了这个功能,它工作正常,但问题是chat_id手动更改将是一场噩梦

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());
}

这是我的电子表格的外观照片

电子表格

“如您所见chat_id,第 3、4、5 行中有一个重复的s,我只想要其中一个,所以当我向他们发送消息时,它不会多次发送 - 我想要的是 1”

这是我使用的消息处理程序

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
  
}

——编辑——

所以我使用 Nikko J.editd 函数 saveMessage(message) 来解决我的第一个问题(不再重复 chat_id!)

之后,我找到了这个谈论 Retrieve Rows 的表单,所以我用它来选择电子表格中的所有 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");
 });
} 
日光J。

问题:

  • 如果聊天 ID 存在于工作表中,您希望忽略传入消息以避免重复
  • 使用发送消息给用户 UrlFetchApp.fetch()

在这里,我复制了您的代码并进行了一些修改以复制场景:

function sendMessage(body ,chatId){
  var botSecret = "xxx";
  var response = UrlFetchApp.fetch("https://api.telegram.org/bot" + botSecret + "/sendMessage?text=" + encodeURIComponent(body) + "&chat_id=" + chatId + "&parse_mode=HTML");
}

function testFunction(){
  var message = {
    message : {
      date: "01/01/20",
      text: "This is jus a random text",
      chat: {
        first_name: "FNAME",
        last_name: "LNAME",
        id: 1234
      },
      from: {
      username: "abcd"
      }
    }
  }
  saveMessage(message);
}

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

参考:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用我的电报机器人通过用户名向某人发送消息

我无法向电报机器人用户发送消息,但我自己

我的电报机器人不断发送消息

电报机器人发送消息

如何安排电报机器人发送消息?

我的电报机器人无法读取其他电报机器人发送的消息

您如何获取从电报机器人发送的当前发送消息的消息 ID?

电报机器人向聊天错误发送消息:找不到聊天

自动发送消息到电报机器人

从电报机器人发送消息的限制是多少

删除电报机器人发送的消息

电报机器人不发送消息

电报机器人无法发送直接消息

向不与我的机器人共享服务器的用户发送消息

如何在机器人消息电报机器人之后从用户那里获取消息更新

如何向电报机器人发送有关最近餐馆的位置?

如何更改电报机器人消息中的图片?

如何从电报机器人创建私人消息?

电报机器人错误:我无法发送其用户名

电报机器人,如何请求用户输入?

电报机器人用户控制

我如何部署电报机器人?

如何获取上一条消息(使用JAVA的电报机器人)

电报机器人:如何删除/删除机器人的用户图片?

删除电报机器人发送的外发消息(电报,python)

我如何知道电报用户是否使用电报机器人 API 加入了我的频道?

如何阻止有人向电报机器人的嵌入式键盘发送垃圾邮件?

当组中的用户发送消息中的 URL 时,如何在电报机器人脚本的末尾检测预览是否打开

我的Discord机器人正在使用discord.js一次发送多条消息