簡體   English   中英

如何只向與我的電報機器人互動的每個用戶發送一次消息

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

所以我使用這個源代碼在 GAS 中構建了我的電報機器人,還使用消息處理程序在 Google 電子表格中獲取message.message.chat.id

所以現在我想要的是:

  1. 刪除重復的chat_id ,因為它在每次用戶發送消息時都會重復
  2. 向那些chat_id發送消息

我試過的:

我使用了這個 function,它運行良好,但問題是手動更改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());
}

這是我的電子表格的照片

電子表格

“如您所見,第 3、4、5 行中有一個重復的chat_id ,我只想要其中一個,所以當我向他們發送消息時,它不會發送多次 - 我想要第 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 function saveMessage(message) 來解決我的第一個問題(不再重復 chat_id!)

之后我找到了這個表格,它談到了檢索行,所以我用它來 select 電子表格中的所有 chat_id 並將它循環到我的發送文本 function

它在這里代表:

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");
 });
} 
  1. 不要對 chat_id 進行chat_id ,而是將chat_id作為 function 參數傳遞,並使用字符串連接或字符串模板構建 URL
  2. 制作執行以下操作的 function:
    1. 從您的電子表格中讀取chat_id
    2. 獲取唯一的chat_id列表
    3. 遍歷唯一的chat_id列表以將消息發送到唯一的chat_id列表(從 1 調用您的 function 的改進版本。

有關的

問題:

  • 如果工作表中存在聊天 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)
  }
}

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM