Search code examples
rslack

How to improve formatting of slack messages using slackr?


I'm using slackr to send alert messages to a Slack channel. It works great except the message format is not great and I want to improve it.

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)

Here is an example of how a message from slackr looks in Slack:

enter image description here

Here is an example of how I'd like it to look:

enter image description here

slackr doesn't seem to have many options in the way of formatting. I was thinking of building an image and inserting that, but I'm having trouble building an image out of a text file using R.

Perhaps there is another api I could call that could take my text and format it for slack?

I'm open to any suggestions.

Addendum: Slackr has an option to upload files, so my latest attempt is to create an image from the text message and upload that object.

I am able to create a png file from the text message using the magick library. I created an image with a colored background, and I simply add the message text to the image:

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

The image looks pretty good (although there doesn't seem to be an easy way to bold or underline specific words in the message), but the obstacle now is that I can't get the upload command to work.

slackr_upload(filename = "alert_picture.png")

I don't get any error messages but nothing is uploaded to slack.


Solution

  • I got around this issue by using the httr package to execute the post image function to slack.

    Thanks to Adil B. for providing the solution:

    Post Image to Slack Using HTTR package in R