diff --git a/DESCRIPTION b/DESCRIPTION index 04c7226..0493ad3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: twilio Type: Package Title: An interface to the Twilio API for R -Version: 0.1.0.9008 +Version: 0.1.0.9012 Authors@R: person("Sean", "Kross", , "sean@seankross.com", c("aut", "cre")) Description: Send and retrieve text and picture messages using the Twilio API. Depends: @@ -9,7 +9,9 @@ Depends: Imports: purrr, httr, - jsonlite + jsonlite, + magrittr, + lubridate License: MIT + file LICENSE URL: http://github.com/seankross/twilio Encoding: UTF-8 diff --git a/NAMESPACE b/NAMESPACE index 9999f19..5566d8a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ S3method(print,twilio_media) S3method(print,twilio_message) export(get_message_media) export(get_messages) +export(message_tbl) export(send_message) importFrom(httr,GET) importFrom(httr,POST) @@ -12,5 +13,10 @@ importFrom(httr,content) importFrom(httr,http_type) importFrom(httr,modify_url) importFrom(httr,status_code) +importFrom(httr,user_agent) importFrom(jsonlite,fromJSON) +importFrom(lubridate,parse_date_time) +importFrom(magrittr,"%<>%") +importFrom(magrittr,"%>%") importFrom(purrr,map) +importFrom(purrr,map_if) diff --git a/R/get_message_media.R b/R/get_message_media.R index 6327adc..52b1607 100644 --- a/R/get_message_media.R +++ b/R/get_message_media.R @@ -3,7 +3,7 @@ #' @param message_sid An SID for a message that contains media. #' @return A list containing media information. #' @importFrom jsonlite fromJSON -#' @importFrom httr modify_url GET authenticate http_type content +#' @importFrom httr modify_url GET authenticate http_type content user_agent #' @importFrom purrr map #' @export #' @examples diff --git a/R/get_messages.R b/R/get_messages.R index 67068a6..df0b816 100644 --- a/R/get_messages.R +++ b/R/get_messages.R @@ -4,7 +4,7 @@ #' @param page_size The number of messages per page. The maximum number allowed is 1000. #' @return A \code{twilio_message_log} object. #' @importFrom jsonlite fromJSON -#' @importFrom httr modify_url GET authenticate http_type content +#' @importFrom httr modify_url GET authenticate http_type content user_agent #' @importFrom purrr map #' @export #' @examples diff --git a/R/message_tbl.R b/R/message_tbl.R new file mode 100644 index 0000000..1b5ac1e --- /dev/null +++ b/R/message_tbl.R @@ -0,0 +1,39 @@ +#' Make a Data Frame from Message Logs +#' +#' @param message_log An S3 object with the class \code{twilio_message_log}. Likely +#' the result of a call to \code{\link{get_messages}}. +#' @return A data frame. +#' +#' @importFrom magrittr %>% %<>% +#' @importFrom purrr map map_if +#' @importFrom lubridate parse_date_time +#' @export +#' @examples +#' \dontrun{ +#' +#' # Set API credentials +#' # You only need to do this once per R session +#' Sys.setenv(TWILIO_SID = "M9W4Ozq8BFX94w5St5hikg7UV0lPpH8e56") +#' Sys.setenv(TWILIO_TOKEN = "483H9lE05V0Jr362eq1814Li2N1I424t") +#' +#' # Get messages sent to your account +#' messages <- get_messages() +#' +#' # Create data frame from log +#' sms_data <- message_tbl(messages) +#' +#' } +message_tbl <- function(message_log){ + stopifnot(identical(class(message_log), "twilio_message_log")) + + raw_log <- as.data.frame(do.call(rbind, message_log), stringsAsFactors = FALSE) + raw_log$date_created %<>% map(parse_date_time, orders = "%a %d %b %Y %H:%M:%S %z") + raw_log$date_created <- do.call(c, raw_log$date_created) + raw_log$date_updated %<>% map(parse_date_time, orders = "%a %d %b %Y %H:%M:%S %z") + raw_log$date_updated <- do.call(c, raw_log$date_updated) + raw_log$date_sent %<>% map(parse_date_time, orders = "%a %d %b %Y %H:%M:%S %z") + raw_log$date_sent <- do.call(c, raw_log$date_sent) + raw_log$error_code %<>% map(function(x){ifelse(is.null(x), NA, x)}) %>% unlist() + raw_log$error_message %<>% map(function(x){ifelse(is.null(x), NA, x)}) %>% unlist() + raw_log %>% map_if(is.list, unlist) %>% as.data.frame(stringsAsFactors = FALSE) +} diff --git a/R/messages_tbl.R b/R/messages_tbl.R deleted file mode 100644 index 0216ac9..0000000 --- a/R/messages_tbl.R +++ /dev/null @@ -1,4 +0,0 @@ -message_log_tbl <- function(message_log){ - stopifnot(identical(class(message_log), "twilio_message_log")) - as.data.frame(do.call(rbind, message_log), stringsAsFactors = FALSE) -} diff --git a/README.md b/README.md index c4ccfc5..39dde56 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ You should only need to do this once per session. ```r Sys.setenv(TWILIO_SID = "M9W4Ozq8BFX94w5St5hikg7UV0lPpH8e56") Sys.setenv(TWILIO_TOKEN = "483H9lE05V0Jr362eq1814Li2N1I424t") + +library(twilio) ``` ### Send a Text Message @@ -39,7 +41,8 @@ send_message("2125557634", "9178675903", media_url = "https://www.r-project.org/ ### Send a Picture Message with Text ```r -send_message("2125557634", "9178675903", "Do you like the new logo?", "https://www.r-project.org/logo/Rlogo.png") +send_message("2125557634", "9178675903", "Do you like the new logo?", + "https://www.r-project.org/logo/Rlogo.png") ``` ### Get Message Logs @@ -48,9 +51,15 @@ send_message("2125557634", "9178675903", "Do you like the new logo?", "https://w messages <- get_messages() ``` +### Makes Logs into a Data Frame + +```r +sms_data <- message_tbl(messages) +``` + ### Get Media from a Message ```r -media <- get_message_media(messages[[1]]$sid) +media <- get_message_media(sms_data$sid[1]) browseURL(media[[1]]$url) ``` diff --git a/man/message_tbl.Rd b/man/message_tbl.Rd new file mode 100644 index 0000000..efaf6f1 --- /dev/null +++ b/man/message_tbl.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/message_tbl.R +\name{message_tbl} +\alias{message_tbl} +\title{Make a Data Frame from Message Logs} +\usage{ +message_tbl(message_log) +} +\arguments{ +\item{message_log}{An S3 object with the class \code{twilio_message_log}. Likely +the result of a call to \code{\link{get_messages}}.} +} +\value{ +A data frame. +} +\description{ +Make a Data Frame from Message Logs +} +\examples{ +\dontrun{ + +# Set API credentials +# You only need to do this once per R session +Sys.setenv(TWILIO_SID = "M9W4Ozq8BFX94w5St5hikg7UV0lPpH8e56") +Sys.setenv(TWILIO_TOKEN = "483H9lE05V0Jr362eq1814Li2N1I424t") + +# Get messages sent to your account +messages <- get_messages() + +# Create data frame from log +twilio_df <- message_tbl(messages) + +} +} +