Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhook support for formatted markdown #201

Open
tarlgordon opened this issue May 13, 2024 · 3 comments
Open

Webhook support for formatted markdown #201

tarlgordon opened this issue May 13, 2024 · 3 comments
Assignees

Comments

@tarlgordon
Copy link

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
Copy link
Owner

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
Copy link
Author

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

@mrkaye97
Copy link
Owner

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 🙏

@stale stale bot added the wontfix label Aug 22, 2024
Repository owner deleted a comment from stale bot Aug 24, 2024
@mrkaye97 mrkaye97 removed the wontfix label Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants