Skip to content

Commit

Permalink
Merge pull request #51 from NCEAS/no_initial_email
Browse files Browse the repository at this point in the history
Accommodate cases where there is no initial email
  • Loading branch information
Jasmine authored Aug 25, 2021
2 parents baae674 + b91ec6a commit bbd70e8
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 356 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: awardsBot
Type: Package
Title: Periodic Correspondence Bot for NSF Award Recipients
Version: 1.0
Date: 2018-06-18
Date: 2021-08-12
Authors@R: c(
person("Dominic", "Mullen", email = "[email protected]", role = c("cre", "aut")),
person("Mitchell", "Maier", email = "[email protected]", role = c("ctb")),
Expand Down
44 changes: 25 additions & 19 deletions R/RT_check_correspondence.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,39 @@
#' }
get_recent_incoming_correspondence <- function(ticket_id, after) {
correspondences <- list()

req <- rt::rt_ticket_history(ticket_id, format = "s")

if (req$status != 200) {
out <- "Failed to log into RT"

slackr::slackr_bot(out)
stop(out)
}

# look for all instances with an email
ticket_history <- unlist(strsplit(req$body, "\\n"))
incoming <- ticket_history[stringr::str_detect(ticket_history, "Correspondence added by .+@.+")]

incoming <-
ticket_history[stringr::str_detect(ticket_history, "Correspondence added by .+@.+")]

if (length(incoming) == 0) {
return(correspondences)
}

for (inc in incoming) {

# get the specific correspondence
id <- stringr::str_extract(inc, "[0-9]*")

response <- rt::rt_ticket_history_entry(ticket_id, id)

if (response$Created <= after) {
next
}

correspondences <- append(correspondences, format_history_entry(response))

correspondences <-
append(correspondences, format_history_entry(response))
}

return(correspondences)
}

Expand All @@ -60,18 +62,20 @@ get_recent_incoming_correspondence <- function(ticket_id, after) {
#' format_history_entry(response)
#' }
format_history_entry <- function(msg, trunc_at = 200) {

if (msg["Type"] == "Correspond") {
msg["Type"] <- "Correspondence"
} else if (msg["Type"] == "Create") {
msg["Type"] <- "Ticket created"
}

# construct the slack message
sprintf(
"%s by %s on <%s/Ticket/Display.html?id=%s|Ticket %s>:\n>%s",
msg$Type, msg$Creator, Sys.getenv("RT_BASE_URL"),
msg$Ticket, msg$Ticket,
msg$Type,
msg$Creator,
Sys.getenv("RT_BASE_URL"),
msg$Ticket,
msg$Ticket,
stringr::str_trunc(stringr::str_remove_all(msg$Content, "\\n\\s+"), trunc_at)
)
}
Expand All @@ -89,9 +93,11 @@ format_history_entry <- function(msg, trunc_at = 200) {
#' get_tickets_with_new_incoming_correspondence("2021-05-03")
#' }
get_tickets_with_new_incoming_correspondence <- function(after) {
tickets <- rt::rt_ticket_search(paste0("Queue='arcticAwards' AND LastUpdated >'", after, " 00:00:00'"))

tickets <-
rt::rt_ticket_search(paste0("Queue='arcticAwards' AND LastUpdated >'", after, " 00:00:00'"))

if (nrow(tickets) > 0) {
correspondence <- lapply(tickets$id, get_recent_incoming_correspondence, after)
correspondence <-
lapply(tickets$id, get_recent_incoming_correspondence, after)
}
}
Loading

0 comments on commit bbd70e8

Please sign in to comment.