BlakeWilliams / Elixir-Slack

Slack real time messaging and web API client in Elixir
MIT License
675 stars 182 forks source link

Support JSON payloads on web api chat methods #225

Closed juanazam closed 4 years ago

juanazam commented 4 years ago

Slack offical API supports JSON objects on these methods, but this package doesn't. I tried sending a JSON string on the text parameter and it doesn't work because of how HTTPoison is used underneath the hood. Any plans on supporting this?

https://api.slack.com/changelog/2017-10-keeping-up-with-the-jsons#what

juanazam commented 4 years ago

Note sure if change the "text" on the documentation to "view" would make it work as expected

acconrad commented 4 years ago

@juanazam want to give it a shot and send us a PR?

juanazam commented 4 years ago

@acconrad sure, I wll give it a try and let you know!

juanazam commented 4 years ago

@acconrad it's not as easy as I thought :(, this is what I'm doing as a workaround:

base_url = Application.get_env(:slack, :url, "https://slack.com")
endpoint = "#{base_url}/api/chat.postMessage"

HTTPoison.post!(
  endpoint,
  payload,
  [
    {"Content-Type", "application/json; charset=utf-8"},
    {"Authorization", "Bearer #{@slack_token}"}
  ]
)

payload contains UI elements as well as parameters for this case: channel

From what I understand on how the requests are created, it's going to be a fair amount of work to support this, I'm willing to implement this if you agree this would be nice to have and also deciding on an implementation approach before giving it a try

acconrad commented 4 years ago

@juanazam so you want to be able to POST JSON payloads? What is wrong with the current slack methods, just so I understand better? What is the use case you're looking for here?

juanazam commented 4 years ago

@acconrad exactly, I want to send JSON payloads.

For this case in particular, not supporting JSON means that we cannot use rich UI elements on the message.

With JSON payloads I'm able to send a message with UI elements from slack (BlockKit). This is just not possible with the current implementation. The "text" parameter as it's been sent and processed is treated as simple text.

acconrad commented 4 years ago

@juanazam are you referring to something like a Modal? Modals use BlockKit and I am able to structure modals via Maps (view.open offers a view property which can be built via Maps)

juanazam commented 4 years ago

I mean something like rich messages with buttons for example But not using the view methods because I'm sending a message.

Slack.Web.Chat.post_message

This method expects the message to be just text, but the slack API supports sending BlockKit responses to include UI elements on the message.

acconrad commented 4 years ago

So I think the way you would have to do this is create an overloaded function of the documentation function with a slightly modified argument set that allows you to pass either the Elixir arguments or one argument which is a Map that can be JSON-stringified.

juanazam commented 4 years ago

@acconrad cool, just wanted to make sure you were onboard with the idea before doing anything :)

juanazam commented 4 years ago

@acconrad turns out what I wanted to do was possible with current implementation, but one argument was missing https://github.com/BlakeWilliams/Elixir-Slack/pull/226

acconrad commented 4 years ago

Thanks for the help @juanazam ! 0.23.5 published - feel free to add to your package now!