forked from usaussie/appscript-chat-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.gs
244 lines (211 loc) · 7.62 KB
/
code.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* CONFIGURE DEBUG, YOUR CHAT ROOM WEBHOOK URL, AND YOUR ARRAY OF FEEDS TO CHECK
*/
// When DEBUG is set to true, the topic is not actually posted to the room
var DEBUG = false;
// URL to a google sheet you have permissions to
// Columns MUST be in this order:
// feed_name {STRING)}
// feed_url (URL)
// feed_type (RSS or ATOM)
// feed_logo (URL)
// webhook_url (URL)
// status (STRING) - active | disabled
var GOOGLE_SHEET_URL = "https://docs.google.com/spreadsheets/d/doc_id/edit#gid=0";
var GOOGLE_SHEET_TAB_NAME = "feed_data";
var RESET_LAST_LOOKUP_TIME = 'Sun Jan 16 00:00:00 GMT-05:00 2022'; // Format: Fri Dec 10 14:37:12 GMT-05:00 2021
/*
* DO NOT CHANGE ANYTHING BELOW THIS LINE
*/
// only use this if you want to reset the time back to the specified time
function job_reset_last_lookup_time() {
var getcurrentUpdateTime = new Date(parseFloat(PropertiesService.getScriptProperties().getProperty("lastUpdate")) || 0);
Logger.log('Current Update time is: ' + getcurrentUpdateTime);
PropertiesService.getScriptProperties().setProperty("lastUpdate", new Date(RESET_LAST_LOOKUP_TIME).getTime());
var getnewUpdateTime = new Date(parseFloat(PropertiesService.getScriptProperties().getProperty("lastUpdate")) || 0);
Logger.log('New Update time is: ' + getnewUpdateTime);
}
// loop through all the filtered rows (the active ones)
function job_fetch_all_feeds() {
var all_sheet_rows = SpreadsheetApp.openByUrl(GOOGLE_SHEET_URL).getSheetByName(GOOGLE_SHEET_TAB_NAME).getDataRange().getValues();
var filteredRows = all_sheet_rows.filter(function(row){
if (row[5] === 'active') {
return row;
}
});
filteredRows.forEach(function(row, index) {
fetchNews_(row[0], row[1], row[2], row[3], row[4]);
});
var now = new Date();
PropertiesService.getScriptProperties().setProperty("lastUpdate", now.getTime());
Logger.log('Last Update Setting to: ' + now);
var getnewUpdateTime = new Date(parseFloat(PropertiesService.getScriptProperties().getProperty("lastUpdate")) || 0);
Logger.log('Last Update Time is:' + getnewUpdateTime);
}
// fetch a feed, and send any new events through to the associated Chat room
function fetchNews_(FEED_NAME, FEED_URL, FEED_TYPE, FEED_LOGO_URL, WEBHOOK_URL) {
var lastUpdate = new Date(parseFloat(PropertiesService.getScriptProperties().getProperty("lastUpdate")) || 0);
Logger.log("Last update: " + lastUpdate);
Logger.log("Fetching '" + FEED_NAME + "'...");
try{
var xml = UrlFetchApp.fetch(FEED_URL).getContentText();
var document = XmlService.parse(xml);
}
catch(err){
Logger.log(err);
}
if(FEED_TYPE == "RSS") {
Logger.log("RSS Feed being parsed - " + FEED_NAME);
try{
var items = document.getRootElement().getChild('channel').getChildren('item').reverse();
}
catch(err){
Logger.log(err);
}
Logger.log(items.length + " entrie(s) found");
var count = 0;
for (var i = 0; i < items.length; i++) {
try{
var pubDate = new Date(items[i].getChild('pubDate').getText());
var title = items[i].getChild("title").getText();
var description = items[i].getChild("description").getText();
var link = items[i].getChild("link").getText();
var eventDate = items[i].getChild("pubDate").getText();
}
catch(err){
Logger.log(err);
}
if (description == null){
description = "No description available."
}
// check to make sure the feed event is after the last time we ran the script
if(pubDate.getTime() > lastUpdate.getTime()) {
//Logger.log("Logging Event - Title: " + title + " | Date: " + eventDate + " | Link: " + link);
if(!DEBUG){
postTopicAsCard_(WEBHOOK_URL, FEED_NAME, FEED_URL, FEED_LOGO_URL, title, eventDate, description, link);
}
else{
Logger.log(pubDate);
Logger.log(pubDate.getTime());
Logger.log(title);
Logger.log(link);
Logger.log(description);
Logger.log("--------------------");
}
count++;
}
}
} else {
//must be ATOM then
Logger.log("ATOM Feed being parsed - " + FEED_NAME);
var url = FEED_URL;
var xml = UrlFetchApp.fetch(url).getContentText();
var document = XmlService.parse(xml);
var root = document.getRootElement();
var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
var entries = root.getChildren('entry', atom);
var count = 0;
for (var i = 0; i < entries.length; i++) {
try{
var title = entries[i].getChild('title', atom).getText();
var pubDate = new Date(entries[i].getChild('updated', atom).getText());
var link = entries[i].getChild("link", atom).getAttribute('href').getValue();
var eventDate = entries[i].getChild("updated", atom).getText();
}
catch(err){
Logger.log(err);
}
// check to make sure the feed event is after the last time we ran the script
if(pubDate.getTime() > lastUpdate.getTime()) {
//Logger.log("Logging Event - Title: " + title + " | Date: " + eventDate + " | Link: " + link);
if(!DEBUG){
postTopicAsCard_(WEBHOOK_URL, FEED_NAME, FEED_URL, FEED_LOGO_URL, title, eventDate, title, link);
}
else{
Logger.log(pubDate);
Logger.log(pubDate.getTime());
Logger.log(title);
Logger.log(link);
Logger.log("--------------------");
}
count++;
}
}
Logger.log(entries.length + " entrie(s) found");
Logger.log("--> " + count + " item(s) posted");
}
}
// quick function to take the info, send it to create a card, and then post the card.
function postTopicAsCard_(webhook_url, feed_name, feed_url, feed_logo_url, card_title, card_subtitle, card_description, card_link) {
var card_json = createCardJson_(feed_name, feed_url, feed_logo_url, card_title, card_subtitle, card_description, card_link);
// set options for what will be sent to Chat according to documentation
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(card_json)
};
try{
UrlFetchApp.fetch(webhook_url, options);
}
catch(err){
Logger.log(err);
}
}
/**
* Creates a card-formatted response.
* @return {object} JSON-formatted response
*/
function createCardJson_(feed_name, feed_url, feed_logo_url, card_title, card_subtitle, card_description, card_link) {
return {
"cards": [
{
"header": {
"title": feed_name,
"subtitle": feed_url,
"imageUrl": feed_logo_url
},
"sections": [
{
"widgets": [
{
"keyValue": {
"topLabel": "New Post",
"content": card_title,
"contentMultiline": "true",
"icon": "BOOKMARK",
"bottomLabel": card_subtitle
}
},
{
"keyValue": {
"topLabel": "Description",
"content": card_description,
"contentMultiline": "true",
"icon": "DESCRIPTION"
}
}
]
},
{
"widgets": [
{
"buttons": [
{
"textButton": {
"text": "OPEN LINK",
"onClick": {
"openLink": {
"url": card_link
}
}
}
}
]
}
]
}
]
}
]
};
}