<pre>
<code>
var bud = 0
function main ()
{
sendTelegramMessage('Budget check in Google Ads:', CHAT_ID);
var accounts = MccApp.accounts()
.withCondition("Clicks >= 1")
.forDateRange("LAST_14_DAYS")
.executeInParallel("budgetControl")
Logger.log(bud);
sendTelegramMessage('Budget check: complete', CHAT_ID);
}
function budgetControl()
{
var accountName = AdWordsApp.currentAccount().getName();
var budgets = AdWordsApp.budgetOrders().withCondition('Status = ACTIVE').get();
while (budgets.hasNext())
{
try {
var budget = budgets.next();
if (budget.getSpendingLimit() !== null )
{
var startDate = timeFormat(budget.getStartDateTime());
var cost = AdWordsApp.currentAccount().getStatsFor(startDate,today()).getCost().toFixed(2);
var last7DaysCostByDay = (AdWordsApp.currentAccount().getStatsFor("YESTERDAY").getCost()).toFixed(2);
var limit = budget.getSpendingLimit().toFixed(2);
var remainingDays = rDays(limit, cost, last7DaysCostByDay);
var budgetNow = (limit - cost).toFixed(2);
if (budgetNow < 0)
{
var budgetNow = 0;
}
else
{
var budgetNow = budgetNow;
}
Logger.log([accountName, limit, cost, budgetNow,last7DaysCostByDay,remainingDays]);
if (remainingDays < 6 && last7DaysCostByDay >0)
{
bud = 1
sendTelegramMessage('Account ' + accountName+ '. Remaining days = ' + remainingDays + '.' + ' Current balance = ' + budgetNow + ' rubles.'+ ' Yesterday’s cost = ' + last7DaysCostByDay + ' rubles.',CHAT_ID);
/*
MailApp.sendEmail(CONFIG.email,
CONFIG.names +' / Account budget is getting low: ' + accountName,
'Account ' + accountName + ' . Current balance = ' + budgetNow +
'. Cost per day = ' + last7DaysCostByDay + ' in the account currency. ' +
'Budget has ' + remainingDays + 'day(s) worth of advertising funds. Account budget is getting low. It is necessary to warn the PM.');
*/
}
}
}
catch (e)
{
Logger.log(e);
sendTelegramMessage('Budget Control script error ' + accountName);
//MailApp.sendEmail(CONFIG.email,
//'Budget Control script error',
//'Budget Control script check requested ' + accountName + ' ' + e);
}
}
}
function timeFormat (date)
{
var year = date.year.toString();
var month = date.month.toString();
var day = date.day.toString();
if (month.length == 1)
{
month = "0" + month;
}
if (day.length == 1) {
day = "0" + day;
}
return [year, month, day].join("");
}
function today () {
var date = new Date();
var timeZone = AdWordsApp.currentAccount().getTimeZone();
var format = 'yyyyMMdd';
return Utilities.formatDate(date, timeZone, format);
}
function rDays(limit, cost, last7DaysCostByDay) {
var remainingDays = ((limit - cost) / last7DaysCostByDay).toFixed();
if (remainingDays < 1 || remainingDays == "Infinity" || remainingDays == "-Infinity" || remainingDays == -0 ) {
remainingDays = 0;
}
return remainingDays;
}
function sendTelegramMessage(text, chat) {
var CONFIG2 = {
TOKEN: TOKEN_ID,
//CHAT_ID: '211002715'
CHAT_ID: '-575137669'
};
var telegramUrl = 'https://api.telegram.org/bot' + CONFIG2.TOKEN + '/sendMessage?chat_id=' + chat + '&text=';
var message = encodeURIComponent(text);
var sendMessageUrl = telegramUrl + message;
var options = {
method: 'POST',
contentType: 'application/json'
};
try {
UrlFetchApp.fetch(sendMessageUrl, options);
} catch (e) {
// Logs an ERROR message.
Logger.log(Error: ' +e);
Logger.log('User: ' + chat);
}
}
function rDays(limit, cost, last7DaysCostByDay) {
var remainingDays = ((limit - cost) / last7DaysCostByDay).toFixed();
if (remainingDays < 1 || remainingDays == "Infinity" || remainingDays == "-Infinity" || remainingDays == -0 ) {
remainingDays = 0;
}
return remainingDays;
}
</code>
</pre>