-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathFind Paper.js
49 lines (43 loc) · 1.98 KB
/
Find Paper.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
/*
activation_example:!findpaper paper details
regex:!findpaper
flags:
*/
// Extracting the search query
var searchQuery = current.text.replace(/!findpaper/gmi, "").trim().substring(0, 1000);
// Searching for a research paper
var researchPaperRequest = new sn_ws.RESTMessageV2();
researchPaperRequest.setEndpoint('https://api.semanticscholar.org/graph/v1/paper/search?query=' + encodeURIComponent(searchQuery) + '&limit=1');
researchPaperRequest.setHttpMethod("GET");
researchPaperRequest.setRequestHeader("Accept", "application/json");
// Executing the API call
try {
var response = researchPaperRequest.execute();
var responseBody = response.getBody();
var responseJson = JSON.parse(responseBody);
// Extracting details of the first paper found
if (responseJson && responseJson.data && responseJson.data.length > 0) {
var paper = responseJson.data[0];
var title = paper.title || "Title not available";
var authors = "Authors not available";
if (paper.authors && paper.authors.length > 0) {
var authorNames = [];
for (var i = 0; i < paper.authors.length; i++) {
authorNames.push(paper.authors[i].name);
}
authors = authorNames.join(", ");
}
var year = paper.year || "Year not available";
var url = paper.url || "URL not available";
// Creating the message to send
var message = `Here's a research paper I found:\n\nTitle: ${title}\nAuthors: ${authors}\nYear: ${year}\nURL: ${url}`;
// Sending the response message
new x_snc_slackerbot.Slacker().send_chat(current, message, false);
} else {
// No paper found for the given search query
new x_snc_slackerbot.Slacker().send_chat(current, "Sorry, I couldn't find any research papers for your query.", false);
}
} catch (e) {
// Handling errors
new x_snc_slackerbot.Slacker().send_chat(current, "An error occurred while trying to find a research paper.", false);
}