var thesh = SpreadsheetApp.getActive().getSheetByName("Setup").getRange(26, 2, 6).getValues()
var token = thesh[1][0]
var fastSend = thesh[2][0]
var imprange = thesh[4][0]
var norepeat = thesh[5][0]
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Telegram')
.addItem('Send Now', 'telegram')
.addToUi();
}
function setTrigger(){
removeTrigger()
var time = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Setup").getRange("C15").getValue()
createTrigger(time)
}
function createTrigger(time) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ScriptApp.newTrigger('telegram')
.timeBased()
.everyDays(1)
.atHour(time)
.create();
}
function createTriggerOnChange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ScriptApp.newTrigger('telegram')
.forSpreadsheet(SpreadsheetApp.getActive())
.onChange()
.create();
}
function removeTrigger() {
// Loop over all triggers.
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) {
// If the current trigger is the correct one, delete it.
if (allTriggers[i].getUniqueId() == allTriggers[i].getUniqueId()) {
ScriptApp.deleteTrigger(allTriggers[i]);
break;
}
}
}
function telegramf() {
var throttle = 0
var nlen2 = 0
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName("Data")
var lastRow = sheet.getLastRow() - 1
var date = new Date()
var data = sheet.getRange(2, 1, lastRow, 12).getValues()
var data1 = sheet.getRange(1, 1, 1, 12).getValues()
var end = data.length
if (fastSend === "Yes") {
var nlen2 = data.filter(function(value){return value[11]}).length
}
if (imprange === "Yes") {
var end = data.filter(function(value){return value[0]}).length
}
var messaget = ss.getSheetByName("Template").getRange("B1").getValue()
for (var i = nlen2; i < end; ++i){
if (throttle >= 8) {
Utilities.sleep(10000)
throttle = 0
} else {
throttle = throttle + 1
}
var message = messaget
var row = data[i];
var mobile = row[0].toString()
if (row[10] <= date){
for (var g = 1; g < 11; ++g){
var temp = row[g-1]
if (temp instanceof Date) {
temp = temp.toLocaleDateString("en-US")
}
var message = message.replace("$("+data1[0][g-1]+")",temp)
}
var existinStatus = data.filter(function(value){
if (value[11].toString() === message) {
return "yes"
}
})
var strictNoRepeat = existinStatus.length
if (norepeat === "Off") {
strictNoRepeat = 0
}
if (message != row[11] && strictNoRepeat === 0) {
if(sendMessage(mobile,message) === 1){
sheet.getRange(2 + i,12).setValue(message);
} else {
sheet.getRange(2 + i,12).setValue("Failed");
}
}
}
}
}
function sendMessage(chat_id, text) {
var data = {}
var allContacts = chat_id.split("&")
try {
for (var i = 0; i < allContacts.length; ++i){
var number = allContacts[i]
data = {
method: "post",
payload: {
method: "sendMessage",
chat_id: String(number),
text: text,
parse_mode: "HTML",
disable_web_page_preview: true
}
}
var sendNow = UrlFetchApp.fetch("https://api.telegram.org/bot" + token + "/", data);
var b = sendNow
}
return 1
} catch (e) {
return 2
}
}
function telegram() {
var lock = LockService.getScriptLock();
lock.waitLock(60000);
telegramf()
lock.releaseLock();
}
No comments:
Post a Comment
THANKS FOR YOUR SUPPORT