mrkaye97 / slackr

An R package for sending messages from R to Slack
https://matthewrkaye.com/slackr/
Other
307 stars 83 forks source link

Webhook support for formatted markdown #201

Open tarlgordon opened 2 months ago

tarlgordon commented 2 months ago

Is your feature request related to a problem? Please describe. Using the slackr_bot() function, I am unable to post Slack Markdown "mrkdwn".

Describe the solution you'd like I would like slackr_bot() to accept Slack Markdown formatted text.

Describe alternatives you've considered From what I've been able to read on the Slack API, it should be possible for even webhook bots to be able to post Markdown text, although it is possible that this is not supported.

Additional context I wrote some code to try out, but I don't have enough experience with CRAN / packages / other people's code to straighten out my namespaces. I can't get the POST function in a version of slackr_bot that I tried to add this ability. Even changing the POST to httr::POST didn't straighten me out.

My attempt was to change: function(..., incoming_webhook_url = Sys.getenv("SLACK_INCOMING_WEBHOOK_URL")) { to: function(..., incoming_webhook_url = Sys.getenv("SLACK_INCOMING_WEBHOOK_URL"),formatted=F) {

and then change:

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

to:

resp <- POST(
        url = incoming_webhook_url,
        encode = "form",
        add_headers(
          `Content-Type` = "application/x-www-form-urlencoded",
          Accept = "*/*"
        ),
        body = ifelse(formatted,
                      URLencode(
                        sprintf(
                          "payload=%s",
                          output
                        )
                      ), # End URLencode without formatting
                      URLencode(
                        sprintf(
                          "payload={\"text\": \"```%s```\"}",
                          output
                        )
                      )
                      ) # End URLencode with formatting
      ) # End POST

Test cases:

slackr_bot('Test message') # Test original syntax
slackr_bot('Test message',formatted=F) # Test original syntax with explicit argument
slackr_bot('{\"text\": \"```Test message```\"}',formatted=T) # Explicitly format message to what the function does already
slackr_bot('{\"text\": \"Test message\"}',formatted=T) # Minor modification to the original format
slackr_bot('{ # Test case with section keyword
  \"type\": \"section\",
  \"text\": {
    \"type\": \"mrkdwn\",
    \"text\": \"Formatted text with section\"
  }
}',formatted=T)
mrkaye97 commented 2 months ago

Thanks! I think this is a good proposal and I can look into it. In general, my sense is we should support more than just text, because Slack has a whole Blocks API that you can use to send nicely formatted webhook messages with all kinds of data

tarlgordon commented 1 month ago

@mrkaye97 , do you have any idea when you'll be able to take a look?

mrkaye97 commented 1 month ago

hey sorry, thanks for the ping! I suspect this is going to be a pretty big change to the package if we wanted to do this right. I'm not spending much time maintaining slackr nowadays so I'm not totally sure if I'll get to it, but I can let you know!

If you're interested in contributing by the way, would be happy to review a PR for this 🙏