-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.gs
39 lines (33 loc) · 1.15 KB
/
main.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function reset(){ docProps.setProperty('oldValue', 0); }
function onChange(e)
{
const sheet = e.source.getSheetByName('Sheet1');
const value = sheet.getRange('C1').getValue();
const oldValue = docProps.getProperty('oldValue') || 0 ;
const valueToNotify = 10;
if (value === valueToNotify && oldValue !== valueToNotify) { // prevent repeated notification, notify only once
docProps.setProperty('oldValue', value);
// schedule re-check after X hours
const resetAfter = 10; // in hours
const datetime = new Date();
datetime.setHours(datetime.getHours() + resetAfter);
ScriptApp.newTrigger('reset').timeBased().at(datetime).create();
// send notifications
const message = 'My value is now 10.';
const url = 'https://api.telegram.org/bot' + botToken + '/';
const persons = JSON.parse(list);
const requests = persons.map(person => {
return {
url: url,
method: 'post',
payload: {
method: 'sendMessage',
chat_id: person.id,
text: message,
parse_mode: 'HTML'
}
};
});
UrlFetchApp.fetchAll(requests); // to all persons in the `list`
}
}