-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckForNewFiles.gs
More file actions
101 lines (82 loc) · 3.08 KB
/
checkForNewFiles.gs
File metadata and controls
101 lines (82 loc) · 3.08 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
/***************************************************
This Google Script will send an slack notification to your Slack Channel
when a file in a Google Drive folder has been added, or modified.
***************************************************/
var ROOT_FOLDER_ID = '***************';
var SLACK_WEBHOOK = 'https://hooks.slack.com/services/************************';
var CHANNEL = 'Channel_name';
function checkForChangedFiles() {
var folderList = listFolders(ROOT_FOLDER_ID) || [];
folderList = folderList.split(",");
for (var i = 0; i < folderList.length; i++) {
checkForChangedFilesInFolder(folderList[i]);
}
}
//Function to check which files have changed in the last 15 minutes
function checkForChangedFilesInFolder(folder_id) {
var folderID = '"' + folder_id + '"';
var folderSearch = folderID + " " + "in parents";
var timezone = Session.getScriptTimeZone();
var today = new Date();
var fifteenMinutesAgo = new Date(today.getTime() - 60 * 1000 * 15);
var startTime = fifteenMinutesAgo.toISOString();
var search = '(trashed = true or trashed = false) and ' + folderSearch;
var files = DriveApp.searchFiles(search);
today = Utilities.formatDate(fifteenMinutesAgo, timezone, "yyyy-MM-dd HH:mm");
while (files.hasNext()) {
var file = files.next();
var dateCreated = Utilities.formatDate(file.getDateCreated(), timezone, "yyyy-MM-dd HH:mm")
if (dateCreated > today) {
sendSlackNotification({
fileName: file.getName(),
fileURL: file.getUrl(),
fileOwner: file.getOwner().getName()
});
}
}
}
//Makes the payload and calls the function to send the message
function sendSlackNotification(changedFile) {
var payload = {
"icon_emoji": ":rolled_up_newspaper:",
"channel": "#" + CHANNEL,
"username": "Nueva Entrada",
"text": changedFile.fileOwner + " ha añadido un nuevo documento " + changedFile.fileName + " en " + changedFile.fileURL
};
sendMessageToSlack(payload);
}
//Send message to slack
function sendMessageToSlack(payload) {
var url = SLACK_WEBHOOK;
var options = {
'method': 'post',
'payload': JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(url, options);
}
//Get all the subfolders from the root folder
function listFolders(id) {
var parentFolder = DriveApp.getFolderById(id);
var childFolders = parentFolder.getFolders();
var childs = [];
while (childFolders.hasNext()) {
var child = childFolders.next();
childs.push(getSubFolders(child));
}
childs.push(id);
return childs.join(",");
}
//Get all the subfolders recursively
function getSubFolders(parent) {
parent = parent.getId();
var childFolder = DriveApp.getFolderById(parent).getFolders();
var childs = [];
if (!childFolder.hasNext()) return parent;
while (childFolder.hasNext()) {
var child = childFolder.next();
var res = getSubFolders(child);
childs.push(res);
}
childs.push(parent);
return childs.join(",");
}