-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.gs
More file actions
166 lines (143 loc) · 4.83 KB
/
code.gs
File metadata and controls
166 lines (143 loc) · 4.83 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var LOG_EXPIRY_DURATION = 365;
var token = "123456:dfgdfgfd";
var telegramUrl = "https://api.telegram.org/bot" + token;
var telegramWillieChatId = "123";
var telegramSharChatId = "124";
var googleWebAppAPI = "https://script.google.com/macros/s/xxxxxxxxxxxxxxxxxxxxxxxxxx/exec"
function setupWebApp() {
PropertiesService.getScriptProperties().setProperty("sheetID", SpreadsheetApp.getActiveSpreadsheet().getId());
}
function getMe(){
var url = telegramUrl + "/getMe";
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
}
function setupWebHook() {
var url = telegramUrl + "/setWebhook?url=" + googleWebAppAPI;
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
}
function getSheet() {
var spreadsheet = SpreadsheetApp.openById(PropertiesService.getScriptProperties().getProperty("sheetID"));
return spreadsheet.getSheetByName("Log");
}
function doGet(e) {
if (!validateCredentials(e.parameters["username"], e.parameters["password"])) {
return ContentService.createTextOutput(JSON.stringify({"result":"fail"})).setMimeType(ContentService.MimeType.JSON);
}
var data = getSheet().getDataRange().getValues().slice(1).reverse();
return ContentService.createTextOutput(JSON.stringify({"result":"success", "data":data})).setMimeType(ContentService.MimeType.JSON);
}
function doPost(e) {
Logger.log(e.postData.contents);
// var jsonString = e.postData.contents.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");
// Logger.log(jsonString);
var myObj = JSON.parse(e.postData.contents);
// if (!validateCredentials(myObj.password)) {
// return ContentService.createTextOutput(JSON.stringify({"result":"fail"})).setMimeType(ContentService.MimeType.JSON);
// }
if(validateCredentials(myObj.password)) {
Logger.log(myObj);
if(myObj.type == "SMS"){
forwardSMS(myObj);
}
else if (myObj.type == "NOTIFICATION"){
forwardNotification(myObj);
}
else if (myObj.type == "GMAIL"){
forwardGmail(myObj);
}
}
return ContentService.createTextOutput(JSON.stringify({"result":"success"})).setMimeType(ContentService.MimeType.JSON);
}
function forwardNotification(myObj){
var text = "Notification ("+myObj.appName+")" + "\n" + myObj.notificationTitle + "\n\n" + myObj.notificationText;
var url = telegramUrl + "/sendMessage";
var data = {
'text': text,
'chat_id' : telegramWillieChatId
};
var options = {
'method' : 'post',
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch(url, options);
}
function forwardGmail(myObj){
var text = "Notification ("+myObj.appName+")" + "\n" + myObj.notificationTitle + "\n\n" + myObj.summaryText;
var url = telegramUrl + "/sendMessage";
var data = {
'text': text,
'chat_id' : telegramWillieChatId
};
var options = {
'method' : 'post',
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch(url, options);
}
function forwardSMS(myObj){
getSheet().appendRow([new Date(), myObj.name, myObj.number, myObj.data]);
var text = myObj.data + "\n\nFrom " + myObj.name+ " (" + myObj.number + ")";
var url = telegramUrl + "/sendMessage";
var telegramChatId;
var data = {
'text': text,
'chat_id' : telegramWillieChatId
};
var options = {
'method' : 'post',
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch(url, options);
if(isFilterSMS(myObj.number)){
data.chat_id = telegramSharChatId;
options.payload = JSON.stringify(data);
UrlFetchApp.fetch(url, options);
}
}
function validateCredentials(password) {
// if (username != PropertiesService.getScriptProperties().getProperty("username")) {
// return false;
// }
if (password != PropertiesService.getScriptProperties().getProperty("password")) {
return false;
}
return true;
}
function isFilterSMS(number) {
var myObj = JSON.parse(PropertiesService.getScriptProperties().getProperty("SharSMSFilter").toLowerCase());
if(myObj.indexOf(number.toLowerCase())+1 == 0){
return false;
}
else{
return true;
}
}
function cleanOldRecords() {
var sheet = getSheet();
var data = sheet.getDataRange().getValues();
var rowsToDelete = 0;
for (var i in data) {
if (data[i][0] instanceof Date) {
var dayDifference = getDayDifference(new Date(), data[i][0]);
if (dayDifference > LOG_EXPIRY_DURATION) {
rowsToDelete = i;
} else {
break;
}
}
}
if (rowsToDelete > 0) {
sheet.deleteRows(2, rowsToDelete);
}
}
function getDayDifference(date1, date2) {
return (date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24);
}