slackapi / java-slack-sdk

Slack Developer Kit (including Bolt for Java) for any JVM language
https://slack.dev/java-slack-sdk/
MIT License
568 stars 210 forks source link

Message sent after file upload comes earlier than the file appears in the channel #1307

Closed burningxxx closed 2 months ago

burningxxx commented 2 months ago

(Describe your issue and goal here)

Reproducible in:

slack-api-client-1.38.2
slack-api-model-1.38.2
slack-app-backend-1.38.2

The Slack SDK version

slack-api-client-1.38.2 slack-api-model-1.38.2 slack-app-backend-1.38.2

Java Runtime version

Java 11

OS info

ProductName: macOS ProductVersion: 14.2.1 BuildVersion: 23C71 Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:34 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T8103

Steps to reproduce:

(Share the commands to run, source code, and project settings (e.g., pom.xml/build.gradle))

  1. Upload the file using Slack.getInstance().methods().filesUploadV2(request) make sure it completes successfully with if (response.isOk()) {
  2. Post a new message in the chat using Slack.getInstance().methods().chatPostMessage(chatPostMessageRequest) after that

Expected result:

The message should appear after the file.

Actual result:

Message comes before the file.

Requirements

In our case it's quite important because we are sending list of controls which should go after the uploaded file and not before cuz otherwise it creates wrong assumption for the user that those controls are from the file above and not below.

filmaj commented 2 months ago

Due to the way the file upload APIs work and what Slack does in the background, this won't be possible as you expect it. That is, issuing two API calls like this:

.. there is no guarantee the file will be completely uploaded and available within the Slack client. This is because the file APIs also do additional checks, such as malware and virus detection, in the background. This is also completely dependent upon the file size: larger files will take longer to show up in the client. This necessarily means the file uploading APIs should be interacted with an asynchronous way.

To accomplish what you are trying to achieve, my recommendation would be to listen for one of the file_* events, such as file_created or file_shared (depending on your context), before issuing the chat.postMessage API call. If you are sharing the file directly into a channel, I would listen for the file_shared event. Look for the file ID returned in the event and that it matches the file ID returned in the fileUploadV2 response before issuing the postMessage call.

Let us know how this goes for you - if it solves your problem, please close the issue. Thanks.

seratch commented 2 months ago

Another approach is to poll files.info API until the file metadata (especially shares property) is ready. See also: https://dev.to/seratch/further-tips-on-slacks-filesupload-deprecation-33j6

burningxxx commented 2 months ago

Thanks for your replies @filmaj @seratch.

Both these methods require us to request additional bot scope files:read and run through whole resubmission process of our published app once again that might take few months optimistically.

My personal opinion you should allow users of the SDK to decide whether they want it to be async or sync and maybe do this scans under the hood. Can you consider it as feature request as you can see these workarounds aren't easy to implement due to additional scopes needed for them to work.

For now I ended up to setting timeout before publishing the follow up message as tmp solution which isn't good but still better than nothing.

filmaj commented 2 months ago

I completely understand your frustration but that must be balanced with security considerations here, as well. I don't think from a product perspective we would sideline malware scanning as something that could be optional or secondary or happen after a user is able to interact with an uploaded file. Anything that would potentially expose end-users to malicious files would be a no-go. Sorry :(

I will close this issue but feel free to re-open if there is more to discuss on this topic or file a new one.