Tyki Wada

R Communication with Slack API

05 Oct 2019 tarihinde yayınlandı.

Much of the scripting I run happens overnight (nicer to sleep while you wait right?). But what if you know it never ended up finishing because of a bug?

I used to be a big fan of the cool tips and tricks of Automate the Boring Stuff (I still am!). However, times have changed and updates must be made. Slack, with over 10 million users, (as of June 2019) has gone direct public with a $15.7 billion valuation. I use slack, and you probably do too.

Let’s make ourselves a messaging system to ping us when our code is done.

If you get lost at any point, you can refer to https://github.com/hrbrmstr/slackr for some extra help.

First, make sure you have slackr. You don’t really have to have the slack app installed and ready to go, but it helps if you already do.

library(slackr)

Next object you need is a webhook and an API token. You technically don’t need a webhook, however Slack has proclaimed that the API token is a legacy tool that may someday become obsolete. So, it’ll be nice to know how to get both (webhooks are faster and more reliable anyways). Keep in mind that you do NOT need the webhook for this app to work. First, to create a webhook, click here to get it. What you will do here is create a slack app that will post in a certain channel. Now, the API key is much simpler. Simply scroll down the page and access your api token here. Not too bad huh?

Once you get them, you can assign them as variables to prepare for the next step.

whurl = 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'
apitk = 'xoxo-...'

Now, there are two options. The annoying way, or the easy way. Let’s just stick to the easy way. The conventional method requires you to create a .dcf file (pretty much a JSON) and record your username, channel, api key, etc. However, slackr_setup() can take care of that for you. Just fill in the blanks below with the required details and

whurl = "https://hooks.slack.com/services/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"
apitk = "xoxp-390852287680-733442795302-772178717717-fa0a2a0838aa09e3d6c5ccfc34cb9a7d"

slackr_setup(channel = "finished", username = '@Tyki Wada',
             icon_emoji = ":thumbsup_all:", incoming_webhook_url = whurl,
             api_token = apitk, config_file = "~/.slackr", echo = TRUE)
## {
##   "SLACK_CHANNEL": ["finished"],
##   "SLACK_USERNAME": ["@Tyki Wada"],
##   "SLACK_ICON_EMOJI": [":thumbsup_all:"],
##   "SLACK_INCOMING_URL_PREFIX": ["https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"],
##   "SLACK_API_TOKEN": ["xoxo-..."]
## } 

And this is pretty much the end of the setup. Now you are good to post messages however you like. How about your first hello world?

slackr("Hah, so easy!")

You can always stick it at the end of your script to let you know when you complete your process.

start_time <- Sys.time()

library(MASS)
library(parsnip)

logistic_glm <-
  logistic_reg(mode = "classification") %>%
  set_engine("glm") %>%
  fit(type ~ ., data = Pima.te)

end_time <- Sys.time()


out <- sprintf("Code Completed in %f seconds", end_time - start_time)
slackr(out)

Now, we see the utility here, but just know that this isn’t a single use tool. The real VALUE for this is not only a reminder for individuals to receive notifications, but to share information of various kinds.

For example, you can even show the out contents of the logistic regression and other summary items and send it to your group.

slackr(logistic_glm)
library(skimr)
slackr(skimr::skim(Pima.te))

The histograms in skimr didn’t come out so great.. but did I just say plots? Pshh please. Easy. (Base plots work too, but a bit different)

library(ggplot2)
ggslackr(qplot(age, bmi, data=Pima.te))

Now we see that even functional/regular reports may be written up and sent via slack to whoever to see. Any information that we need to send is at our fingertips. Go impress your boss or do something worth your time.

Good luck on your next project!