Slack bots6/15/2023 ![]() Then I remembered the R plumber package, which can turn any R function into an API. But they all seemed more complex to install and set up than was worthwhile just to run a simple Slack command. 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. 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. Mytext <- paste0("Price for ", thesymbol, " is $", myresults, " as of ", myresults) Mytext <- paste0("A price is not available for ", thesymbol) Myresults <- quantmod::getQuote(thesymbol) 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. 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. ![]() 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. Results come back as an R data frame: Trade Time Last Change % Change Open High Low Volume For example, quantmod::getQuote("MSFT") returns the latest Microsoft stock price. ![]() Install the quantmod package with install.packages("quantmod") and then run quantmod::getQuote("stocksymbol"). ![]() Getting a current stock price in R is laughably easy. ![]() The same techniques can be used for any other type of information you can import and process with R. Here's how I created a custom Slack slash command bot in R to retrieve current stock prices. ![]()
0 Comments
Leave a Reply. |