-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
179 lines (154 loc) · 5.6 KB
/
bot.js
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
var request = require('request');
var xml2js = require('xml2js');
var fs = require('fs');
var config = require('./config');
var PERSIST_FILE = "persist.json";
var CD_SPY_URL = "https://www.chiefdelphi.com/forums/cdspy.php?do=xml";
const CD_POST_URL = "https://www.chiefdelphi.com/forums/showthread.php?p=";
const CD_THREAD_URL = "https://www.chiefdelphi.com/forums/showthread.php?threadid=";
var previousIDs;
function refresh() {
request(CD_SPY_URL, function(err, resp, body) {
if (err === null) {
handleXMLPage(body);
} else {
console.log("ERROR: could not retrieve chief delphi page.");
}
});
// call myself after timeout
setTimeout(refresh, config.refresh_rate);
}
function handleXMLPage(xmlData) {
xml2js.parseString(xmlData, function(err, result) {
var posts = result.events.event;
var newIDs = [];
for (var i = 0; i < posts.length; i++) {
var postXML = posts[i];
// more convenient object
var post = {
id : postXML.id[0],
author : postXML.poster[0],
postID : postXML.postid[0],
threadID : postXML.threadid[0],
preview : postXML.preview[0],
authorID : postXML.userid[0],
title : postXML.title[0]
};
// post url
post.url = CD_POST_URL + post.postID + "#post" + post.postID;
// thread url
post.t_url = CD_THREAD_URL + post.threadID;
// preview text but quoted
post.preview_quoted = "> " + post.preview.replace(/\n/g, "\n> ");
newIDs.push(post.id);
// if we haven't seen this before
if (!previousIDs.includes(post.id)) {
handleNewPost(post);
}
}
savePersistedIDs(newIDs);
previousIDs = newIDs;
});
}
function handleNewPost(post) {
var previewText = post.preview;
var previewTextLower = post.preview.toLowerCase();
config.triggers.forEach(function(trigger) {
// haha very funny i know
var triggered = false;
if (trigger.authors) {
for (var i = 0; i < trigger.authors.length; i++) {
var author = trigger.authors[i];
if (post.author.toLowerCase() == author.toLowerCase()) {
triggeredByAuthor(post, trigger);
triggered = true;
break;
}
}
}
if (triggered)
return;
var keywordHits = [];
if (trigger.keywords) {
trigger.keywords.forEach(function(k) {
if (k.constructor.name === 'String') {
var regexStr = "\\b" + k + "\\b";
var regex = new RegExp(regexStr, 'gi');
if (regex.test(previewText)) {
keywordHits.push(k);
triggered = true;
}
} else if (k.constructor.name === 'RegExp') {
if (k.test(previewTest)) {
keywordHits = keywordHits.concat(previewText.match(k));
triggered = true;
}
}
});
}
if (triggered)
triggeredByKeywords(post, trigger, keywordHits);
});
}
// post triggered by a particular author
function triggeredByAuthor(post, trigger) {
var message = config.format_author
.replace('$author', post.author)
.replace('$url', post.url)
.replace('$post_quoted', post.preview_quoted)
.replace('$post', post.preview)
.replace('$thread', post.title)
.replace('$t_url', post.t_url);
message = message.replace('$mention', trigger.mention || "");
sendMessage(message, trigger);
}
// post triggered by a particular keywords(s)
function triggeredByKeywords(post, trigger, keywords) {
var message = config.format_keyword
.replace('$keyword', keywords.join(', '))
.replace('$author', post.author)
.replace('$url', post.url)
.replace('$post_quoted', post.preview_quoted)
.replace('$post', post.preview)
.replace('$thread', post.title)
.replace('$t_url', post.t_url);
message = message.replace('$mention', trigger.mention || "");
sendMessage(message, trigger);
}
function sendMessage(msg, trigger) {
var channel = trigger.channel || config.default_channel;
var payload = {
"text": msg,
"username": config.bot_username,
"icon_emoji": config.bot_icon_emoji,
"channel": channel
};
var options = {
url: config.slack_webhook_url,
json: true,
body: payload
};
request.post(options, function(err, httpResp, body) {
console.log("sent with error:", err);
});
}
function getPersistedIDs() {
var ids;
if (fs.existsSync(PERSIST_FILE)) {
ids = JSON.parse(fs.readFileSync(PERSIST_FILE, 'utf8'));
} else {
ids = [];
savePersistedIDs(ids);
}
return ids;
}
// save array of persisted ids to PERSIST_FILE
function savePersistedIDs(ids) {
var json = JSON.stringify(ids);
fs.writeFileSync(PERSIST_FILE, json);
}
// run everything
previousIDs = getPersistedIDs();
console.log("Loaded persisted IDs");
console.log("Started refresh every", config.refresh_rate/1000, "seconds");
refresh();