function sendToTelegram() {
var token = "BOT_TOKEN";
var channelId = "CHANNEL_ID";
var spreadsheetId = "SHEET_ID";
var sheetName = "SHEET_NAME";
var storageKey = "LAST_SENT_ROW";
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
var lastSentRow = parseInt(PropertiesService.getScriptProperties().getProperty(storageKey) || 0);
var lastRow = sheet.getLastRow();
if (lastSentRow >= lastRow) {
// Check if there are any new entries after the last sent row
lastRow = checkForNewEntries(sheet, lastSentRow);
if (lastRow === lastSentRow) {
// No new entries found, exit
return;
}
}
var name, score, image;
for (var i = lastSentRow + 1; i <= lastRow; i++) {
name = sheet.getRange(i, 1).getValue(); // Assuming Name is in column A
score = sheet.getRange(i, 2).getValue(); // Assuming Score is in column B
image = sheet.getRange(i, 3).getValue(); // Assuming Image URL is in column C
if (name !== "" && score !== "") { // Check if name and score are not empty
var message = "Product: " + name + "\nBuyLink: " + score;
// Prepare the payload for sending both text and image, if image URL is provided
var payload = {
"chat_id": channelId,
"caption": message
};
if (image !== "") {
payload["photo"] = image;
var url = "https://api.telegram.org/bot" + token + "/sendPhoto";
} else {
var url = "https://api.telegram.org/bot" + token + "/sendMessage";
payload["text"] = message;
}
var options = {
"method": "post",
"payload": payload
};
try {
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
} catch (error) {
Logger.log("Error:", error);
}
// Update the last sent row
PropertiesService.getScriptProperties().setProperty(storageKey, i.toString());
break; // Exit loop after sending the message
}
}
}
function checkForNewEntries(sheet, lastSentRow) {
var lastRow = sheet.getLastRow();
var hasNewEntries = false;
for (var i = lastSentRow + 1; i <= lastRow; i++) {
var name = sheet.getRange(i, 1).getValue(); // Assuming Name is in column A
var score = sheet.getRange(i, 2).getValue(); // Assuming Score is in column B
if (name !== "" && score !== "") { // Check if name and score are not empty
hasNewEntries = true;
break;
}
}
return hasNewEntries ? lastRow : lastSentRow;
}
Enter Your Name then DISCUSS and don't Post Links otherwise ban here
image quote pre code