Skip to content

Instantly share code, notes, and snippets.

@batazor
Last active April 23, 2023 12:09
Show Gist options
  • Save batazor/99b3176f8fa4e723a9e8f83ab7c2d14d to your computer and use it in GitHub Desktop.
Save batazor/99b3176f8fa4e723a9e8f83ab7c2d14d to your computer and use it in GitHub Desktop.
function getEmails(sheetName, emailColumn, messageColumn) {
// Get the active spreadsheet and the specified sheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName(sheetName);
// Get the range containing the data
var dataRange = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
// Get the values from the data range
var data = dataRange.getValues();
// Create an empty array to store the emails and messages
var emailsAndMessages = [];
// Iterate through the rows in the data
for (var i = 0; i < data.length; i++) {
var row = data[i];
// Get the email and message from the row based on the specified column numbers
var email = row[emailColumn - 1];
var message = row[messageColumn - 1];
// Add the email and message to the emailsAndMessages array
emailsAndMessages.push({email: email, message: message});
}
// Return the emailsAndMessages array
return emailsAndMessages;
}
// Replace 'YOUR_BOT_TOKEN' with the token you received from the BotFather
var TELEGRAM_BOT_TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN';
// Replace 'YOUR_CHAT_ID' with the chat ID where you want to send the messages
var TELEGRAM_CHAT_ID = 'YOUR_YOUR_CHAT_ID';
function sendMessageToTelegram(text) {
var apiUrl = 'https://api.telegram.org/bot' + TELEGRAM_BOT_TOKEN + '/sendMessage';
var payload = {
'chat_id': TELEGRAM_CHAT_ID,
'text': text,
'parse_mode': 'HTML'
};
var options = {
'method': 'post',
'payload': payload
};
UrlFetchApp.fetch(apiUrl, options);
}
function sendEmailsToTelegram() {
// Replace "SheetName" with the name of the sheet containing the emails and messages
var sheetName = "Sheet1";
// Specify the column numbers for the email and message columns
var emailColumn = 1;
var messageColumn = 2;
var emailsAndMessages = getEmails(sheetName, emailColumn, messageColumn);
var message = 'Email addresses and messages from the sheet:\n\n';
// Format the email addresses and messages for the Telegram message
for (var i = 0; i < emailsAndMessages.length; i++) {
var emailAndMessage = emailsAndMessages[i];
message += emailAndMessage.email + ': ' + emailAndMessage.message + '\n';
}
sendMessageToTelegram(message);
}
email message
tes1@gmail.com 111
tes2@gmail.com 222
tes3@gmail.com 333
tes4@gmail.com 444
Email addresses and messages from the sheet:

tes1@gmail.com: 111
tes2@gmail.com: 222
tes3@gmail.com: 333
tes4@gmail.com: 444
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment