Last active
August 10, 2018 13:20
-
-
Save jankuc/b5d02c9e1a9da9d4a9de92f9ecb1c188 to your computer and use it in GitHub Desktop.
Gist showing how to post message from R to slack channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(jsonlite) | |
library(httr) | |
library(R6) | |
library(curl) | |
# Create Slack configuration object | |
slack_conf <- list( | |
incoming_webhook = "", | |
icon_url = "http://breakingapis.org/assets/vis/logo_cran.png", | |
bot_username = "R notification", | |
channel = "#notifications" | |
) | |
post_to_slack <- function(text, channel = slack_conf$channel, username = slack_conf$bot_username, | |
webhook_url = slack_conf$incoming_webhook, icon_url = slack_conf$icon_url) { | |
tryCatch({ | |
res <- POST(url = webhook_url, | |
body = toJSON(list(text = text, | |
channel = channel, | |
username = username, | |
icon_url = icon_url), auto_unbox = TRUE)) | |
if (res$status_code != 200) { | |
stop() | |
} | |
}, error = function(e) { | |
print(paste(c("Could not send message", paste0("'", text, "'"), "to slack channel", channel), collapse = " ")) | |
}) | |
} | |
post_to_slack("Hello World.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment