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:
- remove the duplicated
chat_id
s, because it repeated itself in every time a user sends a message - 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
"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
}
— edit —
so I used Nikko J. editd function saveMessage(message) to help my first problem (no more duplicated chat_id!)
after that I had found this form that talked about Retrieve Rows, so I used it to select all the chat_id form the spreadsheet and loop it in my send text function
and it represntet here :
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");
});
}
2
Answers
chat_id
, pass thechat_id
as a function argument and build the URL either by using string concatenation or string templatechat_id
s from your spreadsheetchat_id
schat_id
s to to send the messages to the unique list ofchat_id
s (call your the improved version of your function from 1.Related
Problem:
UrlFetchApp.fetch()
Here I copied your code and made some modifications to replicate the scenario:
References: