-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5e7047
commit 866409f
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} |