R/dev-helpers.R

Defines functions text_me ifttt_me

text_me <- function(body = NULL, to = "+18019410791", from = "+12055965163", media_url = NULL)
{
  if (is.null(body) && is.null(media_url)) {
    stop("Please specify body, media_url, or both.", call. = FALSE)
  }
  url <- sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json",
                 keyring::key_get("twilio-sid"))
  # ua <- user_agent("https://github.com/seankross/twilio")
  # path <- paste("2010-04-01", "Accounts", keyring::key_get("twilio-sid"),
                # "Messages.json", sep = "/")
  # url <- httr::modify_url(base_url, path = path)
  resp <- httr::POST(url,
                     # ua,
                     httr::authenticate(keyring::key_get("twilio-sid"),
                                  keyring::key_get("twilio-token")),
                     body = list(To = to, From = from, Body = body,
                                 MediaUrl = media_url))
  if (httr::http_type(resp) != "application/json") {
    stop("Twilio API did not return JSON.", call. = FALSE)
  }
  # parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"),
                     # simplifyVector = FALSE)
  # check_status(resp)
  httr::http_status(resp)
}

ifttt_me <- function(message = "R", custom = NULL) {
  IFTTT_url <- paste0("https://maker.ifttt.com/trigger/r_task_complete/with/key/",
                      keyring::key_get("ifttt-webhooks"))
  if (!is.null(custom)) {
    message <- custom
  } else {
    message <- paste("Your R task", message, "completed")
  }

  httr::POST(url = IFTTT_url, body = list(value1 = message),
             httr::use_proxy("http://localhost", port = 3128))

}
danielbride/dfunc documentation built on Jan. 1, 2020, 12:54 a.m.