Create your own Slack bots and web APIs in R

This step-by-step tutorial will show you how to find a current stock price, but the basic framework can be used for all sorts of data.

Create your own Slack bots -- and web APIs -- in R
Thinkstock

One of Slack’s greatest appeals is that it’s so extensible. While its main purpose is group collaboration, add-on apps can do everything from answer questions about business analytics to offer project updates from tools like Jira and Trello.

And while there are dozens of pre-configured Slack bots from commercial third parties and open-source developers, it’s not difficult to code your own.

There are already tutorials on how to do this in PHP and Python. But I wanted to take advantage of some R functions that easily import data about finance, weather, government information and more, and add some of that to what Slack calls “custom slash commands.” These allow users to get responses to queries with simple formats like /weather or /stockprice.

Here’s how I created a custom Slack slash command bot in R to retrieve current stock prices. The same techniques can be used for any other type of information you can import and process with R.

Step 1: Find stock prices with R

Getting a current stock price in R is laughably easy. Install the quantmod package with install.packages(“quantmod”) and then run quantmod::getQuote(“stocksymbol”). For example, quantmod::getQuote(“MSFT”) returns the latest Microsoft stock price. Results come back as an R data frame:

              Trade Time   Last Change % Change  Open  High   Low   Volume
MSFT 2017-01-31 03:38:00 64.575 -0.555  -0.852% 64.86 65.15 64.26 11326241

I’d suggest turning this into a text response so it’s easier for the user to see the most recent price and the change. The first item in the results data frame is the time of the stock price being reported; the second item (the “Last” column) is that most recent available price. Along with returning the price, you’d probably want to add in a bit of error checking so there’s a human-readable response in case a price isn’t available. Here’s a simple R function to do this:


newGetQuote <- function(thesymbol){
  myresults <- quantmod::getQuote(thesymbol)
  if(is.na(myresults[1,1])){
    mytext <- paste0(“A price is not available for “, thesymbol)
  } else {
    mytext <- paste0(“Price for “, thesymbol, “ is $”, myresults[1,2], “ as of “, myresults[1,1])
  }
  
  return(mytext)
}

Now we’ll need to turn this function into something that another service such as Slack can query—in other words, an API that’s available via URL.

Step 2: Set up an API in R

I looked into several ways to make R scripts executable from a URL, such as using the FastRWeb project or running R via PHP and shell scripts. But they all seemed more complex to install and set up than was worthwhile just to run a simple Slack command. Then I remembered the R plumber package, which can turn any R function into an API. Plumber was created by Jeff Allen, an RStudio software engineer and head of a start-up called Trestle Technology, and it’s a very easy and elegant way to create an API with R.

This is easy to set up locally. Install the package with install.packages(“plumber”), and then add the following code on the line directly above the start of your function: #* @get /stockquote.

The newGetQuote function now looks like this:

 #* @get /stockquote 
  newGetQuote <- function(thesymbol){
  myresults <- quantmod::getQuote(thesymbol) 
  if(is.na(myresults[1,1])){ 
    mytext <- paste0(“A price is not available for “, thesymbol) 
  } else { 
   mytext <- paste0(“Price for “, thesymbol, “ is $”, myresults[1,2], “ as of “, myresults[1,1])
  } 
  return(mytext) 
  } 

The #* is a special plumber comment to turn the function into an API endpoint. @get means it should be an HTTP GET request for reading data. Plumber also supports @post, @put and @delete. The /stockquote after @get specifies what path the API should respond to. Using stockquote means that the API URL format will be http://www.myurl.com/stockquote. The character string after the / could have been stockprice, myquotes or anything else.

Save that as an R file. (I called mine stockfunction.R, but you can name it anything.)

A second R file should contain code to launch a plumber server and run the stockfunction.R file (or whatever you called the file with the function you want to API-ize). This second file loads the plumber library, creates a plumber object from the stockfunction.R code with the plumb() function, and sets the port that the server will listen on. That code looks like:


library(plumber)
plumbobject <- plumb(“stockfunction.R”)
plumbobject$run(port=8000)

If you execute all the code in this second file, which I call myserver.R, and then go to the URL http://localhost:8000/stockquote?thesymbol=MSFT in a browser, you should see the stock price in your browser.

Next, we need to deploy this code to a system where Slack can access it.

1 2 Page 1
Page 1 of 2