Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slackrIms #13

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(slackrBot)
export(slackrChTrans)
export(slackrChannels)
export(slackrGroups)
export(slackrIms)
export(slackrSetup)
export(slackrUpload)
export(slackrUsers)
Expand Down
43 changes: 37 additions & 6 deletions R/slackr.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ dev.slackr <- function(channels=Sys.getenv("SLACK_CHANNEL"), ...,
dev.copy(png, file=ftmp, ...)
dev.off()

modchan <- slackrChTrans(channels)
modchan <- slackrChTrans(channels, ims=TRUE)

POST(url="https://slack.com/api/files.upload",
add_headers(`Content-Type`="multipart/form-data"),
Expand Down Expand Up @@ -316,7 +316,7 @@ ggslackr <- function(plot=last_plot(), channels=Sys.getenv("SLACK_CHANNEL"), sca
ftmp <- tempfile("ggplot", fileext=".png")
ggsave(filename=ftmp, plot=plot, scale=scale, width=width, height=height, units=units, dpi=dpi, limitsize=limitsize, ...)

modchan <- slackrChTrans(channels)
modchan <- slackrChTrans(channels, ims=TRUE)

POST(url="https://slack.com/api/files.upload",
add_headers(`Content-Type`="multipart/form-data"),
Expand Down Expand Up @@ -348,7 +348,7 @@ save.slackr <- function(..., channels="",
ftmp <- tempfile(file, fileext=".rda")
save(..., file=ftmp)

modchan <- slackrChTrans(channels)
modchan <- slackrChTrans(channels, ims=TRUE)

POST(url="https://slack.com/api/files.upload",
add_headers(`Content-Type`="multipart/form-data"),
Expand Down Expand Up @@ -382,7 +382,7 @@ slackrUpload <- function(filename, title=basename(filename),

Sys.setlocale('LC_ALL','C')

modchan <- slackrChTrans(channels)
modchan <- slackrChTrans(channels, ims=TRUE)

POST(url="https://slack.com/api/files.upload",
add_headers(`Content-Type`="multipart/form-data"),
Expand All @@ -402,13 +402,19 @@ slackrUpload <- function(filename, title=basename(filename),
#'
#' @param channels vector of channel names to parse
#' @param api_token the slack.com full API token (chr)
#' @param ims Should the IM ids be used instead of the user ids?
#' Defaults to FALSE, which is how the function behaved through version 1.2.3
#' @note Renamed from \code{slackr_chtrans}
#' @return character vector - original channel list with \code{#} or \code{@@} channels replaced with ID's.
#' @export
slackrChTrans <- function(channels, api_token=Sys.getenv("SLACK_API_TOKEN")) {
slackrChTrans <- function(channels, api_token=Sys.getenv("SLACK_API_TOKEN"), ims=FALSE) {

chan <- slackrChannels(api_token)
users <- slackrUsers(api_token)
if ( ims ) {
users <- slackrIms(api_token)
} else {
users <- slackrUsers(api_token)
}
groups <- slackrGroups(api_token)

chan$name <- sprintf("#%s", chan$name)
Expand Down Expand Up @@ -447,6 +453,31 @@ slackrUsers <- function(api_token=Sys.getenv("SLACK_API_TOKEN")) {

}

#' Get a data frame of slack.com IM ids
#'
#' need to setup a full API token (i.e. not a webhook & not OAuth) for this to work
#'
#' @param api_token the slack.com full API token (chr)
#' @return data.table of im ids and user names
#' @export
slackrIms <- function(api_token=Sys.getenv("SLACK_API_TOKEN"))
{
Sys.setlocale('LC_ALL','C')
tmp <- POST("https://slack.com/api/im.list", body=list(token=api_token))
tmp_p <- content(tmp, as="parsed")
ims <- rbindlist(lapply(tmp_p$ims, function(x) {
data.frame(user=x$user, im=x$id)
}))

users <- slackrUsers(api_token)
setnames(users,'id','user')
ims <- merge(ims, users, by='user')
setnames(ims,'im','id')
setcolorder(ims, c('id','name','user','real_name'))

return(ims)
}

#' Get a data frame of slack.com channels
#'
#' need to setup a full API token (i.e. not a webhook & not OAuth) for this to work
Expand Down
6 changes: 5 additions & 1 deletion man/slackrChTrans.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
\alias{slackrChTrans}
\title{Translate vector of channel names to channel ID's for API}
\usage{
slackrChTrans(channels, api_token = Sys.getenv("SLACK_API_TOKEN"))
slackrChTrans(channels, api_token = Sys.getenv("SLACK_API_TOKEN"),
ims = FALSE)
}
\arguments{
\item{channels}{vector of channel names to parse}

\item{api_token}{the slack.com full API token (chr)}

\item{ims}{Should the IM ids be used instead of the user ids?
Defaults to FALSE, which is how the function behaved through version 1.2.3}
}
\value{
character vector - original channel list with \code{#} or \code{@} channels replaced with ID's.
Expand Down
18 changes: 18 additions & 0 deletions man/slackrIms.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/slackr.R
\name{slackrIms}
\alias{slackrIms}
\title{Get a data frame of slack.com IM ids}
\usage{
slackrIms(api_token = Sys.getenv("SLACK_API_TOKEN"))
}
\arguments{
\item{api_token}{the slack.com full API token (chr)}
}
\value{
data.table of im ids and user names
}
\description{
need to setup a full API token (i.e. not a webhook & not OAuth) for this to work
}

19 changes: 19 additions & 0 deletions tests/testthat/test-slackrChChans.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
context("slackrChTrans")
slackrSetup(config_file='~/Dropbox/git/slackr/config.txt')

test_that("returned object looks as expected", {
id <- slackrChTrans('#general')
expect_is(id, 'character')
expect_identical(length(id), 1L)
})

test_that("returns input if not found", {
channel <- "aaa___zzz" # garbage name
id <- slackrChTrans(channel)
expect_identical(id,channel)
})

test_that("works with multiple channels", {
ids <- slackrChTrans(c('#general','#random'))
expect_identical(length(ids), 2L)
})
9 changes: 9 additions & 0 deletions tests/testthat/test-slackrChannels.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
context("slackrChannels")
slackrSetup(config_file='~/Dropbox/git/slackr/config.txt')

test_that("returned object looks as expected", {
channels <- slackrChannels()
expect_is(channels, 'data.table')
expect_less_than(0, nrow(channels))
expect_identical(names(channels),c('id','name','is_member'))
})
9 changes: 9 additions & 0 deletions tests/testthat/test-slackrGroups.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
context("slackrGroups")
slackrSetup(config_file='~/Dropbox/git/slackr/config.txt')

test_that("returned object looks as expected", {
groups <- slackrGroups()
expect_is(groups, 'data.table')
expect_less_than(0, nrow(groups))
expect_identical(names(groups),c('id','name','is_archived'))
})
9 changes: 9 additions & 0 deletions tests/testthat/test-slackrIms.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
context("slackrIms")
slackrSetup(config_file='~/Dropbox/git/slackr/config.txt')

test_that("returned object looks as expected", {
ims <- slackrIms()
expect_is(ims, 'data.table')
expect_less_than(0, nrow(ims))
expect_identical(names(ims),c('id','name','user','real_name'))
})
9 changes: 9 additions & 0 deletions tests/testthat/test-slackrUsers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
context("slackrUsers")
slackrSetup(config_file='~/Dropbox/git/slackr/config.txt')

test_that("returned object looks as expected", {
users <- slackrUsers()
expect_is(users, 'data.table')
expect_less_than(0, nrow(users))
expect_identical(names(users),c('id','name','real_name'))
})