Skip to content

Webhook support for formatted markdown #201

@tarlgordon

Description

@tarlgordon

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)

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions