-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.R
158 lines (130 loc) · 5.15 KB
/
bot.R
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
suppressPackageStartupMessages({
library(httr)
library(jsonlite)
library(purrr)
library(dplyr)
library(stringr)
library(rlist)
library(slackr)
library(stringdist)
library(googledrive)
library(readr)
})
# get command line secret inputs
args <- commandArgs(trailingOnly = TRUE)
# 1st: credentials for Google Drive
credentials <- args[[1]]
# File path on google drive:
file_path <- args[[2]]
# Slack bot token:
bottoken <- args[[3]]
# Sciwheel authentication token:
f1000auth <- args[[4]]
# Load the state from Drive
# authenticate to Google drive
drive_auth(path = rawToChar(base64enc::base64decode(credentials)), email = "[email protected]" )
# read the file from google drive
webhooks <- drive_read_string(file = file_path) %>% read_csv()
slackr_setup(token = bottoken)
users <- slackr_users()
# returns last date
lastDates <- webhooks %>% pmap_dbl(\(...) {
current <- data.frame(...)
templastDate <- current$lastDate
resp <- GET(
paste0(
"https://sciwheel.com/extapi/work/references?projectId=",
current$projectId, "&sort=addedDate:desc"
),
add_headers(Authorization = paste("Bearer", f1000auth))
)
print(paste(
"Sciwheel: ", Sys.time(), "channel", current$channel, "GET status",
resp$status_code
))
if (resp$status_code == 200) {
refs <- content(resp)$results %>%
list.filter(f1000AddedDate > templastDate) %>%
rev()
blocks <- refs %>% map(\(r) {
noteResp <- GET(
paste0("https://sciwheel.com/extapi/work/references/", r$id, "/notes?"),
add_headers(Authorization = paste("Bearer", f1000auth))
)
comments <- list.filter(content(noteResp), is.null(highlightText))
note <- ifelse(noteResp$status_code == 200 & length(comments) > 0,
paste0(list.sort(comments, created)[[1]]$comment, "\n"), ""
) %>% str_replace_all("@\\w+", \(name){
match <- amatch(
str_remove(name, "@") %>%
tolower(),
users %>%
pull(display_name_normalized) %>%
str_remove_all("\\s") %>%
tolower(),
method = "jw",
weight = c(d = 0.1, s = 1, i = 1, t = 1),
p = 0.1,
maxDist = 0.5)
if (is.na(match)) {
return(name)
} else {
paste0("<@", users %>% pluck("id", match), ">")
}
})
details <- paste0(
note,
r$authorsText, ". <", r$fullTextLink, "|", r$title, "> ",
r$journalName, ". ", r$publishedYear,
" - added by: ", r$f1000AddedBy,
ifelse(length(r$f1000Tags) > 0,
paste0(" - tags: ", paste0(r$f1000Tags, collapse = " ")),
""
)
)
if (nchar(details) > 3000) {
details <- paste0(
"Full information too long to display. ",
"<", r$fullTextLink, "|", r$title, "> ",
" - added by: ", r$f1000AddedBy
)
}
list(type = "section", text = list(type = "mrkdwn", text = details))
})
if (length(blocks) > 0) {
webhook <- as.character(current$webhook)
# one paper at a time with wait in between so we don't get rate limited
alive <- TRUE
respcodes <- blocks %>% map2_int(
refs %>% map(~ .x$f1000AddedDate),
\(block, addedDate) {
if (alive) {
Sys.sleep(1.05)
content <- toJSON(list(blocks = list(block)), auto_unbox = TRUE)
outcome <- POST(url = webhook, content_type_json(), body = content)
if (outcome$status_code == 200) {
if (addedDate > templastDate) {
templastDate <<- addedDate
}
} else {
alive <<- FALSE
}
outcome$status_code
} else {
-1
}
}
)
print(paste(Sys.time(), "POST status", paste(respcodes, collapse = ", ")))
}
}else{
stop("Sciwheel authentication failed, check the status code. If status code is 401, then someone needs to generate new sciwheel authentication token and update the github secret (f1000token).")
}
templastDate
})
if (sum(lastDates > webhooks$lastDate) > 0) {
webhooks$lastDate <- lastDates
# write the file back to google drive
webhooks %>% write_csv("./temp.csv")
drive_update(file = file_path, media = "./temp.csv")
}