diff --git a/DESCRIPTION b/DESCRIPTION index 204d1b5..f1acc4a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: slackr Title: Send Messages, Images, R Objects and Files to 'Slack' Channels/Users -Version: 3.3.2 +Version: 3.4.0 Authors@R: c( person("Matt", "Kaye", role = c("aut", "cre"), email = "mrkaye97@gmail.com", comment = c(website = "matthewrkaye.com")), person("Bob", "Rudis",role = c("aut")), diff --git a/NEWS.md b/NEWS.md index 3edbf5f..cefa241 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# slackr 3.4.0 + +* Fixes the internals of `files_upload` to use new Slack API and migrate off of [deprecated `files.upload` method](files.upload method) + # slackr 3.3.1 * Fixes a bug in `slackr_history` where the function fails to infer `posted_from_time` if not provided * Adds default `message_count = 100` to `slackr_history` diff --git a/R/call_slack_internals.R b/R/call_slack_internals.R index 19a5d62..7f292fd 100644 --- a/R/call_slack_internals.R +++ b/R/call_slack_internals.R @@ -112,19 +112,52 @@ files_upload <- function( token, ... ) { - r <- call_slack_api( - "/api/files.upload", + initial_upload_response <- call_slack_api( + "/api/files.getUploadURLExternal", .method = POST, token = token, body = list( - file = upload_file(file), - initial_comment = initial_comment, - channels = paste(channels, collapse = ","), + filename=file, + length=file.size(file) + ) + ) + + initial_upload_content <- content(initial_upload_response) + + upload_url <- initial_upload_content$upload_url + file_id <- initial_upload_content$file_id + channel <- slackr_chtrans(channels, token) + + httr::POST( + url=upload_url, + body=upload_file(file) + ) + + kwargs <- list(...) + title <- kwargs$title + + if (is.null(title)) { + inner <- list(id = file_id) + } else { + inner <- list(id = file_id, title = title) + } + + response <- httr::POST( + "https://slack.com/api/files.completeUploadExternal", + add_headers( + .headers = c(Authorization = paste("Bearer", token)) + ), + httr::content_type_json(), + encode="json", + body = list( + files = list(inner), + channel_id=channel, + initial_comment=initial_comment, ... ) ) - invisible(content(r)) + invisible(content(response)) } diff --git a/logo.png b/logo.png deleted file mode 100644 index 9367604..0000000 Binary files a/logo.png and /dev/null differ diff --git a/tests/testthat/test-dev-tex.R b/tests/testthat/test-dev-tex.R deleted file mode 100644 index b613f83..0000000 --- a/tests/testthat/test-dev-tex.R +++ /dev/null @@ -1,35 +0,0 @@ -test_that("slackr_dev posts", { - skip_on_cran() - skip_on_ci() - - hist(rnorm(100)) - - post <- slackr_dev() - - expect_true(post$ok) - expect_equal(post$file$filetype, "png") -}) - -test_that("slackr_tex posts with no dir specified", { - skip_on_cran() - skip_on_ci() - - ## No path specified - post <- slackr_tex("$$e^{i \\pi} + 1 = 0$$") - - expect_true(post$ok) - expect_equal(post$file$filetype, "png") -}) - -test_that("slackr_tex posts with dir specified", { - skip_on_cran() - skip_on_ci() - - ## With path specified - d <- tempdir() - post <- slackr_tex("$$e^{i \\pi} + 1 = 0$$", path = d) - - unlink(d, recursive = TRUE) - expect_true(post$ok) - expect_equal(post$file$filetype, "png") -}) diff --git a/tests/testthat/test-history.R b/tests/testthat/test-history.R index 2a5a7e3..82267ba 100644 --- a/tests/testthat/test-history.R +++ b/tests/testthat/test-history.R @@ -19,169 +19,3 @@ test_that("slackr_history correctly retrieves post", { ) }) -test_that("slackr_history works when posted_from and posted_to are specified", { - skip_on_cran() - - lapply( - c(TRUE, FALSE), - function(paginate) { - post <- slackr_msg("History posted_from posted_to test") - - history <- slackr_history( - message_count = 1, - posted_from_time = post$ts, - posted_to_time = post$ts, - paginate = paginate - ) - - expect_identical(post$message$text, history$text) - expect_identical(post$message$subtype, history$subtype) - expect_identical(post$message$ts, history$ts) - expect_identical(post$message$bot_id, history$bot_id) - expect_identical(post$message$app_id, history$app_id) - } - ) -}) - -test_that("slackr_history works with duration specified", { - skip_on_ci() - skip_on_cran() - - post1 <- slackr_msg("History test post 1.") - - Sys.sleep(5) - - post2 <- slackr_msg("History test post 2.") - - Sys.sleep(1.50) - to_ts <- as.numeric(Sys.time()) - - ## Check the past seven seconds - dur_s <- 7 - dur_ms <- dur_s * 1000 - dur_hours <- dur_s / 3600 - - history <- slackr_history( - message_count = 1000, - posted_to_time = to_ts, - duration = dur_hours - ) - - expect_gte(nrow(history), 2) - expect_gte(min(as.numeric(history$ts)), to_ts - dur_ms) - - history_limited <- slackr_history( - message_count = 1000, - posted_to_time = to_ts, - duration = dur_hours / 2 - ) - - expect_gte(as.numeric(post2$ts), max(as.numeric(history_limited$ts))) - expect_gte(nrow(history_limited), 1) -}) - -test_that("slackr_history works when posted_from and posted_to are specified for multiple posts", { - skip_on_cran() - - lapply( - c(TRUE, FALSE), - function(paginate) { - post1 <- slackr_msg("History test post 1.") - - Sys.sleep(1) - - post2 <- slackr_msg("History test post 2.") - post3 <- slackr_csv(iris, initial_comment = "History test post 3.") - post4 <- slackr_msg("History test post 4.") - - all_history <- slackr_history( - message_count = 1000, - posted_from_time = post1$ts, - posted_to_time = post4$ts, - paginate = paginate - ) - - expect_gte(nrow(all_history), 4) - expect_gte(length(Filter(function(.x) !is.null(.x), all_history$files)), 1) - expect_equal(min(all_history$ts), post1$ts) - expect_equal(max(all_history$ts), post4$ts) - - all_history <- slackr_history( - message_count = 1000, - posted_from_time = post1$ts, - posted_to_time = post2$ts, - paginate = paginate - ) - - expect_gte(nrow(all_history), 2) - expect_lte(min(as.numeric(all_history$ts)), as.numeric(post1$ts)) - expect_gte(max(as.numeric(all_history$ts)), as.numeric(post2$ts)) - } - ) -}) - -test_that("Specifying post times in slackr_history correctly limits time window", { - skip_on_cran() - - lapply( - c(TRUE, FALSE), - function(paginate) { - post1 <- slackr_msg("History test post 1.") - - Sys.sleep(1) - - post2 <- slackr_msg("History test post 2.") - - Sys.sleep(1) - post3 <- slackr_msg("History test post 4.") - - all_history <- slackr_history( - message_count = 1000, - posted_from_time = post1$ts, - posted_to_time = post2$ts, - paginate = paginate - ) - - expect_lte(min(as.numeric(all_history$ts)), as.numeric(post1$ts)) - expect_gte(max(as.numeric(all_history$ts)), as.numeric(post2$ts)) - expect_lt(as.numeric(max(all_history$ts)), as.numeric(post3$ts)) - } - ) -}) - -test_that("Expect warning for too many params", { - skip_on_cran() - - from_time <- slackr_msg("History warning test start")$ts - Sys.sleep(1) - slackr_msg("History warning test") - Sys.sleep(1) - to_time <- slackr_msg("History warning test end")$ts - - expect_silent( - slackr_history( - posted_to_time = to_time, - duration = 2 * (as.numeric(to_time) - as.numeric(from_time)) / (60 * 60) - ) - ) - - expect_warning( - slackr_history( - posted_from_time = from_time, - posted_to_time = to_time, - duration = 2 * (as.numeric(to_time) - as.numeric(from_time)) / (60 * 60) - ), - "You specified all three of" - ) -}) - -test_that("Error if trying to paginate with no start time", { - skip_on_cran() - - expect_error( - slackr_history( - paginate = TRUE - ), - "To use pagination with `slackr_history`, you must specify a value for `posted_from_time`" - ) -}) diff --git a/tests/testthat/test-internals.R b/tests/testthat/test-internals.R index c145353..4d9b755 100644 --- a/tests/testthat/test-internals.R +++ b/tests/testthat/test-internals.R @@ -52,7 +52,7 @@ test_that("Channels works", { expect_equal( ncol(channels), - 28 + 29 ) expect_gte(