Send result of R expressions to a Slack channel via webhook API

Share:

Description

Takes an expr, evaluates it and sends the output to a Slack chat destination via the webhook API. Useful for logging, messaging on long compute tasks or general information sharing.

Usage

1
2
3
4
5
6
7
8
9
slackr_bot(..., channel = Sys.getenv("SLACK_CHANNEL"),
  username = Sys.getenv("SLACK_USERNAME"),
  icon_emoji = Sys.getenv("SLACK_ICON_EMOJI"),
  incoming_webhook_url = Sys.getenv("SLACK_INCOMING_URL_PREFIX"))

slackrBot(..., channel = Sys.getenv("SLACK_CHANNEL"),
  username = Sys.getenv("SLACK_USERNAME"),
  icon_emoji = Sys.getenv("SLACK_ICON_EMOJI"),
  incoming_webhook_url = Sys.getenv("SLACK_INCOMING_URL_PREFIX"))

Arguments

...

expressions to be sent to Slack.com

channel

which channel to post the message to (chr)

username

what user should the bot be named as (chr)

icon_emoji

what emoji to use (chr) "" will mean use the default

incoming_webhook_url

which slack.com API endpoint URL to use (see section Webhook URLs for details)

Details

By default, everyting but expr will be looked for in a "SLACK_" environment variable. You can override or just specify these values directly instead, but it's probably better to call slackr_setup first.

This function uses the incoming webhook API. The webhook will have a default channel, username, icon etc, but these can be overridden.

Webhook URLs

Webhook URLs look like:

  • https://hooks.slack.com/services/XXXXX/XXXXX/XXXXX

OLD STYLE WEBHOOKS ARE NO LONGER SUPPORTED

Note

You need a https://www.slack.com account and will also need to setup an incoming webhook: https://api.slack.com/. Old style webhooks are no longer supported.

See Also

slackrSetup, slackr, dev_slackr, save_slackr, slackr_upload

Examples

1
2
3
4
5
6
7
8
9
## Not run: 
slackr_setup()
slackr_bot("iris info", head(iris), str(iris))

# or directly
slackr_bot("Test message", username="slackr", channel="#random",
  incoming_webhook_url="https://hooks.slack.com/services/XXXXX/XXXXX/XXXXX")

## End(Not run)