-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
360 lines (294 loc) · 12 KB
/
server.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
var express = require('express');
var app = express();
var port = process.env.port || 1337;
const jsdom = require("jsdom");
const {JSDOM} = jsdom;
var request = require("request");
var jsonq=require("jsonq");
var jquery = require("jquery");
var bodyParser = require("body-parser");
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var quote = '' ;
var quotesObj = [];
var all_links = [];
app.use(urlencodedParser);
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.get('/Giri', function(req, res) {
res.send('Giri is god');
});
app.get('/lol', function(req, res) {
res.send('LOL');
});
// app.post('/test', urlencodedParser, function(req, res) {
// // var name = req.query.name;
// // res.send("Hello " + name);
// res.status(200).end() // best practice to respond with empty 200 status code
// var reqBody = req.body
// var responseURL = reqBody.response_url
// if (reqBody.token != 'en4O0pLksumht6WRxvw95Z93'){
// res.status(403).end("Access forbidden")
// }else{
// var message = {
// "text": "This is your first interactive message",
// "attachments": [
// {
// "text": "Building buttons is easy right?",
// "fallback": "Shame... buttons aren't supported in this land",
// "callback_id": "button_tutorial",
// "color": "#3AA3E3",
// "attachment_type": "default",
// "actions": [
// {
// "name": "yes",
// "text": "yes",
// "type": "button",
// "value": "yes"
// },
// {
// "name": "no",
// "text": "no",
// "type": "button",
// "value": "no"
// },
// {
// "name": "maybe",
// "text": "maybe",
// "type": "button",
// "value": "maybe",
// "style": "danger"
// }
// ]
// }
// ]
// }
// sendMessageToSlackResponseURL(responseURL, message)
// }
// });
// function sendMessageToSlackResponseURL(responseURL, JSONmessage){
// var postOptions = {
// uri: responseURL,
// method: 'POST',
// headers: {
// 'Content-type': 'application/json'
// },
// json: JSONmessage
// }
// request(postOptions, (error, response, body) => {
// if (error){
// // handle errors as you see fit
// }
// })
// }
app.post('/actions', urlencodedParser, (req, res) =>{
res.status(200).end(); // best practice to respond with 200 status
var actionJSONPayload = JSON.parse(req.body.payload); // parse URL-encoded payload JSON string
var index = parseInt(actionJSONPayload.actions[0].name);
var s_index = actionJSONPayload.actions[0].name;
var details_full = actionJSONPayload.original_message.attachments[index-1].pretext;
var details_len = details_full.length;
var details = details_full.substring(14, details_len).replace('*', '').replace(/_/g, '').replace('*', '').replace('*', '');
var message = {
"response_type" : "in_channel",
//"text": "A quote from Medium",
"replace_original": true,
"attachments" : [
{
"color": "#1466ad",
"title": details,//">>> A Quote from Medium",
"text" : "_" + quotesObj[index-1] + "_" + "\n*>> Link to original post: *" + all_links[index-1],
"mrkdwn_in" : [
"text",
"title"
]
}
]
};
sendTest(actionJSONPayload.response_url, message);
});
app.post('/slash', function(req, res) {
var reqBody = req.body;
if (reqBody.token != 'en4O0pLksumht6WRxvw95Z93')
{
res.status(403).end("Access forbidden!");
}
else{
profileURL = "https://medium.com/@rahulkayala/highlights";
JSDOM.fromURL(profileURL).then(dom => {
var document = dom.window.document;
links = document.getElementsByClassName(" quoteItem-content");
for (var i = 0; i < links.length; i=i+1)
{
all_links.push(links[i].getAttribute("href"));
}
});
var numPosts = req.body.text || 1;
var highlightsURL = "https://medium.com/_/api/users/9755409acb75/profile/stream?limit=" + numPosts + "&to=0&source=quotes&pages=1";
var output = '' ;//= req.body.response_url + ", ";
var response_url = req.body.response_url;
var attachmentsObj = [];
quotesObj = [];
res.send({
"response_type": "ephemeral", //THIS WAS CHANGED
"text": "Highlights are coming up..."
});
request(highlightsURL, function (error, response, body) {
var newBody = "";
// Trim out random garbage characters in the beginning of the body (non-JSON)
for (var i=16; i<body.length; i++) {
newBody += body[i];
}
// Convert to a JSON object for using jsonQ functions
var object = jsonq(newBody);
// Find all quoteIDs
var quoteID = object.find('payload').find('references').find('quoteId').value();
//console.log(numPosts + " most recent highlights by " + userName + " (@" + userHandle + ")");
for (var i = 0; i < quoteID.length; i++) {
var postID = object.find('payload').find('references').find('Quote').find(quoteID[i]).find('postId').value();
var postName = object.find('payload').find('references').find('Post').find(postID).find('title').value();
var postAuthorID = object.find('payload').find('references').find('Post').find(postID).find('creatorId').value();
var postAuthor = object.find('payload').find('references').find('User').find(postAuthorID).find('name').value();
var quoteParagraphRaw = object.find('payload').find('references').find('Quote').find(quoteID[i]).find('paragraphs').find('text').value();
var startOffset = object.find('payload').find('references').find('Quote').find(quoteID[i]).find('startOffset').value();
var endOffset = object.find('payload').find('references').find('Quote').find(quoteID[i]).find('endOffset').value();
// Convert the array to a string
var quoteParagraphString = quoteParagraphRaw.join("");
// Get only the highlighted section
quote = quoteParagraphString.substring(startOffset, endOffset) //CHANGED AT 1222
quotesObj.push(quote); //ADDED at 1239
// Get a little bit of content before and after the quote
var offset = 50;
var paragraphStart = parseInt(startOffset) - offset;
var paragraphEnd = parseInt(endOffset) + offset;
if (paragraphStart < 0) paragraphStart = 0;
//if (paragraphEnd > quoteParagraphString.length) paragraphEnd = quoteParagraphString.length;
var quoteParagraph = "";
if (paragraphStart != 0) quoteParagraph += "...";
quoteParagraph += quoteParagraphString.substring(paragraphStart, paragraphEnd);
if (endOffset != paragraphEnd) quoteParagraph += "..";
var startingPart = quoteParagraphString.substring(paragraphStart, startOffset);
var endingPart = quoteParagraphString.substring(endOffset, paragraphEnd);
highlightNumber = parseInt(i);
highlightNumber++;
// Output
outputString = "\nHighlight #" + highlightNumber + ": From \"" + postName + "\" by \"" + postAuthor + "\"\n\n" + quoteParagraph + "\n";
var strhighlight = highlightNumber.toString();
// Formatting
var obj = {
"pretext": "*Highlight #" + highlightNumber + "* from *" + postName + "* by _" + postAuthor + "_",
"text": "`" + quote + "`",
"callback_id" : "button-trial",
"mrkdwn_in": [
"text",
"pretext"
],
"color": "#3AA3E3",
"actions": [
{
"name": strhighlight,
"text": "Send as message",
"type": "button",
"value": "Send as message"
}
]
};
attachmentsObj.push(obj);
output += outputString;
//console.log(outputString);
}
var responseObj = {
"response_type" : "in_channel",
"text" : "Here are your highlights:",
"attachments": attachmentsObj
}
sendTest(response_url, responseObj); //CHANGED
// function sendTest(JSONmsg/*CHANGED*/) {
// request({
// url: response_url,
// method: "POST",
// json: JSONmsg, //CHANGED
// headers: {
// "content-type": "application/json",
// },
// }, function(error, response, body)
// {
// });
// }
// function postMsg(){
// request({
// url: response_url,
// method: "POST",
// json: {
// "response_type" : "in_channel"
// }
// })
// }
});
}
});
function sendTest(responseURL, JSONmsg) {
request({
url: responseURL,
method: "POST",
json: JSONmsg,
headers: {
"content-type": "application/json",
},
}, function(error, response, body)
{
});
}
// app.post('/actions', urlencodedParser, function(req, res){
// res.status(200).end()
// var reqBody = req.body
// if (reqBody.token != 'en4O0pLksumht6WRxvw95Z93')
// {
// res.status(403).end("Access forbidden")
// }
// else
// {
// var JSONpayload = JSON.parse(req.body.payload)
// var payload = {
// //"response_type" : "in_channel",
// "text" : "Your message",
// //"replace_original" : true
// }
// sendTest("https://hooks.slack.com/services/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", payload);
// // function sendTest2(JSONmsg/*CHANGED*/) {
// // request({
// // url: response_url,
// // method: "POST",
// // json: JSONmsg, //CHANGED
// // headers:
// // {
// // "content-type": "application/json",
// // },
// // }, function(error, response, body){}
// // );
// // }
// }
// });
var server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
app.get('/setup', function (req, res) {
var InitialSetup = require("./InitialSetup");
//res.send('Setup page');
InitialSetup.Setup(req, res);
});
app.get('/entity', function (req, res) {
var entity = require("./entity");
entity.getEntity(req, res);
});
app.get('/scraper', function (req, res) {
var entity = require("./scraper");
entity.Scraper(req, res);
});
app.get('/search', function (req, res) {
var search = require("./search");
search.Search(req, res);
});