-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
47 lines (38 loc) · 1.15 KB
/
handler.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
"use strict";
const querystring = require("querystring");
const axios = require("axios");
const StackOverflowURI = (title, sort = "relevance", order = "desc") => {
//sort = activity, votes, creation, relevance
//order = desc, asc
const baseURL = "https://api.stackexchange.com/2.2/search/advanced?";
const query = querystring.stringify({
order,
sort,
q: title,
site: "stackoverflow",
filter: "!-NA3i5zsvRkKYaEQLQcSR3s.llhvkX.IY"
});
const searchURL = baseURL + query;
return searchURL;
};
const formatResponse = (titleText, res) => {
let respString = `Top questions for: "${titleText}"\n`;
res.forEach(el => {
let check = "";
if (el.is_answered == true) check = ":heavy_check_mark:";
respString += `|${el.score}| ${check} <${el.link}|${el.title}> (${
el.answer_count
} answers)\n`;
});
return respString;
};
module.exports.overflow = async event => {
let text = querystring.parse(event.body).text;
let { data } = await axios.get(StackOverflowURI(text));
let res = data.items.filter((el, i) => i < 5);
let resp = formatResponse(text, res);
return {
statusCode: 200,
body: resp
};
};