如何使用Slackr改善Slack消息的格式?

时间:2018-09-03 23:30:47

标签: r slack

我正在使用Slackr将警报消息发送到Slack频道。它工作得很好,除了消息格式不是很好,我想对其进行改进。

install_github("hrbrmstr/slackr")
library(slackr)
slackr_setup(channel="#alerts", username="Mark Davis", 
         incoming_webhook_url = "https://hooks.slack.com/services/T31P8UDAB/BCH4HKQSC/*********", 
         api_token = "*********", echo = F)

alert="On Monday, 2018-09-03 @ 2pm Pacific..."
slackr(alert)

以下是slackr中的消息在Slack中的显示示例:

enter image description here

以下是我希望它看起来如何的一个示例:

enter image description here

slackr在格式化方面似乎没有很多选择。我当时想构建图像并将其插入,但是在使用R从文本文件构建图像时遇到了麻烦。

也许我可以调用另一个api来接收我的文本并将其格式化为松弛格式?

我愿意接受任何建议。

附录: Slackr可以选择上传文件,所以我最近的尝试是根据短信创建图像并上传该对象。

我可以使用magick库从文本消息中创建一个png文件。我创建了带有彩色背景的图像,然后将消息文本简单地添加到图像中:

library(magick)
alert_picture <- image_read('alert_480x150_dark_red.png')
alert_picture=image_annotate(alert_picture, DreamCloud_Alert, size = 20, gravity = "southwest", 
                           color = "white", location = "+10+10")
image_write(alert_picture, path = "alert_picture.png", format = "png")

enter image description here

图像看起来不错(尽管似乎没有一种简单的方法来加粗或加下划线消息中的特定单词),但是现在的障碍是我无法使上载命令起作用。

slackr_upload(filename = "alert_picture.png")

我没有收到任何错误消息,但是没有任何内容上传到松弛状态。

2 个答案:

答案 0 :(得分:1)

我通过使用httr包执行了将后图片功能松弛的操作来解决了这个问题。

感谢Adil B.提供解决方案:

Post Image to Slack Using HTTR package in R

答案 1 :(得分:1)

我不确定您的意思是什么,但我解决了通过更改slackr_bot()函数并仅删除代码末尾的2组3个反引号来允许像常规松弛消息中那样进行格式化的问题它说文字的地方。然后将其命名为slackr_bot1()之类的,然后您就可以发布格式化的消息。这是删除反引号后的功能:

    slackr_bot1 <- function(...,
                       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")) {

  if (incoming_webhook_url == "") {
    stop("No incoming webhook URL specified. Did you forget to call slackr_setup()?", call. = FALSE)
  }

  if (icon_emoji != "") { icon_emoji <- sprintf(', "icon_emoji": "%s"', icon_emoji)  }

  resp_ret <- ""

  if (!missing(...)) {

    # mimics capture.output

    # get the arglist
    args <- substitute(list(...))[-1L]

    # setup in-memory sink
    rval <- NULL
    fil <- textConnection("rval", "w", local = TRUE)

    sink(fil)
    on.exit({
      sink()
      close(fil)
    })

    # where we'll need to eval expressions
    pf <- parent.frame()

    # how we'll eval expressions
    evalVis <- function(expr) withVisible(eval(expr, pf))

    # for each expression
    for (i in seq_along(args)) {

      expr <- args[[i]]

      # do something, note all the newlines...Slack ``` needs them
      tmp <- switch(mode(expr),
                    # if it's actually an expresison, iterate over it
                    expression = {
                      cat(sprintf("> %s\n", deparse(expr)))
                      lapply(expr, evalVis)
                    },
                    # if it's a call or a name, eval, printing run output as if in console
                    call = ,
                    name = {
                      cat(sprintf("> %s\n", deparse(expr)))
                      list(evalVis(expr))
                    },
                    # if pretty much anything else (i.e. a bare value) just output it
                    integer = ,
                    double = ,
                    complex = ,
                    raw = ,
                    logical = ,
                    numeric = cat(sprintf("%s\n\n", as.character(expr))),
                    character = cat(sprintf("%s\n\n", expr)),
                    stop("mode of argument not handled at present by slackr"))

      for (item in tmp) if (item$visible) { print(item$value, quote = FALSE); cat("\n") }
    }

    on.exit()

    sink()
    close(fil)

    # combined all of them (rval is a character vector)
    output <- paste0(rval, collapse="\n")

    loc <- Sys.getlocale('LC_CTYPE')
    Sys.setlocale('LC_CTYPE','C')
    on.exit(Sys.setlocale("LC_CTYPE", loc))

    resp <- POST(url = incoming_webhook_url, encode = "form",
                 add_headers(`Content-Type` = "application/x-www-form-urlencoded",
                             Accept = "*/*"), body = URLencode(sprintf("payload={\"channel\": \"%s\", \"username\": \"%s\", \"text\": \"%s\"%s}",
                                                                       channel, username, output, icon_emoji)))
    warn_for_status(resp)
  }
  return(invisible())
}

slackr_bot1("*test* on time")

enter image description here