This article describes how to send Slack notifications from Displayr...
...based on a table.
Requirements
- A Displayr document with an output showing the number of items you wish to show in your alert.
- A Slack channel to send the alert to.
- A Slack app (https://api.slack.com/apps) which uses Bots (under Add features and functionality) and has a valid OAuth token (found under OAuth & Permissions > Bot User OAuth Token).
Please note this requires the Data Stories module or a Displayr license.
Method
1. Select Calculation > Custom Code.
2. Paste the below into the R CODE section of the output:
flipTime::UpdateAt("01-01-2021 23:50", time.zone = "Australia/Sydney", units = "days", frequency = 1, options = "snapshot")
# Alert text
x = sign.up.list
n = NROW(x)
today = strftime(Sys.Date(), format="%A, %d %B %y")
alert = paste0("*",n,"* new sign-ups for ", today)
# Slack parameters
body = list(channel = "#your_channel",
text = alert,
username = "sign_up_alert",
icon_emoji = ":heavy_exclamation_mark:")
# API call
library(httr)
oauth_token = "your_oauth_token"
link = POST("https://slack.com/api/chat.postMessage",
add_headers(Authorization = paste("Bearer",oauth_token)),
body = body)
- In this example, we are using the flipTime R package to update once a day at 23:50 Sydney time. Next, we find the number of records from the sign-up table, and set up the alert text. Slack messaging allows us to also add basic tags to the text, e.g. * to make it bold, set content within blocks or lists.
- We then set the Slack parameters to accompany the alert text by defining the channel we want to send to (replace with your channel name), the intended username to appear when the message is sent, and the accompanying emoji icon (which you can find references for here).
- The final section makes the POST request of your alert message using your OAuth Token.
Next
Built-in Formatting and Markup for Messages in Slack
Creating Rich Message Layouts in Slack
How to Set Up a Slack Alert of Updating Errors from your Displayr account
How to Automatically Update Calculations, Variables and Data Sets Using R